I want to split an image into several pieces and automatically save them without needing to crop them manually. How can I do that?

10 views (last 30 days)
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 Comments
Mohammad Nazmus Saquib
Mohammad Nazmus Saquib on 17 Feb 2022
I want 10 tites across down. It doesn't matter though if I can parametrize it and change it later according to necessity.

Sign in to comment.

Accepted Answer

yanqi liu
yanqi liu on 17 Feb 2022
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 Comments
Mohammad Nazmus Saquib
Mohammad Nazmus Saquib on 17 Feb 2022
@yanqi liu if I want to save the split parts into a different folder, is there any idea what to do? Currently I'm trying this but it saves in the original directory.
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)), sprintf('A1%d.tif', i));
end
yanqi liu
yanqi liu on 18 Feb 2022
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

Sign in to comment.

More Answers (1)

DGM
DGM on 17 Feb 2022
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

Categories

Find more on Image Processing Toolbox in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!