plt亮度折线图

解决中文显示问题,在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')  # 使用点线
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

Yokon_D

您的鼓励将是我最大的动力

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

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

打赏作者

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

抵扣说明:

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

余额充值