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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

opencv四点投影变换

發布時間:2023/12/16 编程问答 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 opencv四点投影变换 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
// opencv實現投影變換 // 2015 - 09 - 05 create by hym // 在屏幕上依次點四個點(左上->左下->右下->右上),再點擊屏幕上任意一點即可#include <opencv2/core/core.hpp> #include <opencv2/highgui/highgui.hpp> #include "opencv2/imgproc/imgproc.hpp" #include "opencv2/calib3d/calib3d.hpp" #include <iostream> #include <limits> #include <numeric> using namespace cv; using namespace std;// We need 4 corresponding 2D points(x,y) to calculate homography. vector<Point2f> left_image; // Stores 4 points(x,y) of the logo image. Here the four points are 4 corners of image. vector<Point2f> right_image; // stores 4 points that the user clicks(mouse left click) in the main image.// Image containers for main and logo image Mat imageMain; Mat imageLogo;// Function to add main image and transformed logo image and show final output. // Icon image replaces the pixels of main image in this implementation. void showFinal(Mat src1, Mat src2) {Mat gray, gray_inv, src1final, src2final;cvtColor(src2, gray, CV_BGR2GRAY);threshold(gray, gray, 0, 255, CV_THRESH_BINARY);//adaptiveThreshold(gray,gray,255,ADAPTIVE_THRESH_MEAN_C,THRESH_BINARY,5,4);bitwise_not(gray, gray_inv);src1.copyTo(src1final, gray_inv);src2.copyTo(src2final, gray);Mat finalImage = src1final + src2final;namedWindow("output", WINDOW_AUTOSIZE);imshow("output", finalImage);cvWaitKey(0);}// Here we get four points from the user with left mouse clicks. // On 5th click we output the overlayed image. void on_mouse(int e, int x, int y, int d, void *ptr) {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;// Deactivate callbackcv::setMouseCallback("Display window", NULL, NULL);// once we get 4 corresponding points in both images calculate homography matrixMat H = findHomography(left_image, right_image, 0);Mat logoWarped;// Warp the logo image to change its perspectivewarpPerspective(imageLogo, logoWarped, H, imageMain.size());showFinal(imageMain, logoWarped);}} }int main(int argc, char** argv) {// We need tow argumemts. "Main image" and "logo image"/*if (argc != 3){cout << " Usage: error" << endl;return -1;}*/// Load images from arguments passed.imageMain = imread("pai.jpg");imageLogo = imread("2.3.jpg");// Push the 4 corners of the logo image as the 4 points for correspondence to calculate homography.left_image.push_back(Point2f(float(0), float(0)));left_image.push_back(Point2f(float(0), float(imageLogo.rows)));left_image.push_back(Point2f(float(imageLogo.cols), float(imageLogo.rows)));left_image.push_back(Point2f(float(imageLogo.cols), float(0)));namedWindow("Display window", WINDOW_AUTOSIZE);// Create a window for display.imshow("Display window", imageMain);setMouseCallback("Display window", on_mouse, NULL);// Press "Escape button" to exitwhile (1){int key = cvWaitKey(10);if (key == 27) break;}return 0; }

效果圖如下:

總結

以上是生活随笔為你收集整理的opencv四点投影变换的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。