
如圖,在使用warpPerspective函式進行Homography變換后得到右圖,如何才能將右圖覆寫到左圖中的螢屏上去呢?
#include "opencv2/opencv.hpp"
#include "highgui.hpp"
using namespace cv;
using namespace std;
int main(int argc, char** argv)
{
// Read source image.
Mat im_src = imread("圖片1.png");
// Four corners of the book in source image
vector<Point2f> pts_src;
pts_src.push_back(Point2f(0, 0));
pts_src.push_back(Point2f(590, 0));
pts_src.push_back(Point2f(590, 460));
pts_src.push_back(Point2f(0, 460));
// Read destination image.
Mat im_dst = imread("圖片2.png");
// Four corners of the book in destination image.
vector<Point2f> pts_dst;
pts_dst.push_back(Point2f(68, 129));
pts_dst.push_back(Point2f(169, 217));
pts_dst.push_back(Point2f(150, 269));
pts_dst.push_back(Point2f(19, 203));
// Calculate Homography
Mat h = findHomography(pts_src, pts_dst);
// Output image
Mat im_out;
// Warp source image to destination based on homography
warpPerspective(im_src, im_out, h, im_dst.size());
// Display images
imshow("Source Image", im_src);
imshow("Destination Image", im_dst);
imshow("Warped Source Image", im_out);
// Mat roi = im_dst(Rect(0, 0, im_dst.cols, im_dst.rows));
//Mat mask(roi.rows, roi.cols, roi.depth(), Scalar(1));
// im_out.copyTo(roi, im_out);
imshow("Warped Source Image", im_out);
waitKey(0);
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/92277.html
標籤:OpenCV
下一篇:opencv
