I want to split an image into several pieces and automatically save them without needing to crop them manually. How can I do that?
78 ビュー (過去 30 日間)
古いコメントを表示
Currently I'm using this code to split an image and save them. But I need to crop the split image manually before I save them. I'm doing this way because I don't want any white border around my image. So I'm trying to write a code where The image will be cropped automatically according to given dimension and save as cropped. Can anyone help to get there? I'm currently using this code:
clf;
clear all;
I = imread('Image1-Filter-segmented.tif');
[r c]= size(I);
A11=I(1:1*r/12,1:1*c/7);
A12=I(1:1*r/12,1*c/7:2*c/7);
A13=I(1:1*r/12,2*c/7:3*c/7);
A14=I(1:1*r/12,3*c/7:4*c/7);
A15=I(1:1*r/12,4*c/7:5*c/7);
A16=I(1:1*r/12,5*c/7:6*c/7);
A17=I(1:1*r/12,6*c/7:7*c/7);
imshow(A11);
crop=imcrop;
imwrite(crop,'A11.tif');
imshow(A12);
4 件のコメント
採用された回答
yanqi liu
2022 年 2 月 17 日
clc;clear all;close all;
I = imread('cameraman.tif');
[r, c]= size(I);
A11=I(1:1*round(r/12),1:round(1*c/7));
A12=I(1:1*round(r/12),round(1*c/7):round(2*c/7));
A13=I(1:1*round(r/12),round(2*c/7):round(3*c/7));
A14=I(1:1*round(r/12),round(3*c/7):round(4*c/7));
A15=I(1:1*round(r/12),round(4*c/7):round(5*c/7));
A16=I(1:1*round(r/12),round(5*c/7):round(6*c/7));
A17=I(1:1*round(r/12),round(6*c/7):round(7*c/7));
% display
sz = size(I);
[x, y] = meshgrid(linspace(1,sz(2),8), linspace(1,sz(1),13));
y(y>r/12+1) = NaN;
z = ones(size(x));
figure;
imshow(I, []);
hold on; axis off;
mesh(x, y, z, 'FaceColor', 'none', ...
'EdgeColor', 'r', 'LineWidth', 1);
% save
for i = 1 : 7
imwrite(eval(sprintf('A1%d',i)), sprintf('A1%d.tif', i));
end
4 件のコメント
yanqi liu
2022 年 2 月 18 日
yes,sir,may be use this
clc;clear all;close all;
I = imread('cameraman.tif');
[r, c]= size(I);
A11=I(1:1*round(r/12),1:round(1*c/7));
A12=I(1:1*round(r/12),round(1*c/7):round(2*c/7));
A13=I(1:1*round(r/12),round(2*c/7):round(3*c/7));
A14=I(1:1*round(r/12),round(3*c/7):round(4*c/7));
A15=I(1:1*round(r/12),round(4*c/7):round(5*c/7));
A16=I(1:1*round(r/12),round(5*c/7):round(6*c/7));
A17=I(1:1*round(r/12),round(6*c/7):round(7*c/7));
% display
sz = size(I);
[x, y] = meshgrid(linspace(1,sz(2),8), linspace(1,sz(1),13));
y(y>r/12+1) = NaN;
z = ones(size(x));
figure;
imshow(I, []);
hold on; axis off;
mesh(x, y, z, 'FaceColor', 'none', ...
'EdgeColor', 'r', 'LineWidth', 1);
% save the split parts into a different folder
folder_name = 'r1';
if ~exist(folder_name, 'dir')
mkdir(folder_name);
end
for i = 1 : 7
filepath = fullfile(folder_name);
imwrite(eval(sprintf('A1%d',i)), fullfile(folder_name,sprintf('A1%d.tif', i)));
end
その他の回答 (1 件)
DGM
2022 年 2 月 17 日
Without further information, this is a basic example using the following image.
I = imread('letterscatter.png');
tiling = [2 4]; % [tilesdown tilesacross]
framerange = [1 8]; % which tiles to save
padcolor = 255; % presumed border color to crop off
tol = 5; % color tolerance
% tiling order is row-wise
tilesize = [size(I,1) size(I,2)]./tiling
C = mat2cell(I,repmat(tilesize(1),1,tiling(1)),repmat(tilesize(2),1,tiling(2))).';
for f = 1:prod(tiling)
% find the minimal box enclosing all pixels which aren't the padding color
cdiff = abs(padcolor-C{f})>tol;
rmap = any(cdiff,2);
cmap = any(cdiff,1);
rrange = find(rmap,1,'first'):find(rmap,1,'last');
crange = find(cmap,1,'first'):find(cmap,1,'last');
% crop
thisframe = C{f}(rrange,crange);
% save
fname = sprintf('frame_%02d.tif',f);
imwrite(thisframe,fname)
% show
imshow(thisframe)
pause(0.5)
end
0 件のコメント
参考
カテゴリ
Help Center および File Exchange で Convert Image Type についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!