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

127 views (last 30 days)
Parag on 28 May 2015
Answered: Jonas Thomalla on 11 Nov 2020
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
##### 0 CommentsShowHide -1 older comments

Sign in to comment.

### Accepted Answer

Meshooo on 29 May 2015
Edited: Meshooo on 29 May 2015
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 CommentsShowHide 5 older comments
Veena Chatti on 8 Mar 2020
I created a separate question here for this. Thanks!

Sign in to comment.

### More Answers (2)

Image Analyst on 28 May 2015
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.
##### 0 CommentsShowHide -1 older comments

Sign in to comment.

Jonas Thomalla on 11 Nov 2020
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.
##### 0 CommentsShowHide -1 older comments

Sign in to comment.

### Categories

Find more on ROI-Based Processing 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!

Translated by