Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove points which contains pixels fewer than (N)

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

like image 890
wilbeibi Avatar asked Mar 19 '13 10:03

wilbeibi


3 Answers

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

like image 109
fmonegaglia Avatar answered Nov 08 '22 22:11

fmonegaglia


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().

like image 15
Junuxx Avatar answered Nov 08 '22 20:11

Junuxx


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

like image 10
pradyunsg Avatar answered Nov 08 '22 20:11

pradyunsg