python判断图片所处环境是白天或者黑夜
【代码】python判断图片所处环境是白天或者黑夜。
·
python判断图片所处环境是白天或者黑夜
import cv2
import numpy as np
def detect_day_or_night(image_path):
# 读取图片
image = cv2.imread(image_path)
# 将图片转换为HSV颜色空间
hsv_image = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)
# 提取亮度分量
brightness = hsv_image[:,:,2]
# 计算亮度的平均值
avg_brightness = np.mean(brightness)
# 判断是白天还是黑夜,avg_brightness 阈值调整
if avg_brightness > 127:
return '白天'
else:
return '黑夜'
# 调用函数示例
image_path = 'path/to/your/image.jpg'
result = detect_day_or_night(image_path)
print('图片所处环境:', result)
更多推荐
已为社区贡献2条内容
所有评论(0)