Clear Filters
Clear Filters

imrect function how I can show the image ?

7 views (last 30 days)
Hi guys, I want to show the position that I choosed by using imrect for example see the code ..
I = imread('---.png');
imshow(I);
h = imrect;
imshow(h);
here I want to show the h How I can do it ??
  3 Comments
Khaled Al-Faleh
Khaled Al-Faleh on 14 Apr 2017
I cropped the image by rectangular box using imrect so I want to show that cropped ..
Khaled Al-Faleh
Khaled Al-Faleh on 14 Apr 2017
let say that I have this image I i used imrect to select what I want from the image so I need to show that selection ... is it clear ?

Sign in to comment.

Accepted Answer

Image Analyst
Image Analyst on 14 Apr 2017
See this snippet from the attached demo.
% Have user specify the area they want to define as neutral colored (white or gray).
promptMessage = sprintf('Drag out a box over the ROI you want to be neutral colored.\nDouble-click inside of it to finish it.');
titleBarCaption = 'Continue?';
button = questdlg(promptMessage, titleBarCaption, 'Draw', 'Cancel', 'Draw');
if strcmpi(button, 'Cancel')
return;
end
hBox = imrect;
roiPosition = wait(hBox); % Wait for user to double-click
roiPosition % Display in command window.
% Get box coordinates so we can crop a portion out of the full sized image.
xCoords = [roiPosition(1), roiPosition(1)+roiPosition(3), roiPosition(1)+roiPosition(3), roiPosition(1), roiPosition(1)];
yCoords = [roiPosition(2), roiPosition(2), roiPosition(2)+roiPosition(4), roiPosition(2)+roiPosition(4), roiPosition(2)];
croppingRectangle = roiPosition;
% Display (shrink) the original color image in the upper left.
subplot(2, 4, 1);
imshow(rgbImage);
title('Original Color Image', 'FontSize', fontSize);
% Crop out the ROI.
whitePortion = imcrop(rgbImage, croppingRectangle);
subplot(2, 4, 5);
imshow(whitePortion);
caption = sprintf('ROI.\nWe will Define this to be "White"');
title(caption, 'FontSize', fontSize);
Once you have croppingRectangle you can put it into the overlay above the image using rectangle(). Or you could use xCoords and yCoords and the plot() function to plot the box over the image.
  12 Comments
Image Analyst
Image Analyst on 15 Apr 2017
You can use the function rbbox() instead of imrect():
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));
width = x2-x1;
height = y2-y1;
% The box from rbbox() disappears after drawing, so redraw the box over the image.
hold on
axis manual
plot(xCoords, yCoords, 'b-'); % redraw in dataspace units

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!