how do i make the imcrop function accurate to the pixel?

2 views (last 30 days)
hi,
there is a tip in the help browser imcrop section which says:
"Because rect is specified in terms of spatial coordinates, the width and height elements of rect do not always correspond exactly with the size of the output image. For example, suppose rect is [20 20 40 30], using the default spatial coordinate system. The upper-left corner of the specified rectangle is the center of the pixel (20,20) and the lower-right corner is the center of the pixel (50,60). The resulting output image is 31-by-41, not 30-by-40, because the output image includes all pixels in the input image that are completely or partially enclosed by the rectangle."
is there a way to overcome this accuracy issue and get pixel precise output?
thanks a lot,

Accepted Answer

Image Analyst
Image Analyst on 26 Aug 2012
Subtract 1 from the width and height. I agree that "center-to-center" pixel measurements can be confusing when you would really like "whole" pixels. For example, how long is this stretch of 1 pixels:
0 0 0 0 0
0 1 1 1 0
0 0 0 0 0
Is it 3 long, or 2 long? It depends on if you're considering whole pixels or going from center to center, respectively.
And they aren't always consistent on how they do it (just look at bwarea vs regionprops). I'll bring it up with them. (I've already discussed with them the annoyances we sometimes face with functions dealing with 0-1 instead of gray levels.)
  3 Comments
Image Analyst
Image Analyst on 5 Jan 2014
Edited: Image Analyst on 5 Jan 2014
No. They don't always act on my suggestions, and when they do they don't let me know in advance and I find out afterwards, like when they review my list and say "Oh, we implemented that 2 versions ago." But is my answer good enough for you to mark it as "Accepted"?
DGM
DGM on 13 Nov 2022
For what it's worth, it's easier to accept that it's unintuitive if you consider that imcrop() is not limited to working in image (pixel) coordinates. It's set up to perform the selection in an arbitrary coordinate space.
To use a non-default spatial coordinate system for the target image,
precede the other input arguments with two 2-element vectors specifying
the XData and YData:
[...] = imcrop(X,Y,...)
Also from the synopsis:
Because RECT is specified in terms of spatial coordinates, the WIDTH
and HEIGHT of RECT do not always correspond exactly with the size of
the output image. For example, suppose RECT is [20 20 40 30], using the
default spatial coordinate system. The upper left corner of the
specified rectangle is the center of the pixel (20,20) and the lower
right corner is the center of the pixel (50,60). The resulting output
image is 31-by-41, not 30-by-40, because the output image includes all
pixels in the input that are completely or partially enclosed by the
rectangle.
I've thought before about writing a size predictor or parameter selection aid for imcrop(), but I don't know who would actually need it. If you want to select an image region in pixel coordinates with utter certainty that you get what you think you asked for, just use direct array indexing instead of imcrop(). At least that way you can have the opportunity to properly handle edge collisions without just getting an empty array in return.

Sign in to comment.

More Answers (0)

Tags

Community Treasure Hunt

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

Start Hunting!