Author:Xuliu
Time:20211109
Fuction:Histogram of the image illumination,x symbolizes the value of Average_RGB
and y represents the value of Image Numbers
from PIL import Image, ImageStat
import math
import os
import matplotlib.pyplot as plt
import numpy as np
import cv2
def brightness(im_file):
im = Image.open(im_file)
stat = ImageStat.Stat(im)
r, g, b = stat.mean
return math.sqrt(0.241 * (r ** 2) + 0.691 * (g ** 2) + 0.068 * (b ** 2))
def illumination_averge(directory_name):
illumination_averge = []
for filename in os.listdir(directory_name):
# 用os库读取文件夹内所有图片名称以操作
# print(filename)
# img = Image.open(directory_name + "/" + filename)
im_file = directory_name + "/" + filename
illumination_averge.append(brightness(im_file))
return illumination_averge
def draw_hist(directory_name):
illumination_averges = illumination_averge(directory_name)
plt.figure(figsize=(15,5))
nums,bins,patches = plt.hist(illumination_averges,bins=20,edgecolor='k') #bins:区间个数 ,nums:这个参数是指定每个bin(箱子)分布的数据,对应x轴,pathes
plt.xlabel("Illumination")
plt.ylabel("Image Numbers")
# from IPython import embed
# embed()
for num,bin in zip(nums,bins):
plt.annotate(num,xy=(bin,num),xytext=(bin,num))
plt.show()
if __name__ == '__main__':
directory_name = '/home/jy/xl/workstation/Datasets/Car/Cut_Car_Dataset/Car_dataset_Cut'
draw_hist(directory_name)
|