您好,登錄后才能下訂單哦!
本文實例為大家分享了OpenCV實現拼接圖像的具體方法,供大家參考,具體內容如下
用iphone拍攝的兩幅圖像:
拼接后的圖像:
相關代碼如下:
//讀取圖像 Mat leftImg=imread("left.jpg"); Mat rightImg=imread("right.jpg"); if(leftImg.data==NULL||rightImg.data==NULL) return; //轉化成灰度圖 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); } 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); } //獲取左邊圖像到右邊圖像的投影映射關系 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);
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持億速云。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。