多语言展示
当前在线:191今日阅读:26今日分享:39

通过阈值方法调整图片清晰程度

在图片处理过程中,对阈值处理的方法有了进一步的认识,它不仅仅用于对图片颜色范围的选择还能够提高图片的清晰程度。
工具/原料
1

windows7 pycharm

2

opencv3 python3

方法/步骤
1

import cv2 as cvimport numpy as npfont = cv.FONT_HERSHEY_SIMPLEXimage = cv.imread('c:\\book.png')cv.imshow('image', image)加载图片

3

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)转化成灰度后二值化处理和上图比较:图片都变清晰但是边缘存在彩色阴影或者黑白阴影

4

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

5

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

6

小结:采用adaptiveThreshold函数能够明显改善清晰度,并且边角也比较清晰。注意:但是窗口和常数的选择会影响效果。比如  窗口和常数 13, 1 效果马上变坏。

注意事项
1

adaptiveThreshold是比较好的处理方法。注意常数和窗口的选择

2

adaptiveThreshold选用平均还是高斯 影响不太大

推荐信息