Reputation: 51
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.
Upvotes: 1
Views: 1947
Reputation: 18705
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:
cv::findHomography
;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.
Upvotes: 1