日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

getPerspectiveTransform函数

發布時間:2025/4/16 编程问答 11 豆豆
生活随笔 收集整理的這篇文章主要介紹了 getPerspectiveTransform函数 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

getPerspectiveTransform函數

函數作用:

根據輸入和輸出點獲得圖像透視變換的矩陣

函數的調用形式:

C++:?Mat?getPerspectiveTransform(InputArray?src, InputArray?dst)

InputArray?src, InputArray?dst:分別為變換的輸入和輸出點

分別四個點

透視變換后的圖像不是平行的,



函數的運用:

解變換公式的函數:

[cpp]?view plaincopy
  • Mat?getPerspectiveTransform(const?Point2f?src[],?const?Point2f?dst[])??
  • 輸入原始圖像和變換之后的圖像的對應4個點,便可以得到變換矩陣。之后用求解得到的矩陣輸入perspectiveTransform便可以對一組點進行變換:

    [cpp]?view plaincopy
  • void?perspectiveTransform(InputArray?src,?OutputArray?dst,?InputArray?m)??
  • 注意這里src和dst的輸入并不是圖像,而是圖像對應的坐標。應用前一篇的例子,做個相反的變換:

    [cpp]?view plaincopy
  • int?main(?)??
  • {??
  • ????Mat?img=imread("boy.png");??
  • ????int?img_height?=?img.rows;??
  • ????int?img_width?=?img.cols;??
  • ????vector<Point2f>?corners(4);??
  • ????corners[0]?=?Point2f(0,0);??
  • ????corners[1]?=?Point2f(img_width-1,0);??
  • ????corners[2]?=?Point2f(0,img_height-1);??
  • ????corners[3]?=?Point2f(img_width-1,img_height-1);??
  • ????vector<Point2f>?corners_trans(4);??
  • ????corners_trans[0]?=?Point2f(150,250);??
  • ????corners_trans[1]?=?Point2f(771,0);??
  • ????corners_trans[2]?=?Point2f(0,img_height-1);??
  • ????corners_trans[3]?=?Point2f(650,img_height-1);??
  • ??
  • ????Mat?transform?=?getPerspectiveTransform(corners,corners_trans);??
  • ????cout<<transform<<endl;??
  • ????vector<Point2f>?ponits,?points_trans;??
  • ????for(int?i=0;i<img_height;i++){??
  • ????????for(int?j=0;j<img_width;j++){??
  • ????????????ponits.push_back(Point2f(j,i));??
  • ????????}??
  • ????}??
  • ??
  • ????perspectiveTransform(?ponits,?points_trans,?transform);??
  • ????Mat?img_trans?=?Mat::zeros(img_height,img_width,CV_8UC3);??
  • ????int?count?=?0;??
  • ????for(int?i=0;i<img_height;i++){??
  • ????????uchar*?p?=?img.ptr<uchar>(i);??
  • ????????for(int?j=0;j<img_width;j++){??
  • ????????????int?y?=?points_trans[count].y;??
  • ????????????int?x?=?points_trans[count].x;??
  • ????????????uchar*?t?=?img_trans.ptr<uchar>(y);??
  • ????????????t[x*3]??=?p[j*3];??
  • ????????????t[x*3+1]??=?p[j*3+1];??
  • ????????????t[x*3+2]??=?p[j*3+2];??
  • ????????????count++;??
  • ????????}??
  • ????}??
  • ????imwrite("boy_trans.png",img_trans);??
  • ??
  • ????return?0;??
  • }??

  • 得到變換之后的圖片:


    《新程序員》:云原生和全面數字化實踐50位技術專家共同創作,文字、視頻、音頻交互閱讀

    總結

    以上是生活随笔為你收集整理的getPerspectiveTransform函数的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。