windows7 pycharm
opencv3 python3
import cv2 as cvimport numpy as npfont = cv.FONT_HERSHEY_SIMPLEXimage = cv.imread('c:\\book.png')cv.imshow('image', image)加载图片
gray = cv.cvtColor(image, cv.COLOR_BGR2GRAY)retval1, threshold = cv.threshold(gray, 100, 255, cv.THRESH_BINARY)imgfont = cv.putText(threshold, 'threshold: 100,255', (100, 100), font, 1.2, (255, 255, 255), 2)cv.imshow('threshold', threshold)转化成灰度后二值化处理和上图比较:图片都变清晰但是边缘存在彩色阴影或者黑白阴影
adaptiveThresholdMean = cv.adaptiveThreshold(gray,255,cv.ADAPTIVE_THRESH_MEAN_C,cv.THRESH_BINARY,13,9)imgfont = cv.putText(adaptiveThresholdMean, 'adaptiveThresholdMean: 13,9', (100, 100), font, 1.2, (255, 255, 255), 2)cv.imshow('adaptiveThresholdMean', adaptiveThresholdMean)采用自适应平均大法。二值化,窗口大小选13,常数c选择9
adaptiveThresholdGaua = cv.adaptiveThreshold(gray, 255, cv.ADAPTIVE_THRESH_GAUSSIAN_C, cv.THRESH_BINARY, 13, 9)imgfont = cv.putText(adaptiveThresholdGaua, 'adaptiveThresholdGaua: 13,9', (100, 100), font, 1.2, (255, 255, 255), 2)cv.imshow('adaptiveThresholdGaua', adaptiveThresholdGaua)采用自适应高斯大法。二值化,窗口大小选13,常数c选择9
小结:采用adaptiveThreshold函数能够明显改善清晰度,并且边角也比较清晰。注意:但是窗口和常数的选择会影响效果。比如 窗口和常数 13, 1 效果马上变坏。
adaptiveThreshold是比较好的处理方法。注意常数和窗口的选择
adaptiveThreshold选用平均还是高斯 影响不太大