How to crop 4 different images from a single image?

1 view (last 30 days)
I have a form with 4 image fields, stacked together horizontally, I want to crop these 4 images automatically, and want to save these 4 images individually with 4 different file names. How could I perform it using Matlab. Can you help me with some code for it?

Accepted Answer

Image Analyst
Image Analyst on 17 Feb 2014
[rows, columns, numberOfColorChannels] = size(theImage);
col4 = round(columns/4, columns/2, 3*columns/4);
if numberOfColorChannels == 1
% Gray scale image.
image1 = theImage(:, 1:col4(1));
image2 = theImage(:, col4(1)+1:col4(2));
image3 = theImage(:, col4(2)+1:col4(3));
image4 = theImage(:, col4(3)+1:end);
else
% Color image.
image1 = theImage(:, 1:col4(1), :);
image2 = theImage(:, col4(1)+1:col4(2), :);
image3 = theImage(:, col4(2)+1:col4(3), :);
image4 = theImage(:, col4(3)+1:end, :);
end
Then use imwrite to save each quadrant.
  2 Comments
Image Analyst
Image Analyst on 17 Feb 2014
OK, with that image, you need to threshold it
binaryImage = grayImage < 128;
and then skeltonize the image with bwmorph,
binaryImage = bwmorph(binaryImage, 'skel', inf);
extract a line half way down and find the middle line and use find() to figure out where the vertical lines are.
lineLocations = find(binaryImage(rows/2,:));
Then use those columns to extract the images like I did above. Let me know if you still can't figure it out.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!