Any way to free hand crop an image?

Is there any way to free hand crop on an image? I have an organic shape I want to quickly crop not using the rectangular box that matlab has for cropping. Any way to go about this?

 Risposta accettata

How do you propose cropping a non-rectangular shape? Do you want to zero out parts that aren't within in your "cropped" region. An image always has to be rectangular.
doc imcrop
doc impoly
doc imfreehand
If you want to zero out the parts not in the free hand reason. use the createMask method of the freehand region to build a mask of the free hand region. Then negate it and zero everything in it out.
I = imread('cameraman.tif');
imshow(I);
h = imfreehand; %draw something
M = ~h.createMask();
I(M) = 0;
imshow(I);

8 Commenti

Perfect. Thank you.
thank you so much
Hi,
I have an image which I want to crop using the freehand(). Please find the image below:fh1.jpg
The boundary is defined in the image above. I tried using the code above to crop the image and display it in a separate window. But I am getting a different result. Any suggestions would be appreciated.
fh3.jpg
Use max and min() to get the limiting coordinates from imfreehand. Then just crop using those indexes:
row1 = min(y);
row2 = max(y);
col1 = min(x);
col2 = max(x);
maskedImage = rgbImage(row1:row2, col1:col2, :);
Hi,
I tried the above code but I am getting the following error:
Unrecognized method, property, or field 'getPosition' for class 'images.roi.Freehand'.
Error in freehand_masking_demo (line 49)
xy = hFH.getPosition;
Try the drawfreehand function instead then.
Hi,
I found tha MATLAB does not recommend getPosition method. They recommend to use ROI classes. I tried using the ROI.FREEHAND class but I get the following error:
Unrecognized method, property, or field 'roi' for class 'images.roi.Freehand'.
Error in fh4 (line 6)
pos = images.roi.Freehand(); % get position for that ROI
thank you

Accedi per commentare.

Più risposte (0)

Community Treasure Hunt

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

Start Hunting!

Translated by