Homography
在圖1中有兩張書的平面圖,兩張圖分別有四個(gè)相對位置相同的點(diǎn),Homography就是一個(gè)變換(3*3矩陣),將一張圖中的點(diǎn)映射到另一張圖中對應(yīng)的點(diǎn)?
因?yàn)镠omography是一個(gè)3*3矩陣,所以可以寫成?
兩張圖間的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 得到輸出
- 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è)差不多,這里直接上代碼
- 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)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: win10优化 win10升级后优化教程
- 下一篇: YCrCb与YPrPb的含义与区别