赞
踩
本文主要将图片降噪的方法进行汇总以及介绍提取图片中简单数字的方法
# 计算图片中值 def median(img, x, y): L = [] xl = [x - 1, x, x + 1] yl = [y - 1, y, y + 1] for i in xl: for j in yl: gray = img.getpixel((i, j)) # 取出灰度值 L.append(gray) L.sort() c = L[4] return c # 中值滤波图片降噪 """ :param path1: 带噪声的图像 :param path2: 降噪后的图像 """ def denoise(path1, path2): img1 = Image.open(path1) # 图像1 img1 = img1.convert('L') # 将图像1转换为灰度图 w, h = img1.size img2 = Image.new('L', (w, h), 'white') # 图像2 for x in range(1, w - 1): for y in range(1, h - 1): c = median(img1, x, y) # 求中值 img2.putpixel((x, y), c) # 将灰度设置为中值 img2.save(path2) # 去除线噪点 def interference_line(img, img_name): filename = img_name.split('.')[0] + '-interferenceline.jpg' h, w = img.shape[:2] for y in range(1, w - 1): for x in range(1, h - 1): count = 0 if img[x, y - 1][0] > 245: count = count + 1 if img[x, y + 1][0] > 245: count = count + 1 if img[x - 1, y][0] > 245: count = count + 1 if img[x + 1, y][0] > 245: count = count + 1 if count > 3: img[x, y][0] = 255 img[x, y][1] = 255 img[x, y][2] = 255 cv.imwrite(filename, img) return img # 去除点噪点 def interference_point(img, img_name, x=0, y=0): """ 9邻域框,以当前点为中心的田字框,黑点个数 :param img: :param img_name: :param x: :param y: :return: """ filename = img_name.split('.')[0] + '-interferencePoint.jpg' cur_pixel = img[x, y][0] height, width = img.shape[:2] for y in range(0, width - 1): for x in range(0, height - 1): if y == 0: if x == 0: sum = int(cur_pixel) \ + int(img[x, y + 1][0]) \ + int(img[x + 1, y][0]) \ + int(img[x + 1, y + 1][0]) if sum <= 2 * 245: img[x, y][0] = 0 img[x, y][1] = 0 img[x, y][2] = 0 elif x == (height - 1): # 右上顶点 sum = int(cur_pixel) \ + int(img[x, y + 1][0]) \ + int(img[x - 1, y][0]) \ + int(img[x - 1, y + 1][0]) if sum <= 2 * 245: img[x, y][0] = 0 img[x, y][1] = 0 img[x, y][2] = 0 else: sum = int(img[x - 1, y][0]) \ + int(img[x - 1, y + 1][0]) \ + int(cur_pixel) \ + int(img[x, y + 1][0]) \ + int(img[x + 1, y][0]) \ + int(img[x + 1, y + 1][0]) if sum <= 3 * 245: img[x, y][0] = 0 img[x, y][1] = 0 img[x, y][2] = 0 elif y == width - 1: if x == 0: # 左下顶点 sum = int(cur_pixel) \ + int(img[x + 1, y][0]) \ + int(img[x + 1, y - 1][0]) \ + int(img[x, y - 1][0]) if sum <= 2 * 245: img[x, y][0] = 0 img[x, y][1] = 0 img[x, y][2] = 0 elif x == height - 1: sum = int(cur_pixel) \ + int(img[x, y - 1][0]) \ + int(img[x - 1, y][0]) \ + int(img[x - 1, y - 1][0]) if sum <= 2 * 245: img[x, y][0] = 0 img[x, y][1] = 0 img[x, y][2] = 0 else: sum = int(cur_pixel) \ + int(img[x - 1, y][0]) \ + int(img[x + 1, y][0]) \ + int(img[x, y - 1][0]) \ + int(img[x - 1, y - 1][0]) \ + int(img[x + 1, y - 1][0]) if sum <= 3 * 245: img[x, y][0] = 0 img[x, y][1] = 0 img[x, y][2] = 0 else: # 不在边界 if x == 0: # 左边非顶点 sum = int(img[x, y - 1][0]) \ + int(cur_pixel) \ + int(img[x, y + 1][0]) \ + int(img[x + 1, y - 1][0]) \ + int(img[x + 1, y][0]) \ + int(img[x + 1, y + 1][0]) if sum <= 3 * 245: img[x, y][0] = 0 img[x, y][1] = 0 img[x, y][2] = 0 elif x == height - 1: # 右边非顶点 sum = int(img[x, y - 1][0]) \ + int(cur_pixel) \ + int(img[x, y + 1][0]) \ + int(img[x - 1, y - 1][0]) \ + int(img[x - 1, y][0]) \ + int(img[x - 1, y + 1][0]) if sum <= 3 * 245: img[x, y][0] = 0 img[x, y][1] = 0 img[x, y][2] = 0 else: sum = int(img[x - 1, y - 1][0]) \ + int(img[x - 1, y][0]) \ + int(img[x - 1, y + 1][0]) \ + int(img[x, y - 1][0]) \ + int(cur_pixel) \ + int(img[x, y + 1][0]) \ + int(img[x + 1, y - 1][0]) \ + int(img[x + 1, y][0]) \ + int(img[x + 1, y + 1][0]) if sum <= 4 * 245: img[x, y][0] = 0 img[x, y][1] = 0 img[x, y][2] = 0 cv.imwrite(filename, img) return img
从图片中提取验证码
# 自适应二值化函数 def get_dynamic_binary_image(filedir, img_name): # filedir:二值化后的图片保存路径 # img_name:图片名 filename = img_name.split('.')[0] + '-binary.png' img_name = filedir + '\\' + img_name im = cv.imread(img_name) im = cv.cvtColor(im, cv.COLOR_BGR2GRAY) # 灰值化 # 二值化 th1 = cv.adaptiveThreshold(im, 255, cv.ADAPTIVE_THRESH_GAUSSIAN_C, cv.THRESH_BINARY, 21, 1) cv.imwrite(filename, th1) return th1 # 取图片 def recognize_text(image): dst = cv.pyrMeanShiftFiltering(image, sp=10, sr=150) gray = cv.cvtColor(dst, cv.COLOR_BGR2GRAY) ret, binary = cv.threshold(gray, 0, 255, cv.THRESH_BINARY_INV | cv.THRESH_OTSU) erode = cv.erode(binary, None, iterations=2) dilate = cv.dilate(erode, None, iterations=1) cv.imshow('dilate', dilate) cv.bitwise_not(dilate, dilate) cv.imshow("binary-image", dilate) test_message = Image.fromarray(dilate) text = pytesseract.image_to_string(test_message) print(f'识别结果为: {text}') src = cv.imread(r'temp.jpg') recognize_text(src) cv.waitKey(0) cv.destroyWindow()
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。