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

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Homography

發(fā)布時(shí)間:2023/12/15 编程问答 34 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Homography 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
  • 什么是Homography?
    在圖1中有兩張書的平面圖,兩張圖分別有四個(gè)相對位置相同的點(diǎn),Homography就是一個(gè)變換(3*3矩陣),將一張圖中的點(diǎn)映射到另一張圖中對應(yīng)的點(diǎn)?
  • 因?yàn)镠omography是一個(gè)3*3矩陣,所以可以寫成?

    H=???h00h10h20h01h11h21h02h12h22???

    兩張圖間的H映射關(guān)系就可以表示成?

    Homography應(yīng)用:圖像對齊

    上面公式得出的H ,對于圖一中的所有點(diǎn)都是正確的,換句話說,可以用H將第一個(gè)圖中的點(diǎn)映射到第二張圖。

    如何得到一個(gè)Homography

    要得到兩張圖片的H,就必須至少知道4個(gè)相同對應(yīng)位置的點(diǎn),opencv中可以利用findHomography正確得到

    // pts_src and pts_dst are vectors of points in source // and destination images. They are of type vector<Point2f>. // We need at least 4 corresponding points.Mat h = findHomography(pts_src, pts_dst);// The calculated homography can be used to warp // the source image to destination. im_src and im_dst are // of type Mat. Size is the size (width,height) of im_dst. warpPerspective(im_src, im_dst, h, size);
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    OpenCV C++ Homography的一個(gè)簡單例子:

    #include "opencv2/opencv.hpp" using namespace cv; using namespace std; int main( int argc, char** argv) { // Read source image.Mat im_src = imread("book2.jpg");// Four corners of the book in source imagevector<Point2f> pts_src;pts_src.push_back(Point2f(141, 131));pts_src.push_back(Point2f(480, 159));pts_src.push_back(Point2f(493, 630));pts_src.push_back(Point2f(64, 601));// Read destination image.Mat im_dst = imread("book1.jpg");// Four corners of the book in destination image.vector<Point2f> pts_dst;pts_dst.push_back(Point2f(318, 256));pts_dst.push_back(Point2f(534, 372));pts_dst.push_back(Point2f(316, 670));pts_dst.push_back(Point2f(73, 473));// Calculate HomographyMat h = findHomography(pts_src, pts_dst);// Output imageMat im_out;// Warp source image to destination based on homographywarpPerspective(im_src, im_out, h, im_dst.size());// Display imagesimshow("Source Image", im_src);imshow("Destination Image", im_dst);imshow("Warped Source Image", im_out);waitKey(0); }
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41

    Homography應(yīng)用:圖像矯正

    假設(shè)你有一張如下所示的圖片?

    你想點(diǎn)擊圖中書的四個(gè)頂點(diǎn),然后得到正放的書:?

    該如何做??
    利用Homography可以做到這點(diǎn)。?
    1.首先獲取書本四個(gè)頂點(diǎn)的坐標(biāo) pts_src?
    2.然后我們需要知道書本的寬高比,此書的寬高比是3/4,所以可使輸出圖像的size 為300*400,就可設(shè)其四個(gè)點(diǎn)的坐標(biāo)為(0,0),(299,0),(299,399),(0,399)保存在pts_dst中?
    3.通過pts_src和pts_dst 獲取homography?
    4.對原圖應(yīng)用homography 得到輸出

    #include <opencv2/opencv.hpp> using namespace cv; using namespace std;struct userdata{Mat im;vector<Point2f> points; };void mouseHandler(int event, int x, int y, int flags, void* data_ptr) {if ( event == EVENT_LBUTTONDOWN ){userdata *data = ((userdata *) data_ptr);circle(data->im, Point(x,y),3,Scalar(0,0,255), 5, CV_AA);imshow("Image", data->im);if (data->points.size() < 4){data->points.push_back(Point2f(x,y));}} } void main() {// Read source image.Mat im_src = imread("book1.jpg");// Destination image. The aspect ratio of the book is 3/4Size size(300,400);Mat im_dst = Mat::zeros(size,CV_8UC3);// Create a vector of destination points.vector<Point2f> pts_dst;pts_dst.push_back(Point2f(0,0));pts_dst.push_back(Point2f(size.width - 1, 0));pts_dst.push_back(Point2f(size.width - 1, size.height -1));pts_dst.push_back(Point2f(0, size.height - 1 ));// Set data for mouse eventMat im_temp = im_src.clone();userdata data;data.im = im_temp;cout << "Click on the four corners of the book -- top left first and" << endl<< "bottom left last -- and then hit ENTER" << endl;// Show image and wait for 4 clicks. imshow("Image", im_temp);// Set the callback function for any mouse eventsetMouseCallback("Image", mouseHandler, &data);waitKey(0);// Calculate the homographyMat h = findHomography(data.points, pts_dst);// Warp source image to destinationwarpPerspective(im_src, im_dst, h, size);// Show imageimshow("Image", im_dst);waitKey(0); }
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74

    Homography應(yīng)用:虛擬廣告牌

    在足球或者棒球體育直播中,經(jīng)常可以看到球場旁邊有虛擬廣告,并且還會根據(jù)地區(qū),國家的不同播放不同的廣告,這是如何做到的??
    看完此篇博客,你應(yīng)該就能知道如何實(shí)現(xiàn)了。原理跟前一個(gè)差不多,這里直接上代碼

    #include <opencv2/opencv.hpp> using namespace cv; using namespace std; struct userdata{Mat im;vector<Point2f> points; };void mouseHandler(int event, int x, int y, int flags, void* data_ptr) {if ( event == EVENT_LBUTTONDOWN ){userdata *data = ((userdata *) data_ptr);circle(data->im, Point(x,y),3,Scalar(0,255,255), 5, CV_AA);imshow("Image", data->im);if (data->points.size() < 4){data->points.push_back(Point2f(x,y));}} } int main( int argc, char** argv) {// Read in the image.Mat im_src = imread("first-image.jpg");Size size = im_src.size();// Create a vector of points.vector<Point2f> pts_src;pts_src.push_back(Point2f(0,0));pts_src.push_back(Point2f(size.width - 1, 0));pts_src.push_back(Point2f(size.width - 1, size.height -1));pts_src.push_back(Point2f(0, size.height - 1 ));// Destination imageMat im_dst = imread("times-square.jpg");// Set data for mouse handlerMat im_temp = im_dst.clone();userdata data;data.im = im_temp;//show the imageimshow("Image", im_temp);cout << "Click on four corners of a billboard and then press ENTER" << endl;//set the callback function for any mouse eventsetMouseCallback("Image", mouseHandler, &data);waitKey(0);// Calculate Homography between source and destination pointsMat h = findHomography(pts_src, data.points);// Warp source imagewarpPerspective(im_src, im_temp, h, im_temp.size());// Extract four points from mouse dataPoint pts_dst[4];for( int i = 0; i < 4; i++){pts_dst[i] = data.points[i];}// Black out polygonal area in destination image.fillConvexPoly(im_dst, pts_dst, 4, Scalar(0), CV_AA);// Add warped source image to destination image.im_dst = im_dst + im_temp;// Display image.imshow("Image", im_dst);waitKey(0);return 0; }
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88

    ?

    結(jié)果:?

    總結(jié)

    以上是生活随笔為你收集整理的Homography的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。