0

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?

Bodo
  • 15
  • 5
  • i'm almost sure this cannot handle 4 channel images as input – berak Mar 18 '22 at 13:35
  • Hey, thanks! This gets me at least into the right direction. If I do the conversion with 3 channels it doesn't throw the exception, although the image looks like it is confusing the cahnnels somehow. I did use: cv::Mat test(matImg.rows,matImg.cols,CV_8UC3, (cv::Vec3b*)matImg.data); – Bodo Mar 18 '22 at 13:59
  • no wait, you cant convert it like that. simply use `matImg = cv::imdecode(cv::Mat(1, SizeToRead, CV_8UC1, datamem), IMREAD_COLOR);` – berak Mar 18 '22 at 14:10
  • Perfect, that did the trick! Thank you! If you post that, I will mark it as an answer. So far I found no documentation which says that it needs three channels. Thanks a lot! – Bodo Mar 18 '22 at 14:32

1 Answers1

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

berak
  • 1,226
  • 2
  • 5
  • 7