下面是一个计算图像中白色区域所占百分比的简单代码:
import cv2# 读取图像
img = cv2.imread("image.jpg")
# 获取图像高度和宽度
height, width = img.shape[:2]
# 计算白色像素数量
white_pixels = 0
for row in img:
for pixel in row:
if (pixel == [255, 255, 255]).all():
white_pixels += 1
# 计算白色所占百分比
percentage = (white_pi