本文實(shí)例為大家分享了OpenCV實(shí)現(xiàn)拼接圖像的具體方法,供大家參考,具體內(nèi)容如下
成都創(chuàng)新互聯(lián)主要從事成都網(wǎng)站制作、成都網(wǎng)站設(shè)計(jì)、外貿(mào)營銷網(wǎng)站建設(shè)、網(wǎng)頁設(shè)計(jì)、企業(yè)做網(wǎng)站、公司建網(wǎng)站等業(yè)務(wù)。立足成都服務(wù)太仆寺,10年網(wǎng)站建設(shè)經(jīng)驗(yàn),價(jià)格優(yōu)惠、服務(wù)專業(yè),歡迎來電咨詢建站服務(wù):13518219792
用iphone拍攝的兩幅圖像:
拼接后的圖像:
相關(guān)代碼如下:
//讀取圖像 Mat leftImg=imread("left.jpg"); Mat rightImg=imread("right.jpg"); if(leftImg.data==NULL||rightImg.data==NULL) return; //轉(zhuǎn)化成灰度圖 Mat leftGray; Mat rightGray; cvtColor(leftImg,leftGray,CV_BGR2GRAY); cvtColor(rightImg,rightGray,CV_BGR2GRAY); //獲取兩幅圖像的共同特征點(diǎn) int minHessian=400; SurfFeatureDetector detector(minHessian); vectorleftKeyPoints,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 matches; matcher.match(leftDescriptor,rightDescriptor,matches); int matchCount=leftDescriptor.rows; if(matchCount>15) { matchCount=15; sort(matches.begin(),matches.begin()+leftDescriptor.rows,DistanceLessThan); } vector leftPoints; vector rightPoints; for(int i=0; i (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);
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持創(chuàng)新互聯(lián)。