Search code examples
c++visual-studio-2010opencvbackground-subtraction

Unhandled exception at ... Access violation reading location


#include <opencv\cv.h>
#include <opencv\highgui.h>
#include <opencv2\core\core.hpp>
#include <opencv2\objdetect\objdetect.hpp>
#include <opencv2\imgproc\imgproc.hpp>
using namespace cv;
using namespace std;

int main(int argc, char** argv)
{
    int i, M;
    Mat ycrcb, rgb, vect, Data;
    vector < String > files;
    /*Names of the pictures*/
    glob("C:\\Users\\lenovo\\Desktop\\cubicle\\trainning\\*.jpg", files); // M=number of training images

    M = files.size();
// calculatong of the matrix Data
    for (i = 0; i < M; i++)
    {

// Lecture of RGB image

        rgb = imread(files[i]);
        namedWindow("RGB image", WINDOW_AUTOSIZE);
        imshow("RGB image", rgb);
        waitKey(10);
        if (i == 0)
        { //for the first iteration
            Mat Data(M, rgb.cols * rgb.rows * 6, CV_32FC1); //statement and allocation of matrix Data
        }
        rgb.convertTo(rgb, CV_32FC3, 1.0 / 255.0);

// Convert to float // Convert the RGB color space to the color space Ycrcbb*/

        cvtColor(rgb, ycrcb, CV_BGR2YCrCb);
        //making each image a vector line

        rgb = rgb.reshape(1, rgb.total() * 3);
        ycrcb = ycrcb.reshape(1, ycrcb.total() * 3);
        /*Concatenate rgb and ycrcb*/

        hconcat(rgb, ycrcb, vect);
        fprintf(stdout,
                "rgb=[%d,%d] , ycrcb=[%d,%d], vect=[%d,%d\n",
                rgb.rows,
                rgb.cols,
                ycrcb.rows,
                ycrcb.cols,
                vect.rows,
                vect.cols);
        vect.copyTo(Data.row(i));
    }

    int nclusters = 35;
    Mat labels, centers(nclusters, Data.cols, CV_32FC1);
    /* clustering Data by kmeans*/

    kmeans(Data,
           nclusters,
           labels,
           TermCriteria(CV_TERMCRIT_ITER | CV_TERMCRIT_EPS, 0.65, 200),
           3,

           KMEANS_PP_CENTERS,
           centers);

}

This is the full code and I'm getting errors :

Unhandled exception at 0x00b85c10 in PB.exe: 0xC0000005: Access violation reading location 0xc35de59f.

this is the code


Solution

  • I know nothing of OpenCV, but this looks questionable:

    int main(int argc, char** argv)
    {
        int i, M;
        Mat ycrcb, rgb, vect, Data;
    

    Just defined variable Data of type Mat. Perfectly reasonable thing to do, unfortunately

            if (i == 0)
            { //for the first iteration
                Mat Data(M, rgb.cols * rgb.rows * 6, CV_32FC1); //statement and allocation of matrix Data
            }
    

    made a second Mat Data that hides the earlier one and only exists inside the bounds of the body of the if. All the work done setting up this Data is thrown away at the closing curly brace when the inner Data goes out of scope. Outside of the if body the original Data is still active and accessible, but has never been properly initialized. Uses of it are somewhat questionable, so when

    vect.copyTo(Data.row(i));
    

    is reached, Data likely has no row into which vect can be copied. The subsequent uses of Data are equally questionable and any one could be causing the seg fault.

    My suggestion is to hold off on the creation of Data until after all data is available.

    Since you have a very simple function, changing

    Mat ycrcb, rgb, vect, Data; 
    

    to

    Mat ycrcb, rgb, vect;
    

    and replacing

        if (i == 0)
        { //for the first iteration
            Mat Data(M, rgb.cols * rgb.rows * 6, CV_32FC1); //statement and allocation of matrix Data
         }
    

    with

        static Mat Data(M, rgb.cols * rgb.rows * 6, CV_32FC1);
    

    may be what you need.

    Read up on Static local variables here.

    EDIT

    static local variables are a bit weird. They are "sorta" global. Their lifespan runs from first use to program termination, but are only visible inside the scope that defines them.

    If defined in a function that will be called multiple times, the static variable will be allocated and initialized once. Not once per call. Subsequent entrance into the variable's scope will start with whatever the last entrance left, exactly what is required in this case. But for a larger program calling a function multiple times, this solution needs to be used with caution.