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)
Logo

腾讯云面向开发者汇聚海量精品云计算使用和开发经验,营造开放的云计算技术生态圈。

更多推荐