Ask Your Question
0

OpenCV mean function crashes with mask created from OpenCV threshold function?

asked 2016-04-11 13:10:51 -0600

the_naive_ gravatar image

updated 2016-04-11 13:18:48 -0600

I'm very new to OpenCV, so I guess I'm making some silly mistake.

First I'm creating an empty cv::mat matrix, mask, of CV_8U type and filling it with zeros. Then I'm filling the matrix with either 0's or 255's by checking the values in CV_32FC1 type cv::mat matrix croppedDifferenceImage by the use of cv::threshold. Then I'm using mask as a parameter for cv::mean function.

cv::Mat mask = cv::Mat(croppedDifferenceImage.rows, croppedDifferenceImage.cols, CV_8U, cv::Scalar(0));
cv::threshold(croppedDifferenceImage, mask, 3.2, 255, CV_THRESH_BINARY);
double mean = cv::mean(croppedDifferenceImage, mask)[0];

But I keep having a crash with the message:

/build/buildd/opencv-2.4.8+dfsg1/modules/core/src/stat.cpp:565: error: (-215) mask.empty() || mask.type() == CV_8U in function mean

What am I doing wrong?

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
0

answered 2016-04-11 14:08:16 -0600

LBerger gravatar image

updated 2016-04-11 14:10:52 -0600

Theshold result type is same type of source image so it is CV_32F. You don't need initialisation before threshold called

Mat croppedDifferenceImage(256,256,CV_32FC1);
for (int i=0;i<croppedDifferenceImage.rows;i++)
for (int j=0;j<croppedDifferenceImage.cols;j++)
    croppedDifferenceImage.at<float>(i,j)=i/10.;
cv::Mat mask,maskBin ;
cv::threshold(croppedDifferenceImage, mask, 3.2, 255, CV_THRESH_BINARY);
mask.convertTo(maskBin,CV_8U);
imshow("r",mask);
double mean = cv::mean(croppedDifferenceImage, maskBin)[0];

cout<<mean;
edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2016-04-11 13:10:51 -0600

Seen: 2,444 times

Last updated: Apr 11 '16