How to obtain homography for metric measurements from an image

56 Views Asked by At

I have an image of a post-it with a line mark and I'd like to calculate the horionzal position of the line in real world units (cm). The size of the image is 3456 x 3456 px and the post-it dimension is 9 x 9 cm

Input image

I manually picked the 4 pixel coordinates of the post-it corners [[1769, 353], [3199, 835], [2683, 2145], [983,1409]] and then I tried to estimate the homography matrix using OpenCV getPerspectiveTransform(src, dst). This is the snippet:

import cv2 as cv
import numpy as np

src_bbox = [[0,0], [9, 0], [9, 9], [0, 9]] # [cm]
dst_bbox = [[1769, 353], [3199, 835], [2683, 2145], [983,1409]] # [px]

H = cv.getPerspectiveTransform(np.array(src_bbox).astype(np.float32),
                               np.array(dst_bbox).astype(np.float32))

H_inv = np.linalg.inv(H)

image = cv.imread("sheet.jpg")
if image is None:
    exit("Could not read the image.")

mod = cv.warpPerspective(image, H_inv, image.shape[:2])

cv.namedWindow("sheet", cv.WINDOW_NORMAL)
cv.resizeWindow("sheet", 400, 400)

cv.namedWindow("sheet_warped", cv.WINDOW_NORMAL)
cv.resizeWindow("sheet_warped", 400, 400)

cv.imshow("sheet", image)
cv.imshow("sheet_warped", mod)
cv.waitKey(0)
cv.destroyAllWindows()

This is the output:

Output images

The rectified image (right) is there but is so small that you cannot see it so I cannot use it to detect the line mark and obtain its position in cm.

Is it just a scaling issue due to the fact that the correspondence points are expressed in different units (px vs cm) and do not have similar magnitude or I am doing something wrong? How should I select a scaling factor anyway?

0

There are 0 best solutions below