解决中文显示问题,在import额外加两行
import matplotlib
matplotlib.rc("font", family='YouYuan')
读取单张影像各像素值出现频次
def generate_histogram(image_path):
image = cv2.imread(image_path)
gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
histogram = cv2.calcHist([gray_image], [0], None, [256], [0, 256])
return histogram
显示函数
def plot_histogram(histogram):
plt.plot(histogram)
plt.xlabel('Pixel Value')
plt.ylabel('Frequency')
plt.title('Brightness Histogram')
plt.show()
读取文件夹下所有图片,传入generate_histogram统计像素值
def get_histogram(folder_path):
# folder_path = 'real_B'
total_histogram = np.zeros((256, 1))
for filename in os.listdir(folder_path):
if filename.endswith('.jpg') or filename.endswith('.png'):
image_path = os.path.join(folder_path, filename)
histogram = generate_histogram(image_path)
total_histogram += histogram
return total_histogram
全部代码
# 生成文件夹下所有图片亮度直方图
#
# 开发时间:2023/7/19 18:59
import cv2
import os
import matplotlib.pyplot as plt
import numpy as np
import matplotlib
matplotlib.rc("font", family='YouYuan')
def generate_histogram(image_path):
image = cv2.imread(image_path)
gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
histogram = cv2.calcHist([gray_image], [0], None, [256], [0, 256])
return histogram
def plot_histogram(histogram):
plt.plot(histogram)
plt.xlabel('Pixel Value')
plt.ylabel('Frequency')
plt.title('Brightness Histogram')
plt.show()
def get_histogram(folder_path):
# folder_path = 'real_B'
total_histogram = np.zeros((256, 1))
for filename in os.listdir(folder_path):
if filename.endswith('.jpg') or filename.endswith('.png'):
image_path = os.path.join(folder_path, filename)
histogram = generate_histogram(image_path)
total_histogram += histogram
return total_histogram
y1 = get_histogram("real_A")
y2 = get_histogram("real_B")
y3 = get_histogram("fake_A")
gary = []
for i in range(0, 256):
item = [i]
gary.append(item)
i = i + 1
plt.plot(gary, y1, label='前时相')
plt.plot(gary, y2, '-.', label='后时相')
plt.plot(gary, y3, '--', label='匀色')
plt.legend()
plt.xlabel('像素值')
plt.ylabel('频率')
plt.title('亮度折线图')
plt.show()
# def calculate_brightness(image):
# # 将图像转换为灰度图像
# gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# # 计算灰度图像的平均像素值,即亮度值
# brightness = cv2.mean(gray_image)[0]
# return brightness
#
# def plot_brightness(filepath, linestyle):
# filepaths = []
# brightness_values = []
# for foldername in os.listdir(filepath):
# image_path = os.path.join(filepath, foldername)
#
# if os.path.isfile(image_path) and image_path.endswith(('.jpg', '.jpeg', '.png')):
# # 读取图像
# image = cv2.imread(image_path)
# if image is not None:
# # 计算图像亮度
# brightness = calculate_brightness(image)
# # 保存图像路径和亮度值
# filepaths.append(image_path)
# brightness_values.append(brightness)
# gary = [0, 255]
# # for i in range(0, 255):
# # gary.append(i)
# # i = i + 1
# # 绘制折线图
# plt.plot(brightness_values, 'r-.p')
# plt.xlabel('Image')
# plt.ylabel('Brightness')
# plt.title('Image Brightness')
# plt.xticks(gary)
# plt.show()
#
# if __name__ == '__main__':
#
# # 设置三个文件夹的路径
# folder1_path = 'real_A'
# folder2_path = 'real_B'
# folder3_path = 'fake_A'
#
# # 绘制亮度折线图
# plot_brightness(folder1_path, 'solid') # 使用实线
# plot_brightness(folder2_path, 'dashed') # 使用虚线
# plot_brightness(folder3_path, 'dotted') # 使用点线