Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OpenCv 3.3 Cuda Medianfilter generates 2/3 of image black

Tags:

c++

image

opencv

I'm trying to use the cv::cuda::createMedianfilter function in OpenCV library.

This simple code produces an image which is 2/3 black, and only 1/3 is filtered.

using namespace cv;
int main(int argc, char** argv)
{
  Mat src; Mat dst;
  src = imread("test.bmp", 1);
  cuda::GpuMat imageGpu(src),imageGpuOut;
  cuda::Stream stream;
  try
    {
        Ptr<cuda::Filter> filterX = cuda::createMedianFilter(CV_8UC1, 31);
        filterX->apply(imageGpu, imageGpuOut,stream);
        imageGpuOut.download(dst,stream);
        stream.waitForCompletion();
        imwrite("test_cuda31.bmp", dst);
    }
    catch (cv::Exception& e)
    {
        const char* err_msg = e.what();
        std::cout << "exception caught: " << err_msg << std::endl;
    }
}

Input image looks like this :Input image
Output image looks like this :Output image

I have tried several other images with different sizes and also different kernal sizes on the medianfilter. All with the same result.

Any ideas on what I'm doing wrong ?

like image 812
Trond Tunheim Avatar asked Jan 01 '26 10:01

Trond Tunheim


1 Answers

You are loading an image of 3 channels (BGR)

src = imread("test.bmp", 1);

then, you are using as if the source was only 1 channel

Ptr<cuda::Filter> filterX = cuda::createMedianFilter(CV_8UC1, 31);

then it will process only 1/3 of the pixels, and probably the image is initialized in all 0, thus the black pixels in the rest of the image.

Sadly, cuda::createMedianFilter only supports CV_8UC1, so it is not possible to change that, but you can load the image as an CV_8UC1

src = imread("test.bmp", 0);

or convert it to greyscale after the load.

I hope this solves your problem

like image 193
api55 Avatar answered Jan 03 '26 11:01

api55



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!