简体   繁体   中英

Calculating the x & y coordinates in cm from the pixelcoordinates

Heyho!
I've got an image of a flat surface (taken from above and with an angle of 30°) and I want the x & y coordinates of a specific pixel but in cm (with the coordinate system being in the surface).
Is there a method to archive this?
I'm using python but any help is appreciated! :)

Edit: I tried the homographie method from below however I didn't quiet manage to make it work. Her is what I did:

#two sets of points I used 
#src: corners + center of my 400px*460px image 
#dst: coordinate system being outside the image 
src = np.matrix(((1, 1, 1),(400, 1, 1),(1, 460, 1),(400, 460, 1),(200,230,1)))
dst= np.matrix(((31.6, 7, 1),(14.5, 7, 1),(28.4, 26.3, 1),(17, 26.3, 1),(22.6,18.6,1 )))

#src: random points from the image of my 400px*460px image 
#dst: coordinate system being in the actual image
src = np.matrix(((1, 1, 1),(400, 460, 1),(200,1,1), (100, 170, 1), (280, 320, 1),(300, 50, 1)))
dst= np.matrix(((0, 0, 1),(14.6, 19.3, 1),(17.1/2,0,1), (5.0, 9.2, 1), (11.65, 15.3, 1), (12.9, 2.9, 1) ))

H = cv2.findHomography(src,dst,0)[0]
print (H)    
for c in range(0,5):
    x= c*100
    y = 1
    print(x,y,np.dot(H,np.array((x,y,1))))  

Actual Photo of the setup
The square is the area visible on the (400px*460px) picture. The Camera is located in the black box on the right. The X & Y are my Pixelcoordinates.
Results with both sets of numbers are good as long as you stay on the x-axis. As soon as I move down the y-axis the numbers go wrong.

One approach is to estimate the homography between the plane of your flat surface and your image plane. A simple way to estimate the homography:

  1. get the coordinates in cm of four (non collinear) points on your flat surface;
  2. find the same four point in the image and get their coordinates in pixel;
  3. estimate the homography with cv::findHomography ;
  4. find in the image your point of interest and get its coordinate in pixel;
  5. compute the coordinate of your point in cm using the homography.

For a worked out sample in C++ with OpenCV see my answer https://stackoverflow.com/a/36388035/15485

If you need more accuracy you need also to compensate for the distortion of the lens and since you say you have a raspberry pi camera maybe the distortion is not negligible... but it depends on the accuracy you require. The homography is not capable to compensate for the distortion.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM