在visual c ++中比较图像和视频

时间:2013-03-01 13:42:45

标签: visual-studio-2010 visual-studio visual-studio-2008 visual-c++ opencv

我正在尝试将视频与已存在于视频中的图像(.jpg格式)进行比较(采用.avi格式)。我试图在Visual C ++中完成此任务。当比较两个静态图像但是当将图像与视频进行比较时,它给出了正确的结果,在视频中没有检测到该图像。我使用以下代码来完成上述任务:

// project1.1.cpp : Defines the entry point for the console application.

#include "stdafx.h"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc_c.h"
#include "opencv2/legacy/legacy.hpp"
#include <stdio.h>
#include <iostream>

IplImage*  image[2] = { 0, 0 }, *image0 = 0, *image1 = 0;
CvSize size;

int  w0, h0,i;
int  threshold1, threshold2;
int  l,level = 4;
int sthreshold1, sthreshold2;
int  l_comp;
int block_size = 1000;
float  parameter;
double threshold;
double rezult, min_rezult;
int filter = CV_GAUSSIAN_5x5;
CvConnectedComp *cur_comp, min_comp;
CvSeq *comp;
CvMemStorage *storage;

CvPoint pt1, pt2;

static void ON_SEGMENT(int a)
{
  (void)a;
  cvPyrSegmentation(image0, image1, storage, &comp,
                    level, threshold1+1, threshold2+1);

  cvShowImage("Segmentation", image1);
}

using namespace std;
int main( int argc, char** argv ) 
{
    char* filename;

    CvCapture *capture = cvCaptureFromAVI("fractogene.avi");

    if(!capture)
    {   
      printf("!!! cvCaptureFromAVI failed (file not found?)\n");
      while(1);
      return -1; 
    }   

    int fps = (int) cvGetCaptureProperty(capture, CV_CAP_PROP_FPS);

    cvNamedWindow("display_video", CV_WINDOW_AUTOSIZE);

    IplImage* frame = NULL;
    IplImage* frame1 = NULL;
    char key = 0;

    filename = argc == 2 ? argv[1] : (char*)"loadedimage.bmp";

    if( (image[0] = cvLoadImage( filename, 1)) == 0 )
    {
        printf("Cannot load fileimage - %s\n", filename);
        return -1;
    }

    frame1=image[0];

    cvNamedWindow("Source", 0);
    cvShowImage("Source", frame1);

    while (key != 'q')
    {
        frame = cvQueryFrame(capture);

        if (frame==frame1)
            printf("frames matched\n");
        else
            printf("frames not matched\n");

        if (!frame)
        {   
            printf("!!! cvQueryFrame failed: no frame\n");
            break;
        }

        cvShowImage("display_video", frame);

        key = cvWaitKey(1000 / fps);
    }

    cvReleaseCapture(&capture);
    cvWaitKey(0);

    return 0;
} 

我的项目即将到期。如果可能,请尽量帮助。请回复您的回复。

1 个答案:

答案 0 :(得分:1)

在你的代码中,当你写if(frame == frame1)时,你正在比较指针,而不是图像内容。

比较两个图像的简单方法是逐个比较每个像素。但是这种方法禁止对图像进行任何修改(例如重新压缩,亮度修改,颜色空间修改......)。

有几种方法可以匹配两个可用的图像,并接受内容的微小修改(旋转,缩放,亮度修改......)。我邀请你谷歌“图像匹配OpenCV”并阅读一些研究实验室的文章。