Home > Mobile >  How to remove black dots of this image using OpenCV?
How to remove black dots of this image using OpenCV?

Time:10-09

Is it possible to remove those black dots without hampering the image text using OpenCV?

So far, I have tried dilation, morphologyEx, erode etc.

enter image description here

CodePudding user response:

Maybe try to connect the letters to big blobs, and remove small blobs:

img = (cv2.imread(r"your_image.png",cv2.IMREAD_UNCHANGED)==0).astype(np.uint8)

mask=cv2.morphologyEx(img,cv2.MORPH_CLOSE,np.ones((20,40))) # this will connect letters together
out = cv2.connectedComponentsWithStats(mask, 4, cv2.CV_32S) # count pixel in each blob
bad = out[2][:,cv2.CC_STAT_AREA]<2000 %remove small blobs
mask = np.zeros_like(img,dtype=np.uint8)
for i in range(1,out[0]):
    if not bad[i]:
        mask[out[1] == i] = 1
img_clean = img & mask
plt.imshow(1-img_clean,interpolation="none",cmap='gray')

it's not perfect but it's a start, hope it helps. enter image description here

  • Related