How to stop the while loop when using imrect() draw many rectangles in an image?

조회 수: 2 (최근 30일)
I use the while loop to enable me extract many rectangle areas of an image by imrect function, and I want to stop the loop when I input the "Enter" from the keyboard. The thing is it won't stop the drawing immediately after I enter the button but until the next draw. How should I avoid this??? Thanks so much! The code is below:
function [ImgPatch_Cluster,ShowMask] = seletpatch()
%%%this function is to let users crop any image patches
temp=imread('./train images/1.png');
OrigImg=rgb2gray(temp);
fprintf('Start to crop the image patches from the whole image.\n');
imgSz = size(OrigImg);
PatchCount = 0;
ImgPatch_Cluster = cell(1,1);
FLAG = 1;
ShowMask = uint8(mat2gray(OrigImg)*255);
global KEY_IS_PRESSED
KEY_IS_PRESSED = 0;
figure('KeyPressFcn', @myKeyPressFcn);
imshow(OrigImg,'Border','tight');
fprintf('If you want to stop selecting on the current image.\n');
fprintf('Press "Enter" button, then drag any size rectangle to finish.\n');
while KEY_IS_PRESSED == 0
% set(gcf, 'KeyPressFcn', @myKeyPressFcn);
if KEY_IS_PRESSED==1
break;
end
H = imrect;
if KEY_IS_PRESSED==1
fprintf('Finish selecting on the current image.\n Drag any size rectangle to finish.\n');
break;
else
position = wait(H);
setColor(H,[1 0 0]);
xmin = round(position(1));
ymin = round(position(2));
width = position(3);
height = position(4);
xmax = xmin+width-1;
ymax = ymin+height-1;
if xmin<=0
xmin = 1;
end
if ymin<=0
ymin = 1;
end
if xmax>=imgSz(2)
xmax = imgSz(2)-1;
end
if ymax>=imgSz(1)
ymax = imgSz(1)-1;
end
new_width = xmax-xmin+1;
new_height = ymax-ymin+1;
if mod(new_width,2)==0
new_width = new_width+1;
end
if mod(new_height,2)==0
new_height = new_height+1;
end
ImgPatch = zeros(new_height,new_width);
ImgPatch(1:new_height,1:new_width) = ...
OrigImg(ymin:ymin+new_height-1,xmin:xmin+new_width-1);
BinaryMask = zeros(imgSz(1),imgSz(2));
BinaryMask(ymin:ymin+new_height-1,xmin:xmin+new_width-1) = 1;
BinaryContour = bwmorph(BinaryMask,'remove');
BinaryContour = imdilate(BinaryContour,ones(5));
ShowMask(BinaryContour) = 255;
% contour(BinaryMask*2-1,[0 0],'r');
% api = iptgetapi(H);
% position = api.getPosition();
PatchCount = PatchCount+1;
ImgPatch_Cluster{1,PatchCount} = ImgPatch;
end
end
hold off;
close;
function myKeyPressFcn(hObject, event)
global KEY_IS_PRESSED
if strcmp(event.Key,'return')
KEY_IS_PRESSED = 1;
end

답변 (1개)

Image Analyst
Image Analyst 2013년 3월 7일
Why not just put code like this at the end of the loop:
message = sprintf('Do you want to continue saving rectangular regions?');
reply = questdlg(message, 'Continue with Loop?', 'OK','Cancel', 'OK');
if strcmpi(reply, 'Cancel')
% User canceled so exit.
break;
end
  댓글 수: 2
Han Li
Han Li 2013년 3월 8일
Thanks for your advice. It sounds great, but generally, the user has to input around 10 rectangles at the same time. It will be a little noisy though since it
Image Analyst
Image Analyst 2013년 3월 8일
So? If you just go as fast as you can until the user hits a key then how do you have any control at all over how many rectangles are created? It would be almost random. Why is my method a little noisy?

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Convert Image Type에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by