在ORB-SLAM初始化的時候,作者提到,如果場景是平面,或者近似平面,或者低視差時,需要用到單應性矩陣(homography),所以簡單瞭解下單應性變換。
單應性變換就是一個平面到另一個平面的映射關係。在OpenCV中利用兩個圖像中至少四個對應的特徵點就能夠利用findHomography函數求解一個單應性矩陣(homography matrix),然後用這個單應性矩陣H能夠將源圖像中的某個座標(u,v)變換到目標圖像中對應的位置(u′,v′)。
// pts_src and pts_dst are vectors of points in source
// and destination images. They are of type vector
. // We need at least 4 corresponding points.
// 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);
兩個圖像能夠計算homography的前提是兩個圖像對應區域必須是同一平面。Homography是一個3*3矩陣,所以 兩張圖間的H映射關係就可以表示成:
Homography應用:圖像矯正
假設你有一張如下所示的圖片
你想點擊圖中書的四個頂點,然後得到正放的書:
該如何做?
1) 首先獲取書本四個頂點的座標 pts_src 。
2) 然後我們需要知道書本的寬高比,此書的寬高比是3/4,所以可使輸出圖像的size 為300*400,就可設其四個點的座標為(0,0),(299,0),(299,399),(0,399)保存在pts_dst中。
3) 通過pts_src和pts_dst 獲取homography。
Mat h = findHomography(pts_src, pts_dst);
4.對原圖應用homography 得到輸出。
warpPerspective(im_src, im_dst, h, size);
閱讀更多 半杯茶的小酒杯 的文章