使用opencv从视频自动图像拼接

时间:2014-04-12 21:51:39

标签: c++ opencv video-processing image-stitching

Hy,当我从视频缝合帧时,我有错误, 这是我的代码

#include <stdio.h>
#include <iostream>

#include "opencv2/core/core.hpp"
#include "opencv2/features2d/features2d.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/nonfree/nonfree.hpp"
#include "opencv2/calib3d/calib3d.hpp"
#include "opencv2/imgproc/imgproc.hpp"

using namespace std;
using namespace cv;

Mat Stitching(Mat image1,Mat image2){
 Mat gray_image1;
 Mat gray_image2;
 // Convert to Grayscale
 cvtColor( image1, gray_image1, CV_RGB2GRAY );
 cvtColor( image2, gray_image2, CV_RGB2GRAY );

//-- Step 1: Detect the keypoints using SURF Detector
 int minHessian = 10;

SurfFeatureDetector detector( minHessian );

std::vector< KeyPoint > keypoints_object, keypoints_scene;

detector.detect( gray_image1, keypoints_object );
 detector.detect( gray_image2, keypoints_scene );

//-- Step 2: Calculate descriptors (feature vectors)
 SurfDescriptorExtractor extractor;

Mat descriptors_object, descriptors_scene;

extractor.compute( gray_image1, keypoints_object, descriptors_object );
 extractor.compute( gray_image2, keypoints_scene, descriptors_scene );

//-- Step 3: Matching descriptor vectors using FLANN matcher
 FlannBasedMatcher matcher;
 std::vector< DMatch > matches;
 matcher.match( descriptors_object, descriptors_scene, matches );

double max_dist = 0; double min_dist = 100;

//-- Quick calculation of max and min distances between keypoints
 for( int i = 0; i < descriptors_object.rows; i++ )
 { double dist = matches[i].distance;
 if( dist < min_dist ) min_dist = dist;
 if( dist > max_dist ) max_dist = dist;
 }

printf("-- Max dist : %f \n", max_dist );
 printf("-- Min dist : %f \n", min_dist );

//-- Use only "good" matches (i.e. whose distance is less than 3*min_dist )
 std::vector< DMatch > good_matches;

for( int i = 0; i < descriptors_object.rows; i++ )
 { if( matches[i].distance < 3*min_dist )
 { good_matches.push_back( matches[i]); }
 }
 std::vector< Point2f > obj;
 std::vector< Point2f > scene;

for( int i = 0; i < good_matches.size(); i++ )
 {
 //-- Get the keypoints from the good matches
 obj.push_back( keypoints_object[ good_matches[i].queryIdx ].pt );
 scene.push_back( keypoints_scene[ good_matches[i].trainIdx ].pt );
 }

// Find the Homography Matrix
 Mat H = findHomography( obj, scene, CV_RANSAC );
 // Use the Homography Matrix to warp the images
 cv::Mat result;
 warpPerspective(image1,result,H,cv::Size(800,600));
 cv::Mat half(result,cv::Rect(0,0,image2.cols,image2.rows));
 image2.copyTo(half);
 //imshow( "Result", result );
 return result;
}

/** @function main */
int main( int argc, char** argv )
{    
// Load the images
 //Mat image1= imread( "E:\\Tugas Akhir\\image\\city2.jpg" );
 //Mat image2= imread( "E:\\Tugas Akhir\\image\\city1.jpg" );
 char *fileName = "E:\\Tugas Akhir\\Video Master\\indv_img_3a.avi";
    /* Create a window */
    cvNamedWindow("Stitching", CV_WINDOW_AUTOSIZE);
    /* capture frame from video file */
    CvCapture* capture = cvCreateFileCapture(fileName);
    /* Create IplImage to point to each frame */
    IplImage* frame;
    IplImage before_frame;
    Mat image1;
    Mat image2;
    cv::Mat result;
    /* Loop until frame ended or ESC is pressed */
    int loop=0;
    //imshow( "Result", Stitching(image1,image2));
    while(1) {
        frame = cvQueryFrame(capture);
        if(loop>0){ 
            if(!frame) break;

            image2=Mat(frame, false);
            result=Stitching(image1,image2);
            before_frame=result;
            frame=&before_frame;
            image1=result;
            image2.release();
            //imshow("Stitching",frame);
            cvShowImage("Stitching",frame);                                             
            //break;

        }else if(loop==0){          
            //Mat aimage1(frame);
            image1=Mat(frame, false);
        }
        loop++;
        char c = cvWaitKey(33);
        if(c==27) break;
    }

 cvReleaseCapture(&capture);
    /* delete window */
 //   cvDestroyWindow("Stitching");

   // return EXIT_SUCCESS;
 waitKey(0);
 return 0;
 }

如果我从图像文件加载,它工作,图像拼接,但当我尝试从每个视频帧缝合图像时,它显示错误

First-chance exception at 0x000007f886dd64a8 in matchingHomography.exe: Microsoft C++ exception: cv::Exception at memory location 0x0080e3b0..
Unhandled exception at 0x000007f886dd64a8 in matchingHomography.exe: Microsoft C++ exception: cv::Exception at memory location 0x0080e3b0..

行错误

Mat H = findHomography( obj, scene, CV_RANSAC );

错误是什么意思?以及如何解决它 感谢

1 个答案:

答案 0 :(得分:0)

首先,您似乎混合了OpenCV(OpenCV VideoCapture doc)的C和C ++接口。为了更好的可读性,请坚持使用其中一个(因为您使用C ++只是坚持使用C ++函数)。

由于从图片加载,但视频没有,您的视频加载可能是问题。

尝试使用cv::imshow("testWindow", frame)显示从视频加载的帧。很可能没有加载框架。

一个可能的原因是视频文件是以OpenCV不支持的格式编码的。要检查您还可以运行grab(),然后retrieve()。如果成功与否,grab函数将返回。尝试抓取几个帧,如果所有帧都失败了,你可能没有必要的编解码器来解码这个视频。