Python程序
# -*- coding: utf-8 -*-
"""
Created on Sat Feb 17 21:26:05 2024
@author: 李立宗
公众号:计算机视觉之光
知识星球:计算机视觉之光
"""
import cv2
# 读取图像
image = cv2.imread('lena.bmp', cv2.IMREAD_GRAYSCALE)
# 应用全局阈值处理
ret, binary_thresh = cv2.threshold(image, 127, 255, cv2.THRESH_BINARY)
# 应用自适应阈值处理
adaptive_thresh = cv2.adaptiveThreshold(image, 255, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY, 11, 2)
# 显示原始图像,全局阈值处理结果和自适应阈值处理结果
cv2.imshow('Original Image', image)
cv2.imshow('Binary Threshold', binary_thresh)
cv2.imshow('Adaptive Threshold', adaptive_thresh)
cv2.waitKey(0)
cv2.destroyAllWindows()
输出结果