简体   繁体   English

从像素坐标计算 x & y 坐标(以 cm 为单位)

[英]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).我有一个平面的图像(从上方拍摄,角度为 30°),我想要特定像素的 x 和 y 坐标,但以厘米为单位(坐标系在表面中)。
Is there a method to archive this?有没有办法存档这个?
I'm using python but any help is appreciated!我正在使用 python 但任何帮助表示赞赏! :) :)

Edit: I tried the homographie method from below however I didn't quiet manage to make it work.编辑:我从下面尝试了 homographie 方法,但是我并没有安静地设法使它起作用。 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.正方形是(400px*460px)图片上可见的区域。 The Camera is located in the black box on the right.相机位于右侧的黑框中。 The X & Y are my Pixelcoordinates. X & Y 是我的像素坐标。
Results with both sets of numbers are good as long as you stay on the x-axis.只要您保持在 x 轴上,两组数字的结果都很好。 As soon as I move down the y-axis the numbers go wrong.一旦我沿着 y 轴向下移动,数字就会出错。

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 ;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.使用单应性计算您点的坐标(以 cm 为单位)。

For a worked out sample in C++ with OpenCV see my answer https://stackoverflow.com/a/36388035/15485对于使用 OpenCV 用 C++ 编写的示例,请参阅我的答案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.单应性不能补偿失真。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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