Q:

Cropping multiple parts from the single image in Matlab

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

A:

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.

A:

How about just pressing Esc when you want to quit?
img = imread('peppers.png');

k = 1;
f = figure;
imshow(img)
while true
    title('Double click to select next area, press Esc when finished')
    [tt, bb] = imcrop(f);
    if isempty(tt)
        close(f)
        break
    end
    crop.img{k,:} = tt;
    crop.bb{k,:} = bb;
    k = k+1;
end

