车牌上下裁剪(去除车牌垂直方向的外部区域以及车牌的锚点)在车牌识别中十分重要,裁剪过后对车牌的后续处理才更加方便。
图1 二值化的车牌图像binary
选取合适的阈值上下裁剪十分重要,根据每行的白色像素点数与阈值的大小关系判断,阈值的选取采用最大类间方差的思想。但是该方法易受噪声影响,求阈值前先进行去噪。每行白色像素点数直方图如图2
图2 垂直方向白色像素点数直方图
从车牌图像中来看,我们期望的阈值应该是能区分250和300部分的,这些小于150的可以看成噪点,可以提前去除
x_histogram_tmp = x_histogram[x_histogram > binary.shape[1]/5*2]
图3 x_histogram_tmp
otsu的代码如下
def otsu(array, scale):
length = len(array)
histogram = np.zeros((scale))
for i in range(length):
histogram[int(array[i])] += 1
histogram /= length
avg = np.sum(np.arange(scale)*histogram)
w = 0
u = 0
max_var = 0
thresholdV = 0
for i in range(scale):
w += histogram[i]
u += i * histogram[i]
t = avg * w - u
variance = t * t / (w * (1 - w) + 0.0001)
if variance > max_var:
max_var = variance
thresholdV = i
return thresholdV
thresholdV = otsu(x_histogram_tmp, binary.shape[1]+1)
求得的阈值为260,最后分割出来的效果为