Remove points which contains pixels fewer than (N)

11,679

Solution 1

I don't think this is what you want, but this works (uses Opencv (which uses Numpy)):

import cv2

# load image
fname = 'Myimage.jpg'
im = cv2.imread(fname,cv2.COLOR_RGB2GRAY)
# blur image
im = cv2.blur(im,(4,4))
# apply a threshold
im = cv2.threshold(im, 175 , 250, cv2.THRESH_BINARY)
im = im[1]
# show image
cv2.imshow('',im)
cv2.waitKey(0)

Output ( image in a window ):
Output image

You can save the image using cv2.imwrite

Solution 2

Numpy/Scipy can do morphological operations just as well as Matlab can.

See scipy.ndimage.morphology, containing, among other things, binary_opening(), the equivalent of Matlab's bwareaopen().

Solution 3

Numpy/Scipy solution: scipy.ndimage.morphology.binary_opening. More powerful solution: use scikits-image.

from skimage import morphology
cleaned = morphology.remove_small_objects(YOUR_IMAGE, min_size=64, connectivity=2)

See http://scikit-image.org/docs/0.9.x/api/skimage.morphology.html#remove-small-objects

Share:
11,679
wilbeibi
Author by

wilbeibi

Updated on July 21, 2022

Comments

  • wilbeibi
    wilbeibi almost 2 years

    I tried almost all filters in PIL, but failed. Is there any function in numpy of scipy to remove the noise? Like Bwareaopen() in Matlab()?

    e.g:

    enter image description here

    PS: If there is a way to fill the letters into black, I will be grateful

  • Junuxx
    Junuxx about 11 years
    +1 for the demonstration, but it seems strange to use openCV for this; OP asked for numpy/scipy, and blurring and thresholding are well within the capabilities of these libraries.
  • pradyunsg
    pradyunsg about 11 years
    @Junuxx I know, but originally even I said that, but he seems to be OK with it... Also, may I add that link into my answer??
  • wilbeibi
    wilbeibi about 11 years
    opencv is ok, numpy/scipy would be better. Thank you again
  • wilbeibi
    wilbeibi about 11 years
    Thank you, I will have a try.
  • Cris Luengo
    Cris Luengo about 6 years
    binary_opening seems to do a structural opening, which is not the same as an area opening that bwareaopen does.