当前位置:   article > 正文

透视变换--基于getPerspectiveTransform()及像素赋值(未涉及插值)_getperspectivetransform()源码

getperspectivetransform()源码

好久没写博客了,因为白天要去实习,晚上看论文实在抽不出时间,由于项目需要,研究了透视变换,在网上找了一圈不是缺腿就是少胳膊的,后来对缺省的代码进行补充使其可以调通,现贴出来供大家学习使用,代码如下:


 
 
  1. #include<iostream>
  2. #include<opencv2/highgui/highgui.hpp>
  3. #include<opencv2/imgproc/imgproc.hpp>
  4. using namespace cv;
  5. using namespace std;
  6. int main( )
  7. {
  8. Mat img=imread( "1.jpg");
  9. int img_height = img.rows;
  10. int img_width = img.cols;
  11. vector<Point2f> corners( 4);
  12. corners[ 0] = Point2f( 0, 0);
  13. corners[ 1] = Point2f(img_width -1, 0);
  14. corners[ 2] = Point2f( 0,img_height -1);
  15. corners[ 3] = Point2f(img_width -1,img_height -1);
  16. vector<Point2f> corners_trans( 4);
  17. corners_trans[ 0] = Point2f( 150, 250);
  18. corners_trans[ 1] = Point2f( 771, 0);
  19. corners_trans[ 2] = Point2f( 0,img_height -1);
  20. corners_trans[ 3] = Point2f( 650,img_height -1);
  21. Mat transform = getPerspectiveTransform(corners,corners_trans);
  22. cout<<transform<< endl;
  23. vector<Point2f> ponits, points_trans;
  24. for( int i= 0;i<img_height;i++){
  25. for( int j= 0;j<img_width;j++){
  26. ponits.push_back(Point2f(j,i));
  27. }
  28. }
  29. perspectiveTransform( ponits, points_trans, transform);
  30. Mat img_trans = Mat::zeros(img_height,img_width,CV_8UC3);
  31. int count = 0;
  32. for( int i= 0;i<img_height;i++){
  33. uchar* p = img.ptr<uchar>(i);
  34. for( int j= 0;j<img_width;j++){
  35. int y = points_trans[count].y;
  36. int x = points_trans[count].x;
  37. uchar* t = img_trans.ptr<uchar>(y);
  38. t[x* 3] = p[j* 3];
  39. t[x* 3+ 1] = p[j* 3+ 1];
  40. t[x* 3+ 2] = p[j* 3+ 2];
  41. count++;
  42. }
  43. }
  44. imwrite( "1_trans.jpg",img_trans);
  45. namedWindow( "原图");
  46. imshow( "原图", img);
  47. namedWindow( "透视变换图");
  48. imshow( "透视变换图", img_trans);
  49. waitKey( 0);
  50. return 0;
  51. }
  • 1
原图:


透视变换之后的效果图:


转载自:https://blog.csdn.net/llx1026/article/details/78289247

声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号