You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
25 lines
1011 B
25 lines
1011 B
import cv2
|
|
from matplotlib import pyplot as plt
|
|
# 载入原图
|
|
img = cv2.imread('D:/Python/ThresholdSegmentation/OtsuThresholding/img/person.jpg', 0)
|
|
|
|
# 全局阈值分割
|
|
ret, img_global = cv2.threshold(img, 127, 255, cv2.THRESH_BINARY)
|
|
|
|
# 大津法
|
|
blur = cv2.GaussianBlur(img, (5, 5), 0)
|
|
ret, img_Otsu = cv2.threshold(blur, 0, 255, cv2.THRESH_BINARY | cv2.THRESH_OTSU)
|
|
|
|
# 显示图片
|
|
images = [img, 0, img_global, blur, 0, img_Otsu]
|
|
titles = ['原始图像', '直方图', '全局阈值分割(127)', '原始图像', '直方图(降噪处理)', 'OTSU分割']
|
|
plt.rcParams['font.sans-serif'] = ['SimHei']
|
|
for i in range(2):
|
|
plt.subplot(2, 3, i*3+1), plt.imshow(images[i*3], 'gray')
|
|
plt.title(titles[i*3]), plt.xticks([]), plt.yticks([])
|
|
plt.subplot(2, 3, i*3+2), plt.hist(images[i*3].ravel(), 256)
|
|
plt.title(titles[i*3+1]), plt.xticks([]), plt.yticks([])
|
|
plt.subplot(2, 3, i*3+3), plt.imshow(images[i*3+2], 'gray')
|
|
plt.title(titles[i*3+2]), plt.xticks([]), plt.yticks([])
|
|
plt.show()
|