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)
python 亮度判断白天夜晚
于 2024-09-07 11:39:05 首次发布