Search code examples
pythonopencvfilteringnoise

OpenCV/Python: MedianBlur filter produces a black image


I'm using OpenCV in python 2.7.

I'm trying to add noise to a grayscale image like:

localvar = skimage.util.random_noise(imgray, mode="localvar")

And then I'm trying to use MedianBlur filter to reduce the noise like

median_blur = cv2.medianBlur(localvar.astype(np.uint8),3)

But when I'm trying to show median_blur image, I get an almost black image.

plt.subplot(133),plt.imshow(median_blur, cmap = 'gray')
plt.title('Median Filter'), plt.xticks([]), plt.yticks([])

Solution

  • I found the solution.

    I have to convert medianBlur image input, in my example localvar image to float32.

    Here is the solution:

    median_blur = cv2.medianBlur(np.float32(localvar),3)