10

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

wilbeibi
  • 3,403
  • 4
  • 25
  • 44

3 Answers3

15

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

Junuxx
  • 14,011
  • 5
  • 41
  • 71
13

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

fmonegaglia
  • 2,749
  • 2
  • 24
  • 34
10

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

pradyunsg
  • 18,287
  • 11
  • 43
  • 96
  • +1 for the demonstration, but it seems strange to use openCV for this; OP asked for numpy/scipy, and [blurring](http://scipy-lectures.github.com/advanced/image_processing/index.html#blurring-smoothing) and thresholding are well within the capabilities of these libraries. – Junuxx Mar 19 '13 at 15:32
  • 1
    @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?? – pradyunsg Mar 19 '13 at 15:34
  • opencv is ok, numpy/scipy would be better. Thank you again – wilbeibi Mar 20 '13 at 01:23