Divide images into four equal images
Clash Royale CLAN TAG#URR8PPP
Divide images into four equal images
How to divide a set of images which (512*512
) into four equal images; each is 256*256
and this repeated for 150
images ?
512*512
256*256
150
I = imread(['file',num2str(i),'.tif']);
I1 = I(1:size(I,1)/2,1:size(I,2)/2,:);
I2 = I(size(I,1)/2+1:size(I,1),1:size(I,2)/2,:);
I3 = I(1:size(I,1)/2,size(I,2)/2+1:size(I,2),:);
I4 = I(size(I,1)/2+1:size(I,1),size(I,2)/2+1:size(I,2),:);
1 Answer
1
I don't have matlab at hand, so there might be some mistakes, but the idea will hold:
You have to choose how you will store your split up images. For example, you can use cells. I'll assume your images are called file1.tif, file2.tif and so on.
num_imgs = 150; %The total ammount of images you have
cropped_imgs = cell(1,num_imgs) %create empty 1 by 150 cell
%create a for loop to repeat over all images
for k in 1:num_imgs:
I = imread(['file',num2str(k),'.tif']); %load your image
sz = size(I,1)/2; %get half the size, assuming it's a square image
%split images and pack into 1 by 4 cell
cropped_imgsk = I(1:sz, 1:sz), I(1:sz, sz+1:end), I(sz+1:end, 1:sz), I(sz+1:end, sz+1:end)
end
Now you can access them by doing something like cropped_imgs1272
for the second crop of image number 127. Hope this helps, but seriously, research a bit before posting a question. Maybe a better way to store the images would be in a 150 by 4 cell and access them by cropped_imgs127,2
, but that depends on what you like.
cropped_imgs1272
cropped_imgs127,2
EDIT: If you were asking how to split images in a less explicit way, you could try and use imcrop
with a given rectangle and move the rectangle:
imcrop
num_imgs = 150; %The total ammount of images you have
cropped_imgs = cell(4,num_imgs) %create empty 4 by 150 cell
%create a for loop to repeat over all images
for k in 1:num_imgs:
I = imread(['file',num2str(k),'.tif']); %load your image
sz = size(I)/2; %get half the size, assuming it's a square image
%split images and pack into 1 by 4 cell
cropped_imgsk,1 = imcrop(I,[0,0,sz]);
cropped_imgsk,2 = imcrop(I,[sz(1),0,sz]);
cropped_imgsk,3 = imcrop(I,[0,sz(1),sz]);
cropped_imgsk,4 = imcrop(I,[sz,sz]);
end
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.