imrect function how I can show the image ?

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 comentarios

Nick
Nick el 14 de Abr. de 2017
What do you mean by show the h?
Do you want to crop the image to that region?
Khaled Al-Faleh
Khaled Al-Faleh el 14 de Abr. de 2017
I cropped the image by rectangular box using imrect so I want to show that cropped ..
Khaled Al-Faleh
Khaled Al-Faleh el 14 de Abr. de 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 ?

Iniciar sesión para comentar.

 Respuesta aceptada

Image Analyst
Image Analyst el 14 de Abr. de 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 comentarios

Khaled Al-Faleh
Khaled Al-Faleh el 14 de Abr. de 2017
Thanks sir for answering but is it necessary to write all this to show the selection part sir ?
Image Analyst
Image Analyst el 14 de Abr. de 2017
You said "I need to show that selection" and, more puzzlingly, " I want to show the h", which is puzzling since h is an ROI object, not really something that you can show. Normally when you double-click inside the rectangle, the rectangle disappears. I thought by "show" you meant that you wanted to show/keep the rectangle over the original image, and to crop the image. So I showed you how to do both of those. You can do either/and/or/both - it doesn't matter to me. If you don't want to show the rectangle on the image, or to crop out the sub-image, then explain better what you want to do.
Khaled Al-Faleh
Khaled Al-Faleh el 15 de Abr. de 2017
thanks sir, let say that I have any image I used imrect to let the user to select what he want from the image so I need to show what he select and save it in new variable its look like imcrop but here I want the selection done by user ,because I will do some process on this image which the user select .. so did you understand sir ?
Yes I understand. Perhaps my demo was too much so I scaled it back to just the essential stuff, or at least less than there was before. Here it is - hope you can follow it now:
% Asks the user to draw a box and displays it on the image.
% Crops out the part of the image within the box to a new image.
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
clear; % Erase all existing variables.
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 15;
% Read in a standard MATLAB color demo image.
baseFileName = 'peppers.png';
folder = fileparts(which('peppers.png')); % Determine where demo folder is (works with all versions).
% Get the full filename, with path prepended.
fullFileName = fullfile(folder, baseFileName);
if ~exist(fullFileName, 'file')
% Didn't find it there. Check the search path for it.
fullFileName = baseFileName; % No path this time.
if ~exist(fullFileName, 'file')
% Still didn't find it. Alert user.
errorMessage = sprintf('Error: %s does not exist.', fullFileName);
uiwait(warndlg(errorMessage));
return;
end
end
[rgbImage, colorMap] = imread(fullFileName);
% Get the dimensions of the image. numberOfColorBands should be = 3.
[rows, columns, numberOfColorBands] = size(rgbImage);
% If it's an indexed image (such as Kids), turn it into an rgbImage;
if numberOfColorBands == 1
rgbImage = ind2rgb(rgbImage, colorMap); % Will be in the 0-1 range.
rgbImage = uint8(255*rgbImage); % Convert to the 0-255 range.
end
% Display the original color image full screen
imshow(rgbImage);
axis on;
title('Double-click inside box to finish box', 'FontSize', fontSize);
% Enlarge figure to full screen.
set(gcf, 'units','normalized','outerposition', [0 0 1 1]);
% 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.\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 top half.
subplot(2, 1, 1);
imshow(rgbImage);
title('Original Color Image', 'FontSize', fontSize);
% Plot the box again so we can see it.
hold on;
plot(xCoords, yCoords, 'r-', 'LineWidth', 3);
rectangle('Position', croppingRectangle);
% Crop out the ROI.
croppedPortion = imcrop(rgbImage, croppingRectangle);
subplot(2, 1, 2);
imshow(croppedPortion);
caption = sprintf('ROI you drew');
title(caption, 'FontSize', fontSize);
message = sprintf('Done with the demo.');
uiwait(helpdlg(message));
Khaled Al-Faleh
Khaled Al-Faleh el 15 de Abr. de 2017
sir there is an error appear at hBox = imrect; how I can solve it please ?
Khaled Al-Faleh
Khaled Al-Faleh el 15 de Abr. de 2017
the error say
  • Attempt to execute SCRIPT imrect as a function:
  • C:\Users\Khalid\Documents\MATLAB\imrect.m
  • Error in imrect (line 46)
  • hBox = imrect;
Khaled Al-Faleh
Khaled Al-Faleh el 15 de Abr. de 2017
sir how I can solve it please ??
Khaled Al-Faleh
Khaled Al-Faleh el 15 de Abr. de 2017
I solve it thanks sir
Image Analyst
Image Analyst el 15 de Abr. de 2017
You haven't accepted the answer yet, so what's still left to solve?
Sir one more question please here
roiPosition = wait(hBox); % Wait for user to double-click
I don't want to wait the user to double-click I want to run it and select and finish no need to wait or double click ..
Khaled Al-Faleh
Khaled Al-Faleh el 15 de Abr. de 2017
how we can do it sir ?
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

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Convert Image Type en Centro de ayuda y File Exchange.

Etiquetas

Preguntada:

el 14 de Abr. de 2017

Comentada:

el 15 de Abr. de 2017

Community Treasure Hunt

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

Start Hunting!

Translated by