Herr von Wurst
Herr von Wurst

Reputation: 2621

Reduce function to calculate sum of each column

I got stuck with OpenCV while trying to calculate the sum of each column for a thresholded (binary) image.

This code

Mat thrs;
threshold(roi, thrs, 252, 255, THRESH_BINARY);
Mat dy;
reduce(thrs, dy, 1, CV_REDUCE_SUM);

gives me a runtime error stating

OpenCV Error: Unsupported format or combination of formats

I assume this is due to the binary image being in CV_8UC1 format, therefore dy is in the same format and cannot hold the summed up values. Is this correct? Whats the way around it?

Upvotes: 2

Views: 6551

Answers (2)

akarsakov
akarsakov

Reputation: 2154

If you not specify the last parameter dtype and destination matrix is not initialized cv::reduce will assume that dtype equal to type of source matrix. In your case it will be CV_8UC1. Since this format can't store summed values you got the runtime error.

So to avoid this error you need to specify dtype parameter. For example:

reduce(thrs, dy, 1, CV_REDUCE_SUM, CV_32SC1);

Upvotes: 7

api55
api55

Reputation: 11420

The reduce function supports only 32S 32F and 64F as Output. This is not in the documentation of the reduce function... but it is like that... by default it will try to take the same type as the input. So, you may try doing something like this:

cv::reduce(thrs, dy, 1, CV_REDUCE_SUM, CV_32S);

just a reminder:

32S is int
32F is float
64F is float

Upvotes: 4

Related Questions