Ask Your Question
0

Problem with cv::reduce when trying to sum both columns and rows

asked 2015-04-16 12:54:38 -0500

nosocks gravatar image

updated 2015-04-20 08:50:33 -0500

I have a binary image that I want to project onto a vector v such that v[i] = 1 if the image has at least one pixel in row i with a value > 0, otherwise v[i] = 0. To accomplish this, I plan on using cv::reduce to get the sum of all pixels in a row, then use a ceiling function to return a maximum value of 1.

I am running into a problem with using cv::reduce to sum all rows in an image. Here is the code I am running:

// mask is a cv::Mat of `uint8_t`
std::vector<uint8_t> reduced;
cv::reduce(mask, reduced, 1, CV_REDUCE_SUM);

This crashes with an access violation exception. However, if I sum over columns instead:

// mask is a cv::Mat of `uint8_t`
std::vector<uint8_t> reduced;
cv::reduce(mask, reduced, 0, CV_REDUCE_SUM);

this works without issue. How am I misusing cv::reduce? This is a core piece of OpenCV, so surely I am just misusing the library :)

edit retag flag offensive close merge delete

Comments

can you try with a cv::Mat instead of a std:vector<uint8_t> ?

berak gravatar imageberak ( 2015-04-16 16:29:13 -0500 )edit

@berak Yeah, I get the same behaviour with cv::Mat

nosocks gravatar imagenosocks ( 2015-04-20 08:48:56 -0500 )edit

1 answer

Sort by ยป oldest newest most voted
0

answered 2015-04-20 10:06:54 -0500

sgarrido gravatar image

updated 2015-04-20 10:33:23 -0500

I think the problem can be the size of mask.

I guess the number of rows of mask is ok, but the number of columns is zero. You can check it by printing mask.cols and mask.rows. So you may have erroneously initialized it with something like this: cv::Mat mask(100,0,CV_...

That would explains why it fails when summing over columns, but not when summing over rows.

Anyway, I have just noticed that the ouput of cv::reduce cannot be of type uint8_t, since it has not enough capacity to store the whole sums. So, for instance, the output needs to be of type int or larger for an input of type uint8_t.

edit flag offensive delete link more
Login/Signup to Answer

Question Tools

1 follower

Stats

Asked: 2015-04-16 12:54:38 -0500

Seen: 1,238 times

Last updated: Apr 20 '15