imcrop won't work

18 views (last 30 days)
jack
jack on 20 Jan 2016
Edited: Thorsten on 20 Jan 2016
Hi guys,
I have the following problem:
I have a binary image and i have tried to imcrop the image by rect which is [1029,535,1,1] but the output of imcrop is empty.
Why is that and is there a work around so that it can be used for matrixes of 3-D, 4-D or n-D?
Thanks in advance.
  1 Comment
Guillaume
Guillaume on 20 Jan 2016
What is the size of your image?

Sign in to comment.

Answers (2)

Thorsten
Thorsten on 20 Jan 2016
Edited: Thorsten on 20 Jan 2016
I = rand(1000, 2000);
rect = [1029,535,1,1];
Ic = imcrop(I, rect)
The thing to remember is that the rect is given by [xmin, ymin, width, height], so x comes first, but the matrix is indexed as row, col, so y/row comes first...
Note that imcrop basically does nothing else than
Ic = I(ymin:ymin+height, xmin:xmin+width);

Image Analyst
Image Analyst on 20 Jan 2016
Edited: Image Analyst on 20 Jan 2016
Jack, the array is [left, top, width, height]. You have [1029,535,1,1] which means you're probably starting out with the upper left corner way down in the bottom right corner of your image (which may or may not be fine). It's fine if your image is a lot bigger than 535 rows by 1029 columns. But the main problem is that your width and height are 1 so you're getting only a single pixel. And that one pixel may be black for all I know.
Also pay attention to what Thorsten said - mixing up rows,columns with x,y is an extremely common beginner mistake. They're reverse order of each other.
If you want to crop out a 535 row by 1029 column chunk of the upper left corner of the image, you want [1, 1, 1029, 535]
imcrop() works for 2D images. It works for 3D if the image is an RGB image but not if it's a volumetric image. For higher dimensions, use indexing like
croppedArray = array(i1low:i1high, i2low:i2high, i3low:i3high, i4low:i4high); % etc.
  1 Comment
Thorsten
Thorsten on 20 Jan 2016
Edited: Thorsten on 20 Jan 2016
Small correction: you get a 2x2 matrix for a rect [xmin xmax 1 1], not a single pixel.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!