以下是将灰度图转化成二值图像的 Python 代码:

import cv2

# 读取灰度图像
img_gray = cv2.imread('gray_image.jpg', cv2.IMREAD_GRAYSCALE)

# 将灰度图像转化为二值图像
threshold_value = 127
max_value = 255
_, img_binary = cv2.threshold(img_gray, threshold_value, max_value, cv2.THRESH_BINARY)

# 显示二值图像
cv2.imshow('Binary Image', img_binary)
cv2.waitKey(0)
cv2.destroyAllWindows()

在这个代码中,我们首先读取了灰度图像。然后,我们使用 cv2.threshold() 函数将灰度图像转化为二值图像。该函数将像素值小于阈值的像素设置为 0,将像素值大于等于阈值的像素设置为 255。最后,我们使用 cv2.imshow() 函数显示二值图像。

Python 代码将灰度图像转换为二值图像

原文地址: https://www.cveoy.top/t/topic/m0Ek 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录