MATLAB: Crop with imfreehand

cropImage Processing Toolboximfreehand

I dont know if my question has an answer.I have an image and crop a ROI with imcrop
[cropped_image,rect]=imcrop(original_image);
I work with cicular regions so this rectangular is not working very well.I want to draw freehand the ROI i want..
I tried this but doesnt work.(I want to get similar image such as cropped_image)
cropped_image=imfreehand();
and then extract this cropped_image and continue to my analysis

Best Answer

I = imread('corn.tif',2);
imshow(I)
h = imfreehand; % Place a closed ROI by dragging and double-click on the region.
position = round(wait(h));
min_values = min(position,[],1);
max_values = max(position,[],1);
Inew = I(min_values(2):max_values(2),min_values(1):max_values(1),:);
[X1,X2] = meshgrid(1:size(Inew,2),1:size(Inew,1));
in = inpolygon(X1,X2,position(:,1)-min_values(1)+1,position(:,2)-min_values(2)+1);
in =repmat(in,1,1,size(I,3));
Inew(~in) = 0; % If I is uint
Now Inew is the rectangular cropped image. Out of ROI has been blacked out.