Example of python Image Denoising Method

  • 2021-07-13 05:30:53
  • OfStack

Noise may be entrained in the process of image generation, transmission or acquisition, and denoising is a common technique in image processing. Usually, filtering methods are used to remove noise, such as median filtering and mean filtering. However, such an algorithm is not suitable for dealing with long and narrow images such as characters, because the pixels of characters themselves are likely to be removed in the process of filtering.

One uses the method of removing miscellaneous points to remove noise. The specific algorithm is as follows: scan the whole image, when a black point is found, examine the number of black points connected indirectly or directly with the black point. If it is greater than a fixed value, it means that the point is not a discrete point, otherwise it is a discrete point, and remove it. The recursive method is used when examining the connected black dots. Here, I simply use python to achieve, you can refer to the following.


#coding=utf-8
"""
 A miracle of creation QQ2737499951
"""
import cv2
import numpy as np
from matplotlib import pyplot as plt
from PIL import Image,ImageEnhance,ImageFilter
 
img_name = 'test.jpg'
# Remove interference line 
im = Image.open(img_name)
# Image 2 Valuation 
enhancer = ImageEnhance.Contrast(im)
im = enhancer.enhance(2)
im = im.convert('1')
data = im.getdata()
w,h = im.size
#im.show()
black_point = 0
for x in xrange(1,w-1):
  for y in xrange(1,h-1):
    mid_pixel = data[w*y+x] # Pixel value of central pixel point 
    if mid_pixel == 0: # Find up, down, left and right 4 Pixel value of pixel points in each direction 
      top_pixel = data[w*(y-1)+x]
      left_pixel = data[w*y+(x-1)]
      down_pixel = data[w*(y+1)+x]
      right_pixel = data[w*y+(x+1)]
 
      # Judge the total number of black pixels up, down, left and right 
      if top_pixel == 0:
        black_point += 1
      if left_pixel == 0:
        black_point += 1
      if down_pixel == 0:
        black_point += 1
      if right_pixel == 0:
        black_point += 1
      if black_point >= 3:
        im.putpixel((x,y),0)
      #print black_point
      black_point = 0
im.show()

Related articles: