Aparajith Sairam
Aparajith Sairam

Reputation: 373

OpenCV MedianBlur function crashing when Mat is of type CV_32S

I am working on Demosaicing a Bayer pattern to RGB image without using OpenCV's direct conversion function. I have used Bilinear interpolation and got it to work, but I want to improve the quality by using The Freeman Method. This method requires Median Filter. OpenCV has medianBlur function which does that. But I am having trouble using this function. When the cv::Mat to which I apply medianBlur is of type CV_8UC1 then it works, but if it is of type CV_32S, then it does not work. This does NOT work:

    redGreenMedian.create(input.size(), CV_32S);
    blueGreenMedian.create(input.size(), CV_32S);
    blueMinusGreen.create(input.size(), CV_32S);
    redMinusGreen.create(input.size(), CV_32S);
    for(int i = 1; i <= 3; i += 2)
    {
            cv::medianBlur(redMinusGreen, redGreenMedian, i);
            cv::medianBlur(blueMinusGreen, blueGreenMedian, i);
    }

If I change all CV_32S to CV_8UC1 then it works. On debugging I found that it crashes in the second iteration only not in first one. However, I need it to run for both iterations. This does NOT work when written separately too:

    cv::medianBlur(redMinusGreen, redGreenMedian, 3);

As an aside, I do not have to use CV_32S, but I need the ability to store negative numbers in the matrices.
NOTE: I have tried making all numbers in the matrices positive and then use medianBlur, but it still did not work.
All help is appreciated. Thanks in advance.

Upvotes: 3

Views: 5332

Answers (1)

Roger Rowland
Roger Rowland

Reputation: 26259

The OpenCV documentation seems to be very clear:

src – input 1-, 3-, or 4-channel image; when ksize is 3 or 5, the image depth should be CV_8U, CV_16U, or CV_32F, for larger aperture sizes, it can only be CV_8U.

As you need to cater for signed values, I think your best option is to use CV_32F.

Also, the documentation says

ksize – aperture linear size; it must be odd and greater than 1, for example: 3, 5, 7

Your loop applies sizes of 1 and 3 (if I read your code correctly), the first of which is invalid, which possibly explains why your first iteration doesn't crash (because it fails earlier).

Upvotes: 6

Related Questions