user1900267
user1900267

Reputation: 1

Comparing an image and a video in visual c++

I am trying to compare a video with an image (in .jpg format) which is already present in the video (in .avi format). I am trying to accomplish this task in Visual C++. It gives the correct result when two static images are compared but when an image is compared with the video, that image is not detected in the video. I am using the following code to accomplish the above task:

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

My project is due soon.Kindly please help if possible.Looking forward to your reply.

Upvotes: 0

Views: 797

Answers (1)

cedrou
cedrou

Reputation: 2790

In your code, when you write if(frame==frame1), you are comparing the pointers, and not the image content.

The simple way to compare two images is to compare each pixel, one by one. But this method forbids any modification in the image (for example recompression, luminance modification, color space modification, ...).

There are several methods to match two images that are available and that accept minor modifications of the content (rotation, zoom, luminance modification, ...). I invite you to google "Image Matching OpenCV" and to read some research labs articles.

Upvotes: 1

Related Questions