Search code examples
c++image-processingopencvthreshold

(Adaptive) thresholding in opencv error (Bad argument (Unknown array type) in cvarrToMat)


I'm trying to use thresholding on my video stream but it is not working.

My video stream:

Mat *depthImage = new Mat(480, 640, CV_8UC1, Scalar::all(0));

Then i try to do the adaptive thresholding, (also doesn't work with regular thresholding)

for(;;){

    if( wrapper->update()){


        wrapper->getDisplayDepth(depthImage);

        cvAdaptiveThreshold(depthImage, depthImage,255,CV_ADAPTIVE_THRESH_MEAN_C, CV_THRESH_BINARY,75,10);


        imshow("Depth", *depthImage);

    }

    int k = waitKey(25);
    if(k == 27 ) exit(0);
}

I get this error :

OpenCV Error: Bad argument (Unknown array type) in cvarrToMat, file /Users/olivierjanssens/source/OpenCV-2.3.1/modules/core/src/matrix.cpp, line 646 terminate called throwing an exception

What am i doing wrong, i can get display and see the stream perfectly.But when i add this thresholding i get the error previously mentioned. (i'm rather new to opencv btw).

Thx in advance !


Solution

  • Your depthImage is a pointer to a cv::Mat, which to me seems strange...

    ...but, if you're using the C++ syntax then you'll want to use the C++ version of adaptiveThreshold, which deals with cv::Mat, with the following definition:

    void adaptiveThreshold(InputArray src, OutputArray dst, double maxValue,
        int adaptiveMethod, int thresholdType, int blockSize, double C);
    

    which will need prefixed by cv:: if you're not using that namespace already.

    For example:

    Mat *depthImage; // Obtain this using your method
    Mat image = *depthImage;  // Obtain a regular Mat to use (doesn't copy data, just headers)
    
    adaptiveThreshold(image, image,255,CV_ADAPTIVE_THRESH_MEAN_C, CV_THRESH_BINARY,75,10);
    
    imshow("Depth Image", *depthImage);
    // OR
    imshow("Depth Image", image);