Hello,
I am new to OpenCV and new to this forum so please go easy on me.
I have a test case that generates a faulty answer and wonder if there might be a bug in the cv::mean() method or if I am simply doing some newbie error.
Here is my test case:
int buffer[] = {
-1951604497, -1576126781, -1048552445, 638096737, 374919884, 1849368146,
1201985774, -716197524, 904658473, -770415778, 583557857, 719101560,
-613502793, -782012263, -342082139, 1379255953, 1670949389, 1757926314,
-281705026, 1341462613, -1148159493, -1187220669, 563026587, 457255110,
-854231508, -461342458, 320669161, -1938001093, 401558855, -1727039178,
-1882661010, -1784502717, -557639100, -2109135101, 1037216761, 662579286,
2044265374, -1276827335, 643791975, 1134686385, 567248739, 545889712 };
cv::Mat mat(6, 7, CV_32S, static_cast<void*>(buffer));
cv::Scalar mean = cv::mean(mat);
EXPECT_EQ(mean[0], -52606863.4047619);
The test reports -1.54868e+08 as the mean, which is quite far from the expected value (-5.26069e+07).
Could someone please either tell me what I am missing or confirm that cv::mean() indeed can compute wrong answers for CV_32S data.
If I convert the data to CV_64F before calling cv::mean() it produces the correct result (which could indicate internal overflow/underflow errors if OpenCV uses some low-bit integer representation in the accumulated sum but I could not mimic the behavior), but the documentation does not mention that you have to make this conversion.
This gives the correct result:
cv::Mat mat(6, 7, CV_32S, static_cast<void*>(buffer));
mat.convertTo(mat, CV_64F); // <--- Need to convert input buffer to double
cv::Scalar mean = cv::mean(mat);