OpenCV python 图片自适应二值化

OpenCV python 图片自适应二值化

原图片[source.jpg]
OpenCV python 图片自适应二值化_第1张图片
代码:

import cv2


def main():
    # 1.导入图片 转换为灰度图片
    img_src = cv2.imread("source.jpg")
    img_gray = cv2.cvtColor(img_src, cv2.COLOR_BGR2GRAY)

    # 2.执行均值自适应
    img_mean = cv2.adaptiveThreshold(img_gray, 255, cv2.ADAPTIVE_THRESH_MEAN_C,
                                     cv2.THRESH_BINARY, 5, 3)
	
	# 3.执行高斯自适应
    img_gauss = cv2.adaptiveThreshold(img_gray, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C,
                                      cv2.THRESH_BINARY, 5, 3)

    # 4.显示结果
    cv2.imshow("img_src", img_src)
    cv2.imshow("img_mean", img_mean)
    cv2.imshow("img_gauss", img_gauss)

    cv2.waitKey()
    cv2.destroyAllWindows()


if __name__ == '__main__':
    main()

结果图片:
[img_gauss.jpg]
OpenCV python 图片自适应二值化_第2张图片

[img_mean.jpg]
OpenCV python 图片自适应二值化_第3张图片

你可能感兴趣的:(Opencv-python)