Search code examples
python-3.xscikit-image

Why does the scikit median filter produce a blank output image?


I am trying to de-noise the following picture using the median filter of Scikit Image.

enter image description here

This is a picture with a dimension of 250 X 100 pixels. The pixels are either black or white.

dtype=uint8
shape=(100,250)
max=255
min=0

This is the Python script

import os
from skimage import io
from skimage.filters.rank import median
from skimage.morphology import disk


def median_filter(inputfilename):
    folder_script=os.path.dirname(__file__)
    absolute_filename=os.path.join(folder_script,"./in/",inputfilename)

    original= io.imread(absolute_filename, as_gray=True)
    
    median_filtered=median(original, disk(50))  #5, 10, 20,100

    filename_result="median-filter-output.png"
    file_result=os.path.join(folder_script,"./out/",filename_result)
    io.imsave(file_result,median_filtered)

median_filter(inputfilename="NoisySine.png")

The resulting image is always blank (full white). I am sure I am doing something wrong - but not clear where.

Any suggestions?

Thank you,

Sau


Solution

  • Because your image is almost full white the median of such large disk is also white.

    As extreme example, Imagine a disk that has 51% whites pixels and 49% black pixels -> the median will be white. In your case is looks like at least 95% white dominance.

    BTW, it is as_grey and not as_gray (update: was like this but not anymore, see @Juan comment below)