python实现 opencv 学习笔记 图像二值化

python实现 opencv 学习笔记 图像二值化_第1张图片

import cv2 as cv


def threshold_image(image):
    gray = cv.cvtColor(image, cv.COLOR_BGR2GRAY)
    cv.imshow("yuantu", gray)
    ret, binary = cv.threshold(gray, 0, 255, cv.THRESH_BINARY | cv.THRESH_OTSU)
    print(ret)
    cv.imshow("OTSU", binary)
    ret, binary = cv.threshold(gray, 0, 255, cv.THRESH_BINARY | cv.THRESH_TRIANGLE)
    print(ret)
    cv.imshow("TRIANGLE", binary)
    ret, binary = cv.threshold(gray, 150, 255, cv.THRESH_BINARY)
    print(ret)
    cv.imshow("my", binary)
    ret, binary = cv.threshold(gray, 150, 255, cv.THRESH_BINARY_INV)
    print(ret)
    cv.imshow("my2", binary)
    ret, binary = cv.threshold(gray, 0, 255, cv.THRESH_TRUNC)
    print(ret)
    cv.imshow("jieduan", binary)
    ret, binary = cv.threshold(gray, 0, 255, cv.THRESH_TOZERO)
    print(ret)
    cv.imshow("JIEDUAN2", binary)


img = cv.imread("D://work//ycy.jpg")
threshold_image(img)
cv.waitKey(0)
cv.destroyAllWindows()

 

你可能感兴趣的:(python实现 opencv 学习笔记 图像二值化)