Problem with cvCalcHist

asked 2013-03-13 14:42:59 -0500

I am trying to detect when an object enters a video feed using histograms and comparisons, but cvCalcHist is giving me an error: "Unhandled exception at 0x774515de in opencv test.exe: 0xC0000005: Access violation reading location 0xcccccccc."

any help at all would be appreciated.

Thanks

#include "opencv\cv.h"
#include "opencv\highgui.h" //include it to use GUI functions.
#include "opencv2\imgproc\imgproc.hpp"
#include <iostream>
#include <stdio.h>

void ImagesAreDifferent(IplImage* frame, IplImage* previousFrame);

int main(int argc, char** argv)
{
    cvNamedWindow( "Example2", CV_WINDOW_AUTOSIZE ); //creates window
    //CvCapture* capture = cvCreateCameraCapture( -1); //to select camera
    CvCapture* capture = cvCreateFileCapture("test video.mp4"); //uses test video
    IplImage* frame; //holds one frame from the video/camera
    IplImage* referenceFrame = NULL; //will hold the reference frame
    IplImage* previousFrame = NULL; //this is used to determine if the object is moving 
    IplImage* currentFrameArray[1];
    IplImage* referenceFrameArray[1];

    /// Using 30 bins for hue and 32 for saturation
    int h_bins = 30; int s_bins = 32;
    int histSize[] = { h_bins, s_bins };

    // Build and fill the histogram
    CvHistogram* frameA;
    {
        int hist_size[] = { h_bins, s_bins };
        float h_ranges[] = { 0, 255 };
        float s_ranges[] = { 0, 180 };
        float* ranges[] = { h_ranges, s_ranges };
        frameA = cvCreateHist( 2, hist_size, CV_HIST_ARRAY, ranges, 1 );
    }

    CvHistogram* frameB;
    {
        int hist_size[] = { h_bins, s_bins };
        float h_ranges[] = { 0, 255 };
        float s_ranges[] = { 0, 180 };
        float* ranges[] = { h_ranges, s_ranges };
        frameB = cvCreateHist( 2, hist_size, CV_HIST_ARRAY, ranges, 1 );
    }

    bool firstFrame = true;

    while(1) //run indefinitely
    {
        frame = cvQueryFrame( capture ); //takes the next frame

        //sets the reference frame
        if(firstFrame)
        {
            referenceFrame = frame;
            cvCvtColor(frame, referenceFrame, CV_BGR2HSV);          
            referenceFrameArray[0] = referenceFrame;
            firstFrame = false;
        }

        //get histograms for fast simple comparison of frames
        currentFrameArray[0] = frame;
        cvCvtColor(frame, currentFrameArray[0], CV_BGR2HSV);
        cvCalcHist(currentFrameArray, frameA);
        cvCalcHist(referenceFrameArray, frameB);
        //1.0 is a perfect match, 0.5 is a half match, -1 is complete mismatch
        if(cvCompareHist(frameA, frameB, CV_COMP_CORREL) < 0.8) 
        {
            //ImagesAreDifferent(frame, previousFrame);
        }

        if( !frame ) break; //if this is the end of the video then close
        cvShowImage( "Example2" , frame); //show the frame in the window

        char c = cvWaitKey(33); //get key pressed
        if( c == 27 ) break; // if key pressed is Esc then close
    }
    cvReleaseCapture( &capture ); //release video/camera
    cvDestroyWindow( "Example2" ); //close window
}
edit retag flag offensive close merge delete

Comments

1

How does cvCalcHist know how many frames are in your arrays?

Constantin S gravatar imageConstantin S ( 2013-03-13 20:43:26 -0500 )edit