How to select the ROI of specific size over the image ?

Hi
I want to select the ROI of 128 by 128 within the image attached. Using the imrect function we can draw the ROI but I wanted to select constant size of 128 by 128 can you please suggest some method where I can provide the size roi and it will get draw on image. Thank you

 采纳的回答

Let's say that your image is I. Then using the follow code you will get a 128x128 ROI on your image. You can move and drag the ROI using your mouse. Once it is located at the place you want just double click inside it.
You can also change the values of S to move the ROI.
S = [1 1 128 128]; %the size of your ROI starts at point X1, Y1
I = imread('cameraman.tif'); % your input image
figure, imshow(I);
h = imrect(gca, S);
addNewPositionCallback(h,@(p) title(mat2str(p,3)));
fcn = makeConstrainToRectFcn('imrect',get(gca,'XLim'),get(gca,'YLim'))
setPositionConstraintFcn(h,fcn)
position = wait(h);
I2 = imcrop(I,position);
imshow(I2); % the output image of your ROI
Hope that helps you.
Meshoo

6 个评论

Thank you so much it works. Can you please explain the use of @ I usually get hard time in understanding. Thanks
That part was not important. I just added it such that you can see the location of your ROI when you start moving it with your mouse. So for example if you start moving your ROI you will see a text appear above to your input image.
If you don't need that, then just use this smaller version of the code.
S = [1 1 128 128]; %the size of your ROI starts at point X1, Y1
I = imread('cameraman.tif'); % your input image
figure, imshow(I);
h = imrect(gca, S);
position = wait(h);
I2 = imcrop(I,position);
imshow(I2); % the output image of your ROI
Hi Meshoo,
Thanks, I found your answer really helpful for something else I'm trying.
Say you wanted to place many such equivalently-sized ROIs, and identify and record each one separately on the same image.
How would you go about that?
Looking forward to hearing your suggestions,
Thanks!
Best,
Veena
Please define "identify and record".
If you placed them, then you must already know everything about them so just use some method to put them on the image, such as patch(), imoverlay(), plot(), rectangle() or however you do it.
Hi Image Analyst,
Thanks a ton for your reply and suggestions. Excited to try out those functions!
I spent a little time working towards this differently yesterday before I saw your comment.
I'm looking to place ROI circles of the same size on about 5-10 coordinate points in an image.
So far, I've extracted and plotted the coordinates of these points by clicking on them with crosshairs:
imshow(Image,[]);
set(gcf, 'units','normalized','outerposition',[0 0 1 1]); %For fullscreen
[x, y] = getpts; % I know this function isn't recommended any more, any better ideas?
hold on
plot(x, y, 'o', 'MarkerSize', 5, 'MarkerEdgeColor','green', 'MarkerFaceColor','green');
hold off
(Look at the tiny green dots above)
What I'd like to do further is to place circle ROIs of a specific radius on each dot, and create a mask with each circle ROI separately, retaining its specific coordinate information.
It would be ideal to have labels for each circle ROI on the final image.
I look forward to hearing your suggestions!
Thanks,
Veena
I created a separate question here for this. Thanks!

请先登录,再进行评论。

更多回答(2 个)

Either rectangle(), plot(), or line() can do that. Be sure you call "hold on" before you draw the box over the image or else the box will blow the image away.
With the newer roi-commands it also can look like this:
height = 128;
width = height;
imshow(image)
roi = images.roi.Rectangle(gca,'Position',[1,1,width,height],'FixedAspectRatio',true,'InteractionsAllowed','translate');
roi = customWait(roi);
function oROI = customWait(hROI)
%from function customWait https://www.mathworks.com/help/images/use-wait-function-after-drawing-roi-example.html
% Listen for mouse clicks on the ROI
l = addlistener(hROI,'ROIClicked',@clickCallback);
% Block program execution
uiwait;
% Remove listener
delete(l);
% Return the current roi
oROI = hROI;
end
function clickCallback(~,evt)
if strcmp(evt.SelectionType,'double')
uiresume;
end
end
Hope this helps anyone because I was searching for a solution for a few days.

Community Treasure Hunt

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

Start Hunting!

Translated by