Ask Your Question
0

Use of Mat in cvAvgSdv gives error

asked 2016-07-20 11:45:38 -0500

CoReCam gravatar image

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!

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
2

answered 2016-07-20 12:30:40 -0500

berak gravatar image

mixing (outdated) c and c++ code is a terrible idea here. you can safely use meanStdDev() like this:

Mat gimage = ... // your input
Mat gray_img;
cvtColor(gimage, gray_img, CV_RGB2GRAY);
Scalar m; // mean
Scalar s; // stdev
meanStdDev(gray_img, m,s);
edit flag offensive delete link more

Comments

Thanks! That worked great!

I guess my googling and stackoverflow results are turning up examples with combinations of c and c++ code. I suppose most of the code is being migrated to be c++ code?

CoReCam gravatar imageCoReCam ( 2016-07-20 13:07:17 -0500 )edit
Login/Signup to Answer

Question Tools

1 follower

Stats

Asked: 2016-07-20 11:45:38 -0500

Seen: 413 times

Last updated: Jul 20 '16