【问题标题】:How can I make histogram without using numpy histogram如何在不使用 numpy histogram 的情况下制作直方图
【发布时间】:2021-04-13 15:28:25
【问题描述】:
import cv2
import numpy as np
import matplotlib.pyplot as plt

def my_histogram(img, bins, range):
    hist = 
    return hist

我需要在不使用 np.histogram 或其他直方图函数的情况下填写 hist

img = cv2.imread('sample.jpg')
img_g = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
hist = my_histogram(img_g, 256, [0,256])

plt.plot(hist)
plt.xlim([0,256])
plt.show()

【问题讨论】:

    标签: python image function numpy histogram


    【解决方案1】:

    np.unique 是一种效率较低的直方图实用程序,因为它对数据进行排序而不是直接处理:

    bins, hist = np.unique(img.ravel(), return_counts=True)
    

    【讨论】:

      【解决方案2】:

      我不会为您提供确切的答案,因为这看起来像是一个家庭作业。基本上,灰度图像中的每个像素都有一个与之关联的数字,范围从 0 到 255。您要做的是对具有相同值的像素数求和(有多少值为 0,有多少值为 1 , 有多少个值为 2 等)。您可能可以使用 numpy 来帮助您,而无需调用 histogram 函数(例如,看看这个link)。

      然后您可以使用 matplotlib 绘制条形图,以显示您的直方图。

      【讨论】:

        猜你喜欢
        • 2017-12-26
        • 2015-05-16
        • 2013-06-25
        • 1970-01-01
        • 1970-01-01
        • 2018-09-23
        • 1970-01-01
        • 2016-03-18
        • 2021-02-03
        相关资源
        最近更新 更多