imrect function how I can show the image ?
이 질문을 팔로우합니다.
- 팔로우하는 게시물 피드에서 업데이트를 확인할 수 있습니다.
- 정보 수신 기본 설정에 따라 이메일을 받을 수 있습니다.
오류 발생
페이지가 변경되었기 때문에 동작을 완료할 수 없습니다. 업데이트된 상태를 보려면 페이지를 다시 불러오십시오.
이전 댓글 표시
0 개 추천
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
Nick
2017년 4월 14일
What do you mean by show the h?
Do you want to crop the image to that region?
I cropped the image by rectangular box using imrect so I want to show that cropped ..
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 ?
채택된 답변
Image Analyst
2017년 4월 14일
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
Thanks sir for answering but is it necessary to write all this to show the selection part sir ?
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.
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));

sir there is an error appear at hBox = imrect; how I can solve it please ?
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;
sir how I can solve it please ??
I solve it thanks sir
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 ..
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
추가 답변 (0개)
카테고리
도움말 센터 및 File Exchange에서 Convert Image Type에 대해 자세히 알아보기
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!웹사이트 선택
번역된 콘텐츠를 보고 지역별 이벤트와 혜택을 살펴보려면 웹사이트를 선택하십시오. 현재 계신 지역에 따라 다음 웹사이트를 권장합니다:
또한 다음 목록에서 웹사이트를 선택하실 수도 있습니다.
사이트 성능 최적화 방법
최고의 사이트 성능을 위해 중국 사이트(중국어 또는 영어)를 선택하십시오. 현재 계신 지역에서는 다른 국가의 MathWorks 사이트 방문이 최적화되지 않았습니다.
미주
- América Latina (Español)
- Canada (English)
- United States (English)
유럽
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
