Hi everyone i want to ask about im crop

조회 수: 1 (최근 30일)
pizzaa
pizzaa 2023년 7월 5일
답변: Image Analyst 2023년 7월 5일
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

답변 (2개)

Nitya Patel
Nitya Patel 2023년 7월 5일
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
Image Analyst 2023년 7월 5일
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.
rgbImage = imread('peppers.png');
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')

카테고리

Help CenterFile Exchange에서 Images에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by