如何绘制图像的accumulated historgram
绘制图像的accumulated histogram需要遵循以下步骤:
-
读取图像并将其转换为灰度图像。
-
计算图像的直方图,即计算每个像素值的数量。
-
计算直方图的累积和,即将每个像素值的数量累加到前一个像素值的数量上。
-
将累积和归一化到[0,255]范围内。
-
绘制累积直方图,其中x轴表示像素值,y轴表示像素值在图像中出现的累积次数。
以下是Python代码示例:
import cv2
import numpy as np
import matplotlib.pyplot as plt
# 读取图像并转换为灰度图像
img = cv2.imread('image.jpg')
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# 计算直方图和累积直方图
hist = cv2.calcHist([gray], [0], None, [256], [0, 256])
cumulative_hist = np.cumsum(hist)
# 将累积直方图归一化到[0,255]范围内
cumulative_hist_normalized = cumulative_hist * 255 / cumulative_hist[-1]
# 绘制累积直方图
plt.plot(cumulative_hist_normalized)
plt.xlim([0, 255])
plt.ylim([0, 255])
plt.show()
这将显示图像的累积直方图。
原文地址: https://www.cveoy.top/t/topic/bLPv 著作权归作者所有。请勿转载和采集!