How to preserve spatial reference using Imcrop with Matlab - matlab

I have an image and the spatial reference object of that image.
Now i want to crop the image by coordinates according to the spatial reference object.
The function Imcrop can only crop according the pixel coordinates. Is there a way to crop based on the world coordinates?
I tried to use Imcrop and compute for the new reference object but I get lost in the coordinate transformation.
An example of the reference object after warping an Image.
imref2d with properties:
XWorldLimits: [-775.4357 555.5643]
YWorldLimits: [-488.3694 523.6306]
ImageSize: [1012 1331]
PixelExtentInWorldX: 1
PixelExtentInWorldY: 1
ImageExtentInWorldX: 1331
ImageExtentInWorldY: 1012
XIntrinsicLimits: [0.5000 1.3315e+03]
YIntrinsicLimits: [0.5000 1.0125e+03]
What I actually want to do is to crop the image such that the point (0,0) is the center of the cropped image.

According to you spatial reference each pixel has a dimensions of 1 x 1 in world coordinates. Therefore if you want to convert between world coordinate (Xw,Yw) and image coordinate (Xi,Yi) do the following:
Xi = round(abs(-775.4357 - Xw))
Yi = round(abs(-488.3694 - Yw))
So if you want to crop the image such that the real world coordinate (0,0) will be the center of the new cropped image and the size of the new image will be width on height than the rectangle for imcrop will be
[(755 - width) (488 - height) width height]

Related

Geometrical transformation of a polygon to a higher resolution image

I'm trying to resize and reposition a ROI (region of interest) correctly from a low resolution image (256x256) to a higher resolution image (512x512). It should also be mentioned that the two images cover different field of view - the low and high resolution image have 330mm x 330mm and 180mm x 180mm FoV, respectively.
What I've got at my disposal are:
Physical reference point (in mm) in the 256x256 and 512x512 image, which are refpoint_lowres=(-164.424,-194.462) and refpoint_highres=(-94.3052,-110.923). The reference points are located in the top left pixel (1,1) in their respective images.
Pixel coordinates of the ROI in the 256x256 image (named pxX and pxY). These coordinates are positioned relative to the reference point of the lower resolution image, refpoint_lowres=(-164.424,-194.462).
Pixel spacing for the 256x256 and 512x512 image, which are 0.7757 pixel/mm and 2.8444 pixel/mm respectively.
How can I rescale and reposition the ROI (the binary mask) to correct pixel location in the 512x512 image? Many thanks in advance!!
Attempt
% This gives correctly placed and scaled binary array in the 256x256 image
mask_lowres = double(poly2mask(pxX, pxY, 256., 256.));
% Compute translational shift in pixel
mmShift = refpoint_lowres - refpoint_highres;
pxShift = abs(mmShift./pixspacing_highres)
% This produces a binary array that is only positioned correctly in the
% 512x512 image, but it is not upscaled correctly...(?)
mask_highres = double(poly2mask(pxX + pxShift(1), pxY + pxShift(2), 512.,
512.));
So you have coordinates pxX, and pxY in pixels with respect to the low-resolution image. You can transform these coordinates to real-world coordinates:
pxX_rw = pxX / 0.7757 - 164.424;
pxY_rw = pxY / 0.7757 - 194.462;
Next you can transform these coordinates to high-res coordinates:
pxX_hr = (pxX_rw - 94.3052) * 2.8444;
pxY_hr = (pxY_rw - 110.923) * 2.8444;
Since the original coordinates fit in the low-res image, but the high-res image is smaller (in physical coordinates) than the low-res one, it is possible that these new coordinates do not fit in the high-res image. If this is the case, cropping the polygon is a non-trivial exercise, it cannot be done by simply moving the vertices to be inside the field of view. MATLAB R2017b introduces the polyshape object type, which you can intersect:
bbox = polyshape([0 0 180 180] - 94.3052, [180 0 0 180] - 110.923);
poly = polyshape(pxX_rw, pxY_rw);
poly = intersect([poly bbox]);
pxX_rw = poly.Vertices(:,1);
pxY_rw = poly.Vertices(:,2);
If you have an earlier version of MATLAB, maybe the easiest solution is to make the field of view larger to draw the polygon, then crop the resulting image to the right size. But this does require some proper calculation to get it right.

How to crop face section from an image with given corner points. MATLAB

I want to crop a face section from an image but face image is not straight/vertically aligned. I am having four pixel points to crop it..
Problem is that,
If i will transform image first the pixel points cannot be used thereafter to crop the facial section out of it.
Or in other case I am not having an exact bounding box to crop the image directly using imcrop as facial sections are somewhat tilted left or right.
The four pixel points are at forehead , chin and ears of the face to be cropped.
You should look at poly2mask. This function produces a mask image from your given x and y coordinates:
BW = poly2mask(x,y,m,n);
where x and y are your coordinates, and the produced BW image is m by n. You can then use this BW image to mask your original image I by doing
I(~BW) = 0;
If you actually want to crop, then you could get the bounding box (either through the regionprops function or the code below):
x1 = round(min(x));
y1 = round(min(y));
x2 = round(max(x));
y2 = round(max(y));
and then crop the image after you have used the BW as a mask.
I2 = I(x1:x2,y1:y2);
Hope that helps.

Matlab: Extracting a ROI with center coordinates

I have a mammographic image of size 1024 x 1024, and I have the center coordinates of the anomaly (338.314) and the radius (56) in pixels of the circle containing the anomaly. I desire to extract a region of interest of size 128 * 128 including the anomaly. I tried with
rect = [338-64,314-64,127,127];
crop = imcrop (img, rect) ;
but I obtien an ROI that does not contain the desired anomaly.
any suggestions please.
MATLAB's matrix indices are in (row,column) format, while rectangle's indices are usually in (x,y) format.
This means that you probably need to swap the two first elements of rectangle.
rect = [314-64,338-64,127,127];
crop = imcrop (img, rect) ;

How to change a pixel distance to meters?

I have a .bmp image with a map. What i know:
Height an Width of bmp image
dpi
Map Scale
Image Center's coordinates in meters.
What i want:
How can i calculate some points of image (for example corners) in meters.
Or how can i change a pixel distanse to meters?
What i do before:
For sure i know image center coordinates in pixels:
CenterXpix = Widht/2;
CenterYpix = Height/2;
But what i gonna do to find another corners coordinates. Don't think that:
metersDistance = pixelDistance*Scale;
is a correct equation.
Any advises?
If you know the height or width in both meters and pixels, you can calculate the scale in meters/pixel. You equation:
metersDistance = pixelDistance*Scale;
is correct, but only if your points are on the same axis. If your two points are diagonal from each other, you have to use good old pythagoras (in pseudocode):
X = XdistancePix*scale;
Y = YdistancePix*scale;
Distance_in_m = sqrt(X*X+Y*Y);

Get a relationship between position vector return by getposition method in matlab

I wrote a code for image cropping. I used imrect to draw a rectangle on the image and then get the position of it by using the method getposition. I wrote a function which uses image pixel coordinates for cropping operation. How can I create a relationship between values return by getposition method and image pixel coordinates.My code for cropping is as follows,
[rnum cnum dim]=size(img);
for h=1:dim
for i=1:width
for j=1:height
negative(i,j,h)=img(xmin+i,ymin+j,h);
end
end
end
width,height,xmin,ymin have to found from getposition method
Like you said, imrect's getPosition method will return:
[xmin ymin width height] = getPosition( h );
The first two values are the top-left corner of the rectangle, and the next two values are the length of the sides of the rectangle. These should all be in pixel coordinates if you are using imrect.
To crop an image based on these position values, you will start at the top-left corner of (xmin, ymin) and go to the bottom-right corner at (xmin+width-1, ymin+height-1).
You should not use for loops to get the pixel data, you can take advantage of MATLAB's vectorization characteristics and do the following:
CroppedImageMatrix = OriginalImageMatrix( [ymin : 1 : ymin+height-1],
[xmin : 1 : xmin+width-1],
: );
This will immediately "crop" the image and place the cropped data into the new matrix. You can do this because you are using a rectangular crop and all of the indices correspond to create a rectangular lattice of points. It would be "trickier" if this was not a rectangular crop.
This also will work the same for color or grayscale images because you do not need to index the channel dimension, you just take the values from every available channel.
P.S. - Documentation page for imrect: http://www.mathworks.com/help/images/ref/imrect.html