Ask Your Question

cv::reduce gives unsupported format exception

asked 2012-11-02 10:34:35 -0600

manmedia gravatar image


I have been trying to use cv::reduce to get row and column projection sums for my image processing operations. However, I am getting an exception about unsupported format for source and destination matrices. My source-code (dev. stage) is given below

    #include "iostream"
    #include "cmath"
    #include "opencv2/core/core.hpp"
    #include "typeinfo"

using namespace std;

int main() {

cv::Mat rt(cv::Mat::ones(5,5,CV_8UC1));
cv::Mat gg(cv::Mat::zeros::(1,5,CV_8UC1));
cv::reduce(rt,gg,0,0, -1);
return 0;


I don't know what is wrong with it!! I have got all the necessary libraries and path setup, otherwise it won't even compile, link, or run. But it is throwing an exception. Could someone educate me or point me to the right direction.

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted

answered 2012-11-02 11:16:06 -0600

Vladislav Vinogradov gravatar image

change gg type to CV_32SC1:

cv::Mat gg(cv::Mat::zeros::(1,5,CV_32SC1));

cv::reduce for SUM and 8U input can return only 32S, 32F or 64F types.

edit flag offensive delete link more


Thanks! You are the first person, honestly to God, who gave me a logical explanation why it might not be working!! +1 for that to you.... :)

Do you happen to know the literal values for CV_REDUCE_SUM, CV_REDUCE_MAX, etc. ? I cannot add them to my code, because it is undefined !! :(

Bad news is that in OpenCV 2.4.3 (still in trunk, not release) I cannot find the CV_REDUCE_SUM flag. That is why I don't know the integer value to put instead of CV_REDUCE_SUM. Nevertheless, it still doesn't give me any result

manmedia gravatar imagemanmedia ( 2012-11-02 14:28:58 -0600 )edit

CV_REDUCE_SUM, CV_REDUCE_MAX and other are defined in "opencv2/core/core_c.h" file.





Vladislav Vinogradov gravatar imageVladislav Vinogradov ( 2012-11-03 08:04:52 -0600 )edit

Hi...sorry but it didn't work! :( still suggesting unsupported format. I have attached my code here:

#include "iostream"
#include "opencv2/core/core.hpp"
#include "opencv2/core/core_c.h"
using namespace std;
int main() {
cv::Mat testMat = cv::Mat(cv::Mat::zeros(4,4,CV_8UC1));
for(int i = 0; i < 4; i++) {<unsigned char>(0,i) = 3;<unsigned char>(1,i) = 1;<unsigned char>(2,i) = 2;<unsigned char>(3,i) = 4;

cv::Mat gg(cv::Mat::zeros(1,4,CV_64FC1));
cv::Mat ck(cv::Mat::ones(4,4,CV_8U));
std::cout << gg << std::endl;
return 0;}

I am using openCV 2.4.3 (not release completely). Do you find any different result if you run it? Thanks!!

manmedia gravatar imagemanmedia ( 2012-11-04 05:24:00 -0600 )edit

You should explicitly specify output type

cv::Mat gg;
cv::reduce(testMat, gg, 0, CV_REDUCE_SUM, CV_64FC1);
Vladislav Vinogradov gravatar imageVladislav Vinogradov ( 2012-11-04 08:59:50 -0600 )edit
1 worked! I hope it is EXPLICITLY stated in the current and future releases of OpenCV docs so that the users don't have to struggle and understand

manmedia gravatar imagemanmedia ( 2012-11-05 03:25:50 -0600 )edit

Thanks alot! I had the same problem and you are the only correct solution in the entire WWW!

I also got an acceptation when using

  cv::reduce(testMat, gg, 0, CV_REDUCE_SUM, -1);

however when I used:

  cv::reduce(testMat, gg, 0, CV_REDUCE_SUM, CV_32SC1);

It worked.

Conclusion: You need to specify the output type both when defining the reduced matrix and ALSO when calling 'cv::reduce()'

Y Simson gravatar imageY Simson ( 2014-04-07 11:03:59 -0600 )edit

Question Tools


Asked: 2012-11-02 10:34:35 -0600

Seen: 12,990 times

Last updated: Nov 02 '12