How do I use for loop to crop an image 4 times?

Hi,
I have a 2848x4288 image, and I need to use a for loop to crop out four 1024x1024 images in the center of the original 2848x4288 image. I want the for loop to crop the first image (i.e. upper left quadrant), and then "transfer" the 1024x1024 template to each of the next 3 areas to be cropped out (i.e. the upper right, lower left, and lower right quadrants). In the end, I want to have four separate images.
I'm very new to Matlab and coding so any help would be much appreciated!
Thank you!

Answers (1)

A for loop isn't really necessary. The centre of the image is at row 2848/2=1424 and column 4288/2=2144, and this centre (coordinate) will be one corner on any of the four images that you want to crop. If img is your 2848x4288 image then
cropLen = 1024; % the length of side of the cropped image
ctrRow = 2848/2; % the image centre row
ctrCol = 4288/2; % the image centre column
% for the upper left quadrant, the image centre is the lower right corner
upperLeftQuad = img(ctrRow-cropLen+1:ctrRow,ctrCol-cropLen+1:ctrCol,:);
% for the upper right quadrant, the image centre is the lower left corner
upperRightQuad = img(ctrRow-cropLen+1:ctrRow,ctrCol+1:ctrCol+cropLen,:);
% for the lower left quadrant, the image centre is the upper right corner
lowerLeftQuad = img(ctrRow+1:ctrRow+cropLen,ctrCol-cropLen+1:ctrCol,:);
% for the lower right quadrant, the image centre is the upper left corner
lowerRightQuad = img(ctrRow+1:ctrRow+cropLen,ctrCol+1:ctrCol+cropLen,:);
The above could be put in a for loop if desired.

Categories

Asked:

on 12 Jun 2014

Edited:

on 12 Jun 2014

Community Treasure Hunt

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

Start Hunting!