'Exception with OpenCv DnnSuperResImpl (C++)

I have a problem with the upsample function from the dnnsuperres pack of OpenCV. I am transferring a bmp via a pipe from one application to another. The image resides in datamem, which is an unsigned char array. I use the pipe to transfer many other things, so I transfer the data in the most common datatype. If I do the following:

        cv::Mat matImg;
        matImg = cv::imdecode(cv::Mat(1, SizeToRead, CV_8UC1, datamem), -1); //-1==CV_LOAD_IMAGE_UNCHANGED 
        imshow("SomeWindowName", matImg);

This succeeds without a problem, the bitmap is shown correctly.

If I now try to apply an upsampling:

        string path = "EDSR_x4.pb";
        string modelName = "edsr";
        int scale = 4;
        DnnSuperResImpl sr;
        sr.readModel(path);
        sr.setModel(modelName, scale);
        Mat outputImage;
        sr.upsample(matImg, outputImage);

sr.upsampling() will thrown an exception. Unfortunately the exception info doesn't help me any further (cv::exception at memory location xyz, no additional info. The address seems reasonable). So I tried to simply load a bitmap from my hdd via cv::imread() and pass this to sr.upsample() and this works. I think it might be some format of the bitmap in datamem which is incompatible with sr.upsampling(). Therefore I tried to convert the bitmap to a png and pass it to the upsampling method, but the result was the same, I got the exception. The conversion I did like this:

        matImg = cv::imdecode(cv::Mat(1, SizeToRead, CV_8UC1, datamem), -1); //-1==CV_LOAD_IMAGE_UNCHANGED
        cv::Mat test(matImg.rows,matImg.cols,CV_8UC4, (cv::Vec4b*)matImg.data);//convert to png

Afterwards I passed of course test to upsampling(), but I got the same exception.

Does anyone have an idea what is causing the problem?



Solution 1:[1]

the DnnSuperResImpl will only accept 3 channel, BGR images, not your 4 channel bitmaps.

convert your image to 3 channels, while decoding it:

matImg = cv::imdecode(cv::Mat(1, SizeToRead, CV_8UC1, datamem), cv::IMREAD_COLOR);
sr.upsample(matImg, outputImage);

So far I found no documentation which says that it needs three channels.

indeed, please raise an issue here

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 berak