# Cropping out two images from one big image by manually selecting the cropping are

9 views (last 30 days)
windmill on 25 Oct 2020
Commented: Ameer Hamza on 29 Oct 2020 at 12:55
How can I crop two images out from one big image by manually selecting the region for cropping out with a rectangle for example? Additionally, how can I extract the rows and columns that I selected with the rectangle?

Ameer Hamza on 25 Oct 2020
Try this code
figure();
ax1 = axes();
imshow(img, 'Parent', ax1)
fprintf('Drag a rectange on the image.\n')
roi = drawrectangle(ax1);
img_small = imcrop(img, roi.Position);
figure();
ax2 = axes();
imshow(img_small, 'Parent', ax2)

Show 1 older comment
Ameer Hamza on 25 Oct 2020
Yes, check the value of
roi.Position
It contains [x y width height] of the extacted image. x and y are coordinates (in pixels) of the top left corner of the rectangle.
windmill on 28 Oct 2020 at 6:57
Thanks, I got it! I just tried to modfify the code so I can use my images that I converted to grayscale images, but I don't know why it won't work anymore... Do you see the problem?
max(max(image))
image2 = mat2gray(image)*255
figure();
ax1 = axes();
imshow(image2, 'Parent', ax1)
fprintf('Draw the ROI on the image using the rectangle.\n')
roi = drawrectangle(ax1);
roiPos=roi.Position;
cropped_image = imcrop(image2, roi.Position);
figure();
ax2 = axes();
imshow(cropped_image, 'Parent', ax2)
Ameer Hamza on 29 Oct 2020 at 12:55
Try this code
image2 = rgb2gray(image);
figure();
ax1 = axes();
imshow(image2, 'Parent', ax1)
fprintf('Draw the ROI on the image using the rectangle.\n')
roi = drawrectangle(ax1);
roiPos=roi.Position;
cropped_image = imcrop(image2, roi.Position);
figure();
ax2 = axes();
imshow(cropped_image, 'Parent', ax2)

Image Analyst on 25 Oct 2020
Did you check out the documentation for imcrop(), or rbbox()?

#### 1 Comment

windmill on 25 Oct 2020
Thank you, haven't heard about rbbox() before, but will make sure to check it out

### Community Treasure Hunt

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

Start Hunting!

Translated by