Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cropping multiple parts from the single image in Matlab

Tags:

image

matlab

crop

I want to crop multiple parts from the image to make groudtruth using mouse pointer. There are different number of objects in each so I can't run for loop for specific number of times. However, I can use while loop. But how can I stop that?

for i=1:10

    [tt bb]=imcrop(img);
    crop.img{i,:}=tt;
    crop.bb(i,:)=bb;
end
like image 395
Aadnan Farooq A Avatar asked Jan 02 '23 00:01

Aadnan Farooq A


1 Answers

You can use an if condition with waitforbuttonpress for this. When you're done cropping an image portion and want to crop another portion, click any mouse button and the loop will continue. When you don't want to crop any other portion, press any keyboard button and the loop will break.

Demo Code:

img = imread('peppers.png');
f = figure;
k = 1;
while 1
    [tt, bb] = imcrop(img);
    crop.img{k,:} = tt;
    crop.bb{k,:} = bb;
    if waitforbuttonpress
        break;
    end
    k = k+1;
end

Note that it requires your figure window to be in focus.

like image 51
Sardar Usama Avatar answered Jan 04 '23 22:01

Sardar Usama