简体   繁体   English

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

[英]Automatic Image Stitching from video with opencv

Hy, i have error when i stitch frame from video, here's my code 嗨,当我从视频中拼接帧时出现错误,这是我的代码

#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;
 }

if i load from image file, it works, image stitched, but when i try to stitch image from every video frame , it shows error 如果我从图像文件加载,它可以工作,可以进行图像拼接,但是当我尝试从每个视频帧中拼接图像时,会显示错误

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..

line error 线路错误

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

what's the error mean? 错误是什么意思? and how to solve it thanks 以及如何解决它,谢谢

First off, you seem to be mixing C and C++ interfaces of OpenCV ( OpenCV VideoCapture doc ). 首先,您似乎正在混合使用OpenCV的C和C ++接口( OpenCV VideoCapture doc )。 For better readability stick to one of them (Since you are using C++ just stick to using C++ functions). 为了获得更好的可读性,请坚持使用其中一种(由于您使用的是C ++,因此请坚持使用C ++函数)。

Since loading from image works, but Video doesn't, your video loading is probably the problem. 由于可以从图像加载,而不能从视频加载,因此视频加载可能是问题所在。

Try using cv::imshow("testWindow", frame) to show the frame loaded from video. 尝试使用cv::imshow("testWindow", frame)来显示从视频加载的帧。 Most likely there was no frame loaded. 很可能没有框架加载。

One possible cause is that the video file is encoded in a format not supported by OpenCV. 一个可能的原因是视频文件以OpenCV不支持的格式编码。 To check you can also run grab() and then retrieve() . 要进行检查,您还可以运行grab() ,然后运行retrieve() The grab function will return if it was successful or not. grab功能是否成功将返回。 Try grabbing a couple of frames, if all of them fail you probably don't have the necessary codec to decode this video. 尝试抓取几帧,如果它们都失败了,您可能没有必要的编解码器来解码此视频。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM