统计图像对比对、饱和度、亮度代码

import cv2
import os
import numpy as np
path_dir = "/data_1/everyday/20221014/pic/18-19-20/"

list_img = os.listdir(path_dir)
cnt = 0
contrast = 0
brightness = 0
saturation = 0
for name in list_img:
    path_img = path_dir + name
    img = cv2.imread(path_img)
    if img is None:
        continue
    cnt += 1

    print(cnt, "  ", path_img)
    print(path_img)
    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    # means_gray = np.mean(gray)
    means_gray1, var_gray = cv2.meanStdDev(gray)
    hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
    means_hsv = np.mean(hsv)

    contrast += var_gray
    brightness += means_gray1
    saturation += means_hsv

    # print("means_gray=", means_gray)
    print("means_gray1=", means_gray1, "var_gray=",var_gray)
    print("means_hsv=", means_hsv)

    cv2.namedWindow("img", 0)
    cv2.imshow("img",img)
    cv2.namedWindow("gray", 0)
    cv2.imshow("gray",gray)
    cv2.namedWindow("hsv", 0)
    cv2.imshow("hsv", hsv)
    cv2.waitKey(1)
print("===end")
print("contrast = ", contrast * 1.0 / cnt)
print("brightness = ", brightness * 1.0 / cnt)
print("saturation = ", saturation * 1.0 / cnt)

posted @ 2022-10-25 10:44  无左无右  阅读(65)  评论(0编辑  收藏  举报