是否有计算高度以绘制图像直方图的标准方法?

问题描述 投票:0回答:1

我有这张图

这是一个 256x256 的图像,其中一半图像是黑色的,然后每列是下一个绿色的颜色,所以

  • 第 1 列有 1 个像素 (0, 1, 0)
  • 第 2 列有 2 个像素 (0, 2, 0)
  • 第 3 列有 3 个像素 (0, 3, 0)
  • 第 255 列有 255 个像素 (0, 255, 0)

仅针对绿色计算 256 个 bin,则有 32678 个条目

bin[0]
,1 个在
bin[1]
,2 个在
bin[2]
,等等,255 个条目在
bin[255]

32768, 1, 2, 3, 4, ..... 255

现在我想显示直方图

我的代码是这样的

// pseudo code

// -- fill in the histogram data as though we had scanned the green
//    triangle image above. Since the image is 256x256 and 
//    half of that is black the first bin, bin[0], the bin for
//    green = 0, has a count of 32768 black pixels.
//    Other bins have a count as tall as their column of green
//    pixels since each column has a different value for green
histogram = [32768]
for (i of [1..256])
   histogram[i] = i

// -- now that we have sample histogram data, plot it.

height = 150
ds = createDrawingSurface(256, height)

maxValue = max(histogram)

for (x of [0..256]) 
  v = histogram[x];
  h = v / maxValue * height;  // what should calculation be?
  ds.drawRect(x, height - h, 1, h);

产生这样的结果:

这是有道理的,0 有 32768 个像素,下一个最大的 bin 只有 255 个条目

但是,如果我去检查具有相同图像的其他图形应用程序,它们会显示其他内容

这是Photoshop

这是亲和照片

这是Photopea

他们做了什么计算,以某种方式显示了

bin[0]
bin[255]
之间的不同关系?

graphics histogram
1个回答
0
投票

看起来你缩放了

0.2 * numBins / numPixels

换句话说

numPixels = 256 * 256
numBins   = 256
scale     = 0.2 * numBins / numPixels;

for (x of [0..256]) 
  v = histogram[x];
  h = v * scale * height;
  ds.drawRect(x, height - h, 1, h);

这会产生这个图表

这看起来与其他的相匹配。我在here找到了这个,但我真的很想知道它来自哪里。为什么是 0.2 而不是 0.1、0.5 或 0.7。这个选择是任意的还是有一些数学逻辑?似乎所有图形应用程序都选择了相同的结果,所以很好奇

© www.soinside.com 2019 - 2024. All rights reserved.