Python 12. OpenCV 简单阈值

import cv2
import numpy as np
from matplotlib import pyplot as plt


img = cv2.imread('pic1.PNG')

'''
cv2.imshow('img', img)
cv2.waitKey(0)
cv2.destroyAllWindows()
'''

ret, thresh1 = cv2.threshold(img, 127, 255, cv2.THRESH_BINARY)
ret, thresh2 = cv2.threshold(img, 127, 255, cv2.THRESH_BINARY_INV)
ret, thresh3 = cv2.threshold(img, 127, 255, cv2.THRESH_TRUNC)
ret, thresh4 = cv2.threshold(img, 127, 255, cv2.THRESH_TOZERO)
ret, thresh5 = cv2.threshold(img, 127, 255, cv2.THRESH_TOZERO_INV)

titles = ['Origin', 'Binary', 'Binary_inv', 'TRUNC', 'TOZERO', 'TOZERO_INV']
images = [img, thresh1, thresh2, thresh3, thresh4, thresh5]

for i in range(6):
    plt.subplot(2, 3, i+1), plt.imshow(images[i], 'gray')
    plt.title(titles[i])
    plt.xticks([]), plt.yticks([])

plt.show()

发布了54 篇原创文章 · 获赞 41 · 访问量 7899

猜你喜欢

转载自blog.csdn.net/qq_36071362/article/details/104111489