一、代码部分
代码如下(示例):
#图像阈值
import cv2 as cv#opencv BGR
import matplotlib.pyplot as plt #包导入
import numpy as np
img=cv.imread('C:/Users/akaak/Pictures/OpenCV/cat.png')
img_gray=cv.imread('C:/Users/akaak/Pictures/OpenCV/cat.png',cv.IMREAD_GRAYSCALE)
ret, thresh1 = cv.threshold(img_gray, 127, 255, cv.THRESH_BINARY)
ret, thresh2 = cv.threshold(img_gray, 127, 255, cv.THRESH_BINARY_INV)
ret, thresh3 = cv.threshold(img_gray, 127, 255, cv.THRESH_TRUNC)
ret, thresh4 = cv.threshold(img_gray, 127, 255, cv.THRESH_TOZERO)
ret, thresh5 = cv.threshold(img_gray, 127, 255, cv.THRESH_TOZERO_INV)
titles = ['Original Image', '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()
二、运行结果
总结
学习了图像阈值处理。