Homography是一个3*3的变换矩阵,将一张图中的点映射到另一张图中对应的点
retval, mask = cv.findHomography( srcPoints, dstPoints[, method[, ransacReprojThreshold[, mask[, maxIters[, confidence]]]]] )
参数 | 描述 |
---|---|
srcPoints | 原始点 |
dstPoints | 目标点 |
retval | 变换矩阵 |
mask | Optional output mask set by a robust method ( RANSAC or LMEDS ). Note that the input mask values are ignored. |
参考文献:
https://blog.csdn.net/liuphahaha/article/details/50719275
https://www.learnopencv.com/homography-examples-using-opencv-python-c/
https://docs.opencv.org/master/d9/d0c/group__calib3d.html#ga4abc2ece9fab9398f2e560d53c8c9780
import cv2 as cv
import numpy as np
im_src = cv.imread("book1.jpg") # 读取原图片
pts_src = np.array([[318, 256],[534, 372],[316, 670],[73, 473]], dtype=float) # 原始图书的四个角的坐标
height, width = 400, 300 # 修改图的大小
im_dst = np.zeros((height, width, 3), dtype=np.uint8) # 目标图片
pts_dst = np.empty((0, 2)) # 目标图片四个角的坐标
pts_dst = np.append(pts_dst, [(0, 0)], axis=0) # 左上角
pts_dst = np.append(pts_dst, [(width - 1, 0)], axis=0) # 右上角
pts_dst = np.append(pts_dst, [(width - 1, height - 1)], axis=0) # 右下角
pts_dst = np.append(pts_dst, [(0, height - 1)], axis=0) # 左下角
h, status = cv.findHomography(pts_src, pts_dst) # 计算转换矩阵
im_out = cv.warpPerspective(im_src, h, (width,height)) # 转换图片
cv.imshow("1", im_src)
cv.imshow("2", im_out)
cv.waitKey(0)
原图片
转换后的图片
参考文献:
https://docs.opencv.org/master/da/d54/group__imgproc__transform.html#gaf73673a7e8e18ec6963e3774e6a94b87
https://www.learnopencv.com/homography-examples-using-opencv-python-c/