6. Opencv实现透视变换

寵の児 提交于 2019-12-14 09:42:26

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()

  1. #include <opencv2\opencv.hpp>
  2. #include
  3. using namespace cv;
  4. using namespace std;
  5. int main()
  6. {
  7. Mat img = imread(“noobcvqr.png”);
  8. if (img.empty())
  9. {
  10.  cout << "请确认图像文件名称是否正确" << endl;
    
  11.  return -1;
    
  12. }
  13. Point2f src_points[4];
  14. Point2f dst_points[4];
  15. //通过Image Watch查看的二维码四个角点坐标
  16. src_points[0] = Point2f(94.0, 374.0);
  17. src_points[1] = Point2f(507.0, 380.0);
  18. src_points[2] = Point2f(1.0, 623.0);
  19. src_points[3] = Point2f(627.0, 627.0);
  20. //期望透视变换后二维码四个角点的坐标
  21. dst_points[0] = Point2f(0.0, 0.0);
  22. dst_points[1] = Point2f(627.0, 0.0);
  23. dst_points[2] = Point2f(0.0, 627.0);
  24. dst_points[3] = Point2f(627.0, 627.0);
  25. Mat rotation, img_warp;
  26. rotation = getPerspectiveTransform(src_points, dst_points); //计算透视变换矩阵
  27. warpPerspective(img, img_warp, rotation, img.size()); //透视变换投影
  28. imshow(“img”, img);
  29. imshow(“img_warp”, img_warp);
  30. waitKey(0);
  31. return 0;
  32. }
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!