OpenCv实现两幅图像的拼接
生活随笔
收集整理的這篇文章主要介紹了
OpenCv实现两幅图像的拼接
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
直接貼上源碼
來源:http://www.myexception.cn/image/1498389.html
實驗效果
Left.jpg????????????????????????????
right.jpg
ImageMatch.jpg
?
#include <iostream> #include <iomanip> #include "opencv2/core/core.hpp" #include "opencv2/objdetect/objdetect.hpp" #include "opencv2/features2d/features2d.hpp" #include "opencv2/highgui/highgui.hpp" #include "opencv2/calib3d/calib3d.hpp" #include "opencv2/nonfree/nonfree.hpp" #include "opencv2/imgproc/imgproc_c.h" #include "opencv2/legacy/legacy.hpp" #include "opencv2/legacy/compat.hpp" using namespace cv; using namespace std;int main() {Mat leftImg=imread("left.jpg");Mat rightImg=imread("right.jpg");if(leftImg.data==NULL||rightImg.data==NULL)return 0;//轉(zhuǎn)化成灰度圖 Mat leftGray;Mat rightGray;cvtColor(leftImg,leftGray,CV_BGR2GRAY);cvtColor(rightImg,rightGray,CV_BGR2GRAY);//獲取兩幅圖像的共同特征點int minHessian=400;SurfFeatureDetector detector(minHessian);vector<KeyPoint> leftKeyPoints,rightKeyPoints;detector.detect(leftGray,leftKeyPoints);detector.detect(rightGray,rightKeyPoints);SurfDescriptorExtractor extractor;Mat leftDescriptor,rightDescriptor;extractor.compute(leftGray,leftKeyPoints,leftDescriptor);extractor.compute(rightGray,rightKeyPoints,rightDescriptor);FlannBasedMatcher matcher;vector<DMatch> matches;matcher.match(leftDescriptor,rightDescriptor,matches); int matchCount=leftDescriptor.rows;if(matchCount>15){matchCount=15;//sort(matches.begin(),matches.begin()+leftDescriptor.rows,DistanceLessThan);sort(matches.begin(),matches.begin()+leftDescriptor.rows);} vector<Point2f> leftPoints;vector<Point2f> rightPoints;for(int i=0; i<matchCount; i++){leftPoints.push_back(leftKeyPoints[matches[i].queryIdx].pt);rightPoints.push_back(rightKeyPoints[matches[i].trainIdx].pt);}//獲取左邊圖像到右邊圖像的投影映射關(guān)系Mat homo=findHomography(leftPoints,rightPoints);Mat shftMat=(Mat_<double>(3,3)<<1.0,0,leftImg.cols, 0,1.0,0, 0,0,1.0);//拼接圖像 Mat tiledImg;warpPerspective(leftImg,tiledImg,shftMat*homo,Size(leftImg.cols+rightImg.cols,rightImg.rows));rightImg.copyTo(Mat(tiledImg,Rect(leftImg.cols,0,rightImg.cols,rightImg.rows)));//保存圖像imwrite("tiled.jpg",tiledImg);//顯示拼接的圖像imshow("tiled image",tiledImg);waitKey(0);return 0; }?
總結(jié)
以上是生活随笔為你收集整理的OpenCv实现两幅图像的拼接的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: struct2(二) struct2的h
- 下一篇: LAMP 系统性能调优:第2 部分: 优