opencv四点投影变换
2016-03-29 21:58
465 查看
// 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 callback cv::setMouseCallback("Display window", NULL, NULL); // once we get 4 corresponding points in both images calculate homography matrix Mat H = findHomography(left_image, right_image, 0); Mat logoWarped; // Warp the logo image to change its perspective warpPerspective(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 exit while (1) { int key = cvWaitKey(10); if (key == 27) break; } return 0; }
效果图如下:
相关文章推荐
- python中使用OpenCV进行人脸检测的例子
- opencv 做人脸识别 opencv 人脸匹配分析
- 使用opencv拉伸图像扩大分辨率示例
- 基于C++实现kinect+opencv 获取深度及彩色数据
- OpenCV 2.4.3 C++ 平滑处理分析
- Python中使用OpenCV库来进行简单的气象学遥感影像计算
- 利用Python和OpenCV库将URL转换为OpenCV格式的方法
- python结合opencv实现人脸检测与跟踪
- Python环境搭建之OpenCV的步骤方法
- Python实现OpenCV的安装与使用示例
- 在树莓派2或树莓派B+上安装Python和OpenCV的教程
- opencv-python学习一--人脸检测
- 在Ubuntu上安装OpenCV3.0和Python-openCV的经历
- OpenCV配置,从来没有这么简单!
- ubuntu下opencv和qt的安装配置
- OpenCV学习笔记(二十五)——OpenCV图形界面设计Qt+VS2008
- 分享一些OpenCV实现立体视觉的经验
- 关于OpenCv图像变换与基本图形检测
- "应用程序正常初始化失败"-0xc0150002 解决办法
- OpenCV->HSV色彩空间