Convert map coordinates in pixels on an image knowing geo bounding box - coordinates

I am having trouble trying to draw a pixel on an image.
I know the geo bounding box values minX, minY, maxX, maxY of the image. I know the image sizes.
Example:
Bounding box:
26.078158345222278,
44.48150993466972,
26.079370252344233,
44.48398544389815
Image size --> width:155, height:299
I need to draw on the image this point:
26.07896575,
44.484298499999994
Can you please help?

Related

Can turfjs provide a coordinate transform between geo coords and application coords

I need a transform from geo coord system to/from another coord system.
I'd think the obvious way to do this would be to give the two bounding boxes of the systems.
So if I had a geo bbox, in lon/lat coords, and a non-geo bbox that corresponds to that but in my own coordinates, I'd like a xfm that could convert geo-to-me and me-to-geo.
I need this for an agent based programming environment that has its own coord system based on minX, minY, maxX, maxY, just as Turf expresses bboxes. It would also work for transforming between geo coords and pixels in a canvas, for example.
I couldn't find such a coord transform in Turf but I may be missing it, or there may be an easy way to do it with Turf primitives.
Is there a way to use Turf for such a coord transform?
I'd use proj4js which handles projection conversions.
Your canvas can be represented in Pseudo-Mercator ([x,y]) while turf will continue working in WGS84 ([lon, lat]).
const xyCoordsToLatLong = (xy_pair) => {
return proj4(PSEUDO_MERC_DATUM, WGS84_DATUM, xy_pair);
}
const latLongCoordsToXY = (latlong_pair) => {
return proj4(WGS84_DATUM, PSEUDO_MERC_DATUM, latlong_pair);
}
Then, a canvas of bbox [0,0,500,200] would be represented as [0, 0, 0.004491576420597608, 0.0017966305679443192].
Full demo here.

How to preserve spatial reference using Imcrop with 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]

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);

CGRectApplyAffineTransform Incorrect Result?

I have a bit of code that is trying to figure out the correct bounding box for a rectangle that is being rotated -5 degrees:
CGRect rectangleToRotate = CGRectMake(0,0,104.949, 131.017);
CGAffineTransform rotateTransform = CGAffineTransformMakeRotation((M_PI * -5) / 180);
CGRectApplyAffineTransform (rectangleToRotate, rotateTransform);
The result that is produced is a rectangle with size: {115.968, 139.664} which, for some reason is too huge and doesn't tightly encompass the rectangle. Yet when I go take this same scenario in Adobe Illustrator, the bounding box is: {104.659, 132.143} which is correct.
What exactly am I missing here?
I guess you are doing something wrong in illustrator. How should the bounding box be smaller (104.9 to 104.6) AFTER a 5 degree rotation.. thats just wrong.
The CGRect results are quite right. I tested it in Photoshop.
A 105x131px rect will result in a 117x141px bounding box after a -5 degree rotation.