图像二值化后送入crnn进行ocr识别实验代码记录

本次处理为记录对文字进行二值化然后送入crnn进行识别测试精度,通过二值化后得到的图像识别率低,因为二值化后图像并不凭证,原图存在很多噪声,导致二值化图很多镂空

import cv2

thresh_value = 127

img = cv2.imread('ocr/test.jpg',0)
method = [cv2.THRESH_BINARY,cv2.THRESH_BINARY_INV,cv2.THRESH_TRUNC,cv2.THRESH_TOZERO,cv2.THRESH_TOZERO_INV]

images = []
for i in range(len(method)):
    ret, thresh = cv2.threshold(img, thresh_value, 255, method[i])
    cv2.imwrite('ocr/%d.jpg'%(i),thresh)
# 下面8图和9图结果差不多,比上面结果好很多
ret,result = cv2.threshold(img, 0, 255, cv2.THRESH_BINARY+cv2.THRESH_OTSU)
cv2.imwrite('ocr/8.jpg',result)
kernel = cv2.getStructuringElement(cv2.MORPH_CROSS, (1, 1))
result = cv2.dilate(result, kernel)
result = cv2.dilate(result, kernel)

cv2.imwrite('ocr/9.jpg',result)

你可能感兴趣的:(opencv)