Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Use of Mat in cvAvgSdv gives error

I am using VS2015, c++, and openCV 3.1

I am trying to calculate the mean and standard deviation of an image stored in a Mat using the cvAvgSdv function. My code is:

Mat gray_img;
cvtColor(gimage, gray_img, CV_RGB2GRAY);
calcHist(&gray_img, 1, 0, Mat(), hist, 1, &histSize, ranges, true, false);
CvScalar image_mean, image_std_dev;
cvAvgSdv(&gray_img, &image_mean, &image_std_dev, NULL);

This compiles but throws an exception when I run my program (I really wish VS explained more what went wrong versus giving some useless memory address). Why wouldn't this code work?

My work-around has been to convert gray_img to an IplImage with the following:

Mat gray_img;
cvtColor(gimage, gray_img, CV_RGB2GRAY);
calcHist(&gray_img, 1, 0, Mat(), hist, 1, &histSize, ranges, true, false);

// Convert Mat into IplImage so we can run cvAvgSdv without an error
    IplImage* image2;
    image2 = cvCreateImage(cvSize(gray_img.cols, gray_img.rows), 8, 1);
    IplImage ipltemp = gray_img;
    cvCopy(&ipltemp, image2);

CvScalar image_mean, image_std_dev;
cvAvgSdv(&image2, &image_mean, &image_std_dev, NULL);

Any insight would be appreciated!