#########################################################
学习函数 cv2.threshold, cv2.adaptiveThreshold等等
简单阈值:
原理很直接,如果像素值大于一个阈值,那么久将它赋值一个数,否则赋值成另外一个数。函数使用cv2.threshold
第一参数是原图像,而且这个图像应该是一个灰度图像。第二个参数用来区分像素的阈值。第三个参数是像素值大于阈值时的上限。opencv提供了不同样式的阈值并且由第四个参数决定。类型如下
cv2.THRESH_BINARY
cv2.THRESH_BINARY_INV
cv2.THRESH_TRUNC
cv2.THRESH_TOZERO
cv2.THRESH_TOZERO_INV
#########################################################
import cv2 import numpy as np from matplotlib import pyplot as plt img = cv2.imread(‘sta.jpg‘,0) 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 = [‘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()
原文地址:https://www.cnblogs.com/bandend/p/10777460.html
时间: 2024-10-11 04:22:52