平面射影變換是關於其次3維矢量的一種線性變換,可以使用一個非奇異的$3 \times 3$矩陣H表示,$X' = HX$,射影變換也叫做單應(Homography)。計算出兩幅圖像之間的單應矩陣H,那么應用這個關系可以將一個視圖中的
所有點變換到另一個視圖中。
上圖,最右邊圖像是將最左邊圖像進行了一次射影變換,變換到中間圖像視圖后的圖像。
使用OpenCV可以調用庫函數findHomography計算兩幅圖像的單應矩陣,其聲明如下
Mat findHomography(InputArray srcPoints, InputArray dstPoints, int method=0, double ransacReprojThreshold=3, OutputArray mask=noArray() )
單應矩陣的計算需要兩幅圖像中相對應的點,srcPoints,dstPoints是兩幅圖像中相對應的點,可以是Vector<Point2f>或者是CV_32FC2類型的矩陣,Method是計算單應矩陣時所使用的方法。
得到了圖像的單應矩陣H就可以使用函數warpPerspective將圖像變換到另一個視圖中
void warpPerspective(InputArray src, OutputArray dst, InputArray M, Size dsize, int flags=INTER_LINEAR, int borderMode=BORDER_CONSTANT, const Scalar& borderValue=Scalar())
下面就使用上面提到的兩個函數,計算出兩幅圖像之間的單應矩陣H,並且將兩幅圖像合並為一副圖像。
其操作過程非常簡單,
- 在“大”圖像(目標圖像)上選擇4個點和“小”圖像(被合並圖像)的四角做對應,然后根據這4對對應的點計算兩幅圖像的單應矩陣。
- 得到單應矩陣H后,利用函數warpPerspective將H應用到“小”圖像上,得到圖像M
- 將圖像M合並到目標圖像中選擇的四個點的位置
實現
首先定義兩個vector保存對應的4對點
//4對相對應的像點計算圖像的單應 Homography vector<Point2f> left_image; vector<Point2f> right_image;
將小圖像的四角坐標插入到left_image中
left_image.push_back(Point2f(0, 0)); left_image.push_back(Point2f(0, image_logo.rows)); left_image.push_back(Point2f(image_logo.cols, image_logo.rows)); left_image.push_back(Point2f(image_logo.cols, 0));
在在大圖中選擇4個點,並用這4對相對應的點計算單應矩陣H
if (e == EVENT_LBUTTONDOWN){ if (right_image.size() < 4){ right_image.push_back(Point2f(float(x), float(y))); cout << x << " " << y << endl; } else { cout << "Calculating Homography" << endl; setMouseCallback("Display window", nullptr, nullptr); Mat H = findHomography(left_image, right_image, 0); Mat logoWarped; warpPerspective(image_logo, logoWarped,H, image_main.size()); showFinal(image_main, logoWarped); } }
最后,將logoWarped和main圖像相加就得到最后結果。
詳細代碼:https://github.com/brookicv/opencvSample/tree/master/Homography