19

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?

Markus Dutschke
  • 9,341
  • 4
  • 63
  • 58
EyalG
  • 1,193
  • 3
  • 10
  • 11
  • 20
    `mean()` appears to be returning a variable of type `cv::Scalar` so try `cv::Scalar myMatMean = mean(MyMat);` – hmjd Oct 02 '12 at 09: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 – remi Oct 03 '12 at 10:09

1 Answers1

42

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];
Mohit Motwani
  • 4,662
  • 3
  • 17
  • 45
EyalG
  • 1,193
  • 3
  • 10
  • 11
  • 1
    what is mean? which header? which namesmace? please put the used namespaces when you use such non standard functions. – Mehdi Apr 17 '20 at 13:15
  • 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). – Bjørn van Dommelen Mar 24 '21 at 06:01