Remove points which contains pixels fewer than (N)
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 ):
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
wilbeibi
Updated on July 21, 2022Comments
-
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:
PS: If there is a way to fill the letters into black, I will be grateful
-
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 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 about 11 yearsopencv is ok, numpy/scipy would be better. Thank you again
-
wilbeibi about 11 yearsThank you, I will have a try.
-
Cris Luengo about 6 years
binary_opening
seems to do a structural opening, which is not the same as an area opening thatbwareaopen
does.