2

how to remove the background of a image which contains many noises and lines etc

[sample image][1]
import cv2
from PIL import Image
image = cv2.imread("1.jpg") #input image
image = cv2.fastNlMeansDenoisingColored(image,None,10,10,7,21)
gray = cv2.cvtColor(image,cv2.COLOR_BGR2GRAY) #to gray scale
res,thresh = cv2.threshold(gray,150,255,cv2.THRESH_BINARY_INV) #to binary using threshold
kernel = cv2.getStructuringElement(cv2.MORPH_CROSS,(3,3)) 
dilated = cv2.dilate(thresh,kernel,iterations = 5) #dilation operation

is there any other preprocessing operations for removing noises

_,contours, hierarchy = cv2.findContours(dilated,cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_NONE) #finding contours
print(1)
coord = []
#contours greater than or less than specified size is removed
for contour in contours:  
      [x,y,w,h] = cv2.boundingRect(contour)   


      if h>300 and w>300:   
          continue   
      if h<100 or w<100:   
          continue  
      coord.append((x,y,w,h)) 
count = 0
for cor in coord:
        [x,y,w,h] = cor
        t = image[y:y+h,x:x+w,:].astype('uint8')
      cv2.adaptiveThreshold(image,255,cv2.ADAPTIVE_THRESH_GAUSSIAN_C,cv2.THRESH_BINARY,11,2)
        plt.imshow(cv2.resize(t, (150, 150)))
        plt.show()
        count=count+1
print("number of char in image:", count)

is there any way to remove the noises,please help

enter image description here

enter image description here

Mathieu
  • 5,410
  • 6
  • 28
  • 55
Milan KD
  • 21
  • 1

0 Answers0