在处理验证码图片时,出现噪音,如横线、像素点等问题往往会影响识别率,这里给出一个去除噪音的方法,仅供学习。
import cv2
import os
import numpy as np
import copydef del_noise(img, number):height = img.shape[0]width = img.shape[1]img_new = copy.deepcopy(img)for i in range(1, height - 1):for j in range(1, width - 1):point = [[], [], []]count = 0point[0].append(img[i - 1][j - 1])point[0].append(img[i - 1][j])point[0].append(img[i - 1][j + 1])point[1].append(img[i][j - 1])point[1].append(img[i][j])point[1].append(img[i][j + 1])point[2].append(img[i + 1][j - 1])point[2].append(img[i + 1][j])point[2].append(img[i + 1][j + 1])for k in range(3):for z in range(3):if point[k][z] == 0:count += 1if count <= number:img_new[i, j] = 255return img_newif __name__ == '__main__':img_dir = './img'img_name = os.listdir(img_dir) # 列出文件夹下所有的目录与文件kernel = np.ones((5, 5), np.uint8)for i in range(len(img_name)):path = os.path.join(img_dir, img_name[i])image = cv2.imread(path)name_list = list(img_name[i])[:-4]print(name_list)if '.' in name_list:print("%s标签错误,请重新标签!" % img_name[i])else:name = ''.join(name_list)# 灰度化# print(image.shape)grayImage = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)# 二值化result = cv2.adaptiveThreshold(grayImage, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, 21, 1)# 去噪声img = del_noise(result, 2)# img = del_noise(img, 2)# img = del_noise(img, 1)# 加滤波去噪im_temp = cv2.bilateralFilter(src=img, d=6, sigmaColor=75, sigmaSpace=75)im_temp = im_temp[1:-1, 1:-1]im_temp = cv2.copyMakeBorder(im_temp, 13, 13, 13, 13, cv2.BORDER_CONSTANT, value=[255])cv2.imwrite('./img_res/%s.jpg' % (name), im_temp)# im_temp.show()print("%s %s.jpg" % (i, name))print("图片预处理完成!")
处理签前后的对比:
若去除不完全,则可以使用多次del_noise方法,或者是提高del_noise函数number参数的值,但是需要调试才知道那个参数才是比较合适的。