how to draw a free hand box on an image to select that region of interest?

4 Ansichten (letzte 30 Tage)
hi,
I used this for free hand drawing in an irregular shape. But I need to draw a box to select a region.
% Get the input free hand image
figure, imshow(grayImage, []);
axis on;
title('Contour Region Required', 'FontSize', fontSize);
set(gcf, 'Position', get(0,'Screensize')); % Maximize figure.
message = sprintf('Left click and hold to begin drawing.\nRelease Mouse Button when Completed');
uiwait(msgbox(message));
hFH = imfreehand();
% Create a binary image ("mask") from the ROI object.
binaryImage = hFH.createMask();
xy = hFH.getPosition;
% Label the binary image and compute the centroid and center of mass.
labeledImage = bwlabel(binaryImage);
% Will keep only the part of the image that's inside the mask, zero outside mask.
blackMaskedImage = grayImage;
blackMaskedImage(~binaryImage) = 0;
Can you please suggest me free hand drawing a box.
Thanks...

Akzeptierte Antwort

Sean de Wolski
Sean de Wolski am 22 Dez. 2014
To force it to a rectangle, use imrect which has the same api as imfreehand.
  3 Kommentare
Manoj Kumar
Manoj Kumar am 27 Jan. 2015
Is there any way that after drawing the box, Can i rotate it ( I mean can I perform any orientation to the box)?
Sean de Wolski
Sean de Wolski am 27 Jan. 2015
Not easily. You would have to build a second impoly and then manually transform its vertices.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (2)

Chad Greene
Chad Greene am 22 Dez. 2014
If you have the Image Processing Toolbox, http://www.mathworks.com/help/images/ref/imfreehand.html
  2 Kommentare
Image Analyst
Image Analyst am 22 Dez. 2014
imfreehand() is for an irregular shape (and he's already using that), not for a perfect box with straight sides.

Melden Sie sich an, um zu kommentieren.


Image Analyst
Image Analyst am 22 Dez. 2014
If you don't have the Image Processing Toolbox, you can use rbbox():
k = waitforbuttonpress;
point1 = get(gca,'CurrentPoint'); % button down detected
finalRect = rbbox; % return figure units
point2 = get(gca,'CurrentPoint'); % button up detected
point1 = point1(1,1:2); % extract x and y
point2 = point2(1,1:2);
p1 = min(point1,point2); % calculate locations
offset = abs(point1-point2); % and dimensions
% Find the coordinates of the box.
xCoords = [p1(1) p1(1)+offset(1) p1(1)+offset(1) p1(1) p1(1)];
yCoords = [p1(2) p1(2) p1(2)+offset(2) p1(2)+offset(2) p1(2)];
x1 = round(xCoords(1));
x2 = round(xCoords(2));
y1 = round(yCoords(5));
y2 = round(yCoords(3));
hold on
plot(xCoords, yCoords, 'b-'); % redraw in dataspace units
  2 Kommentare
Manoj Kumar
Manoj Kumar am 24 Dez. 2014
Then how to get the coordinates of the two points that I use to make a rectange using imrect()?
Image Analyst
Image Analyst am 24 Dez. 2014
Did you overlook how I get x1,x2,y1,y2?
And you don't use imrect anymore - this is used in place of it. One advantage is that you just drag out a box and you're done. It doesn't let you adjust the size (of course you could put it in a loop where you redraw it) but you don't have the extra step of having to double-click inside the box (which is confusing to users - they won't know to do that unless you give them instructions in advance).

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Geometric Transformation and Image Registration finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by