# Hi everyone i want to ask about im crop

3 views (last 30 days)
pizzaa on 5 Jul 2023
Answered: Image Analyst on 5 Jul 2023
So i already deteremined the pixel distance in 1 cm which is 1030 pixel in my photoes,
I want to ask how do i make an interactive crop region whic is 1cm x 1cm in every side??
in other words, i want to make a react which has 1cm for each side, and i can move this react freely along the image to crop

Nitya Patel on 5 Jul 2023
To create an interactive crop region in MATLAB with a fixed size of 1cm x 1cm, you can use the imrect function from the Image Processing Toolbox. This function allows you to create a draggable rectangle on the image and obtain the coordinates of the selected region. But it is deprecated. A newer way is using the Rectangle function.
Here are the documentation links:

Image Analyst on 5 Jul 2023
Since you already know the pixels per cm is 1030, you can crop out a 1 cm region using indexing if you know the upper left row and column. Full demo below:
% Display sample image.
imshow(rgbImage);
axis('on', 'image')
% Ask user to interactively draw a box. Of course it won't be 1 cm but
% that's OK, we'll just go 1 cm from the upper left corner that they
% clicked on.
uiwait(helpdlg('Draw a box'));
% User draws a box. It exits as soon as they lift the mouse.
hBox = drawrectangle('Color', 'r');
% Get the coordinates in the form [xLeft, yTop, width, height].
roiPosition = hBox.Position;
% Delete the ROI object.
delete(hBox);
% and replace it with a rectangle in the graphical overlay.
hold on;
hRect = rectangle('Position', roiPosition, 'EdgeColor', 'r', 'LineWidth', 2);
%----------------------------------------------------------------------------------------------
% Do the crop:
pixelsPerCM = 103; % Just for peppers image. Change to 1030 for your image!!!!!!!!!!!
startingRow = round(roiPosition(2))
startingColumn = round(roiPosition(1))
croppedImage = rgbImage(startingRow : (startingRow + pixelsPerCM - 1), startingColumn : (startingColumn + pixelsPerCM - 1), :);
% Display cropped image in a new figure.
figure;
imshow(croppedImage);
axis('on', 'image')

### Categories

Find more on Images 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!