文章目录
在OCR中,特别是为了提高验证码识别率,对会图片进行预处理。
一般的流程是:
- 转成灰度图,或者对特定图取其中一个通道,
- 高斯滤波去噪
- 进行阈值二值化处理
- 二值图去除噪点
- OCR识别(传统机器学习算法,基于CNN的深度学习算法)
如下写了两个非高斯降噪方法,一个是针对灰度图,一个是针对二值图的。
一、灰度图非高斯降噪
处理一张1920*1080的图片0.107s(CPU=i5-8300H)
#%%cython --compile-args=/openmp --link-args=/openmp
import numpy as np
cimport numpy as np
cimport cython
from cython.parallel import parallel, prange
@cython.boundscheck(False)
@cython.wraparound(False)
cdef int getPixel(unsigned char[:,::1] raw_view, int x, int y, int G, int N) nogil:
cdef int pixel = raw_view[x,y]
cdef int nearDots = 0
cdef bint L
if pixel > G:
L = True
else:
L = False
if L == (raw_view[x - 1, y - 1] > G):
nearDots += 1
if L == (raw_view[x - 1, y] > G):
nearDots += 1
if L == (raw_view[x - 1, y + 1] > G):
nearDots += 1
if L == (raw_view[x, y - 1] > G):
nearDots += 1
if L == (raw_view[x, y + 1] > G):
nearDots += 1
if L == (raw_view[x + 1, y - 1] > G):
nearDots += 1
if L == (raw_view[x + 1, y] > G):
nearDots += 1
if L == (raw_view[x + 1, y + 1] > G):
nearDots += 1
if nearDots < N:
pixel = raw_view[x, y - 1]
else:
pixel = -1
return pixel
@cython.boundscheck(False)
@cython.wraparound(False)
def clearNoise(np.ndarray[np.uint8_t, ndim=2] img_array, int G= 50, int N= 4, int Z= 4):
cdef