21

In the openCV cheat sheet (C++), I have found the matrix opration mean(). When I use it:

float myMatMean = mean( MyMat ); 

I get the error:

no suitable conversion function from "cv::Scalar" to "float" exists

What can I do in order to use this data?

2
  • 21
    mean() appears to be returning a variable of type cv::Scalar so try cv::Scalar myMatMean = mean(MyMat); Commented Oct 2, 2012 at 9:13
  • I think it works using double instead of float, if you dont want to bother with Scalar. But Scalar gives you mean across all channels Commented Oct 3, 2012 at 10:09

1 Answer 1

45

Thanks.

The problem was that although myMat was a 2D image. The return type was still a Scalar of size 4.

The solution was

cv::Scalar tempVal = cv::mean( myMat ); float myMAtMean = tempVal.val[0]; 
Sign up to request clarification or add additional context in comments.

2 Comments

what is mean? which header? which namesmace? please put the used namespaces when you use such non standard functions.
The problem is not that "the image is 2D" but that an image can have multiple color planes (B,G,R normally for color images in openCV) and the mean value is taken per color plane giving you one value per color plane hence the Scalar. Your solution gives you the average of the blue portion of the image (unless that is a grayscale image).

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.