python 亮度判断白天夜晚



import cv2
import numpy as np

import sys
import os
import glob
os.chdir(os.path.dirname(os.path.abspath(__file__)))

def is_daytime(image, threshold=120):

    # 将图像转换为灰度图像
    gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

    # 计算图像的平均亮度
    mean_brightness = np.mean(gray_image)

    # 根据亮度阈值判断是否是白天
    if mean_brightness > threshold:
        return 1,mean_brightness
    else:
        return 0,mean_brightness

def is_daytime_hsv(image,threshold=90):
    # 将图像转换为HSV颜色空间,分析亮度和饱和度
    hsv_image = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)
    brightness = hsv_image[:, :, 2]  # V通道表示亮度

    mean_brightness = np.mean(brightness)

    # 根据亮度阈值判断是否是白天
    if mean_brightness > threshold:
        return 1,mean_brightness
    else:
        return 0,mean_brightness
        
if __name__ == "__main__":

    dir_a=r'/lpai/volumes/ad-op-ga/data/box_fen_960'
    dirs=glob.glob(dir_a+'/*')
    day_count=0
    night_count=0
    for dir_m in dirs:
        if os.path.isdir(dir_m):
            dir_ms=glob.glob(dir_m+'/*')
            
            for clip_dir in dir_ms:
                if os.path.isdir(clip_dir):

                    files=glob.glob(clip_dir+'/image/front/*.jpg')
                    img_path=files[0]
                    
                    img=cv2.imread(img_path)

                    # img_crop=img[0:200,0:580]
                    img_crop=img[0:200]
                    result,mean_brightness = is_daytime_hsv(img_crop)
                    if result:
                        day_count+=1
                        # print(day_count,len(dirs),img_path)     
                        if day_count<=50:
                            print(img_path)     
                    else:
                        with open("night_list.txt", 'a') as fr:
                            fr.write(clip_dir+'\n')

                        night_count+=1
                        # print(night_count,int(mean_brightness),len(dirs),img_path,day_count,day_count)                          

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

AI算法网奇

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值