zl程序教程

您现在的位置是:首页 >  后端

当前栏目

opencv 自适应阈值

Opencv 适应 阈值
2023-09-14 09:09:30 时间
import cv2
import numpy as np
from matplotlib import pyplot as plt

img = cv2.imread('cc.jpeg',0)
img = cv2.medianBlur(img,5)

ret,th1 = cv2.threshold(img,127,255,cv2.THRESH_BINARY)
th2 = cv2.adaptiveThreshold(img,255,cv2.ADAPTIVE_THRESH_MEAN_C,\
            cv2.THRESH_BINARY,11,2)
th3 = cv2.adaptiveThreshold(img,255,cv2.ADAPTIVE_THRESH_GAUSSIAN_C,\
            cv2.THRESH_BINARY,11,2)

titles = ['Original Image', 'Global Thresholding (v = 127)',
            'Adaptive Mean Thresholding', 'Adaptive Gaussian Thresholding']
images = [img, th1, th2, th3]

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


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

#img = cv2.imread('messi5.jpg',0)
plt.imshow(images[3], cmap = 'gray', interpolation = 'bicubic')
plt.xticks([]), plt.yticks([])  # to hide tick values on X and Y axis
plt.show(

在这里插入图片描述
在这里插入图片描述