OpenCV图像二值化,Python

OpenCV图像二值化,Python

原图:

OpenCV图像二值化,Python_第1张图片

 

二值化处理(只有黑和白):

import cv2

if __name__=="__main__":
    image = cv2.imread("pic.jpg")
    gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

    #超过阈值thresh,值为255,未超过为0。
    ret1, p1 = cv2.threshold(src=gray, thresh=127, maxval=255, type=cv2.THRESH_BINARY)
    print(ret1)

    ret2, p2 = cv2.threshold(src=gray, thresh=127, maxval=255, type=cv2.THRESH_BINARY_INV)
    print(ret2)

    cv2.imshow('p1', p1)
    cv2.imshow('p2', p2)
    cv2.waitKey(0)
    cv2.destroyAllWindows()

 

结果:

OpenCV图像二值化,Python_第2张图片

 

你可能感兴趣的:(计算机视觉,OpenCV,计算机图形图像,opencv,python,计算机视觉)