当前位置:   article > 正文

python图片降噪方法汇总以及提取图片验证码_python去除灰度图噪点

python去除灰度图噪点

本文主要将图片降噪的方法进行汇总以及介绍提取图片中简单数字的方法

# 计算图片中值
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
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168

从图片中提取验证码

# 自适应二值化函数
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()
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/IT小白/article/detail/78250
推荐阅读
相关标签
  

闽ICP备14008679号