get image pixel location using coordinates

1.6k Views Asked by At

I am working on a small project and have to draw the location of the indoor map. I have a mapping between some points on an image from pixel to coordinates (point, Latitude, Longitude):

  1. 55.9444578385393,-3.1866151839494705
  2. 55.94444244275808,-3.18672649562358860
  3. 55.94452336441765,-3.1866540759801865
  4. 55.94452261340533,-3.1867526471614838

Each of these points corresponds to the pixel location on an image, (x, y), where (0,0) is the top left corner:

  1. 1428.5, 552.5
  2. 1092.5, 537.5
  3. 1392.5, 168.5
  4. 1140.5, 96.5

P.S. 43.1 pixels map to 1 meter

Is there a quick way to calculate the pixel location given new coordinate, let us say, (latitude) 55.94445295695079, (longitude)-3.186666816473007 if I know that an image is 2060px(width) by 1109px(height)?

I was trying to the position of a pixel by creating a proportional relation between the coordinates and pixels, but that gave me non-sensible results.

An image of with points marked

2

There are 2 best solutions below

5
On BEST ANSWER

Take the abscissa/ordinate (in pixels) and the longitude/latitude of any three points (preferably far apart).

The transformation relations are affine:

$$x=a u+b v+c,\\y=d u+e v+f.$$

You have two independent systems of 3 equations in 3 unknowns, which you can solve for $a,b,c,d,e,f$.

You can obtain the inverse transform by solving the equations anew with the roles of $x,y$ and $u,v$ exchanged.

0
On

You can create a linear model for each coordinate, here's a little python script

import numpy as np

coords = [
    [55.9444578385393, -3.1866151839494705],
    [55.94444244275808, -3.18672649562358860],
    [55.94452336441765, -3.1866540759801865], 
    [55.94452261340533,-3.1867526471614838]]

pixels = [
    [1428.5, 552.5],
    [1092.5, 537.5],
    [1392.5, 168.5],
    [1140.5, 96.5]]

coords = np.array(coords)
pixels = np.array(pixels)

al, bl = np.polyfit(coords[ : , 0], pixels[ : , 0], 1)
ab, bb = np.polyfit(coords[ : , 1], pixels[ : , 1], 1)

This is an example

l, b = 55.944457, -3.1866151839
pl = al * l + bl
pb = ab * b + bb


print 'pixels({}, {}) = ({}, {})'.format(l, b, pl, pb)

with output

pixels(55.944457, -3.1866151839) = (1246.50869663, 444.441180377)