How to convert pixel cordinates to spatial cordinates.??

I have pixel cordinates of a rectangle but imcrop require spatial coordinates of rectangle to crop...Please tell me how to switch between the two coordinate systems?

 Accepted Answer

imcrop() uses pixel coordinates. What do you have?

4 Comments

IMCROP Crop image. I = IMCROP creates an interactive image cropping tool, associated with the image displayed in the current figure, called the target image. The tool is a moveable, resizable rectangle that is interactively placed and manipulated using the mouse. After positioning the tool, the user crops the target image by either double clicking on the tool or choosing 'Crop Image' from the tool's context menu. The cropped image, I, is returned. The cropping tool can be deleted by pressing backspace, escape, or delete, or via the 'Cancel' option from the context menu. If the tool is deleted, all return values are set to empty.
I2 = IMCROP(I) displays the image I in a figure window and creates a
cropping tool associated with that image. I can be a grayscale image,
an RGB image, or a logical array. The cropped image returned, I2, is
of the same type as I.
X2 = IMCROP(X,MAP) displays the indexed image [X,MAP] in a figure
window and creates a cropping tool associated with that image.
I = IMCROP(H) creates a cropping tool associated with the image
specified by handle H. H may be an image, axes, uipanel, or figure
handle. If H is an axes, uipanel, or figure handle, the cropping tool
acts on the first image found in the container object.
The cropping tool blocks the MATLAB command line until the operation is
completed.
You can also specify the cropping rectangle non-interactively, using
these syntaxes:
I2 = IMCROP(I,RECT)
X2 = IMCROP(X,MAP,RECT)
RECT is a 4-element vector with the form [XMIN YMIN WIDTH HEIGHT];
these values are specified in spatial coordinates.
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,...)
[I2 RECT] = IMCROP() returns the cropping rectangle in addition to the
cropped image.
[X,Y,I2,RECT] = IMCROP() additionally returns the XData and YData of
the target image.
Remarks
-------
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.
this is from the documentation of imcrop...it requires spatial cordinates m not getting exact segmented object as seen by rectangle drawn by bounding box...
True, but it's not so intuitive. The pixel coordinates returned from imcrop() are not integer coordinates, and the width and height go from pixel center to pixel center - they are not the number of pixels wide or high. See this code:
m = magic(7) % Sample array.
mc1 = imcrop(m, [2 3 4 5]) % Get 5 by 6 array, not 4 by 5 array.
uiwait(msgbox('Drag out a very small box. Double click the center of it to finish'));
imshow('cameraman.tif');
[x, y, croppedImage, rect] = imcrop()
size(croppedImage)
sania, pixel locations ARE a spatial coordinates. What do you think they are?
https://www.mathworks.com/help/vision/gs/coordinate-systems.html
https://www.mathworks.com/help/images/image-coordinate-systems.html#brcu_cr

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!