import cv2
import numpy as np
img = cv2.imread(‘D:\pycharm project\cameraCalibration\chang.jpg’)
H_rows, W_cols= img.shape[:2]
print(img.shape[0])
print(H_rows, W_cols)
原图中书本的四个角点(左上、右上、左下、右下),与变换后矩阵位置
pts1 = np.float32([[71, 617], [711, 411], [265, 1315], [1079, 963]])
pts2 = np.float32([[0, 0],[1080,0],[0, 1080],[1080, 1080]])
生成透视变换矩阵;进行透视变换
M = cv2.getPerspectiveTransform(pts1, pts2)
dst = cv2.warpPerspective(img, M, (1080,1080))
cv2.imshow(“original_img”,img)
cv2.imshow(“result”,dst)
cv2.waitKey(0)
cv2.destroyAllWindows()
- #include <opencv2\opencv.hpp>
- #include
- using namespace cv;
- using namespace std;
- int main()
- {
- Mat img = imread(“noobcvqr.png”);
- if (img.empty())
- {
-
cout << "请确认图像文件名称是否正确" << endl;
-
return -1;
- }
- Point2f src_points[4];
- Point2f dst_points[4];
- //通过Image Watch查看的二维码四个角点坐标
- src_points[0] = Point2f(94.0, 374.0);
- src_points[1] = Point2f(507.0, 380.0);
- src_points[2] = Point2f(1.0, 623.0);
- src_points[3] = Point2f(627.0, 627.0);
- //期望透视变换后二维码四个角点的坐标
- dst_points[0] = Point2f(0.0, 0.0);
- dst_points[1] = Point2f(627.0, 0.0);
- dst_points[2] = Point2f(0.0, 627.0);
- dst_points[3] = Point2f(627.0, 627.0);
- Mat rotation, img_warp;
- rotation = getPerspectiveTransform(src_points, dst_points); //计算透视变换矩阵
- warpPerspective(img, img_warp, rotation, img.size()); //透视变换投影
- imshow(“img”, img);
- imshow(“img_warp”, img_warp);
- waitKey(0);
- return 0;
- }
来源:CSDN
作者:weixin_42505877
链接:https://blog.csdn.net/weixin_42505877/article/details/103479576