java - 创建直方图

标签 java arrays

对于一项作业,我收到了我无法遵循的可怕指示,而且我的教授处理电子邮件的速度很慢,所以我想我会在这里得到更好的回复。

最初的目标是使用公式 {r,g} = {R,G}/(R+G+B) 将正常颜色 RGB 转换为色度。然后我们要创建一个以 r 和 g 为轴的直方图。 bin 的数量应等于1 + log2(n),其中 n = 行数

这怎么可能?假设我们有一个 4x4 的数组,我们必须将其创建为 3x3 矩阵的直方图,这不会均匀划分,我们会遇到问题...

以下是项目规范:

It turns out that we also know that if we replace the colour, RGB, by the chromaticity {r, g} = {R, G}/(R + G + B) then the image is much more characteristic of the surfaces being imaged, rather than of the light illuminating those surfaces. So, instead of colour, {R, G, B}, let’s use chromaticity, {r, g}. [But watch out for black, i.e., {R, G, B}={0, 0, 0}, pixels.]

The nice thing about chromaticity is that it’s 2D. So our histogram is a 2D array, with r along one axis and g along another. The chromaticity is necessarily in the interval [0, 1]. But how many bins along each axis should we use? Applying (a cheap version of) a rule of thumb called Sturges’s Rule, the number of bins N = 1 + log2(n), where n=size of data, so a rough idea is to use n = number of rows, so e.g. N = 7 bins along each of the r and g directions. That makes our histogram, H, a small, 7 × 7 array of integer values. If we normalize to make the sum of H equal unity, then we have an array of floats.

要求任何人做这项工作,我只是要求澄清是否有人知道如何做到这一点。

目前我可以获得色度值:

       for(int i= 0; i < 1280; i++) {
            for(int j = 0; j < 720; j++) {
                rgb = resTemp.getRGB(i, j);

                R = (rgb >> 16) & 0xFF;
                G = (rgb >> 8) & 0xFF;
                B = (rgb) & 0xFF;

                r = R/(R+G+B);
                g = G/(R+G+B);
                b = B/(R+G+B);

            }
        }

最佳答案

您的 2D 色度值在 0..1、0..1 范围内

r = R/(R+G+B);
g = G/(R+G+B);

这意味着对于每个像素,您都有一个 2d 值,您可以将其放置在这个连续空间中的某个位置

^ R
|(1,0)
|
|(0,0)    (0,1) G
|----------->

如果您以 2x2 的方式对其进行分类,您会将每个分类分配到类似的分类

R
^
|-------|------|
|  bin  | bin  |
|       |      |
|-------|------|
|  bin  | bin  |
|       |      |
|-------|------|>G

例如值 [0.3, 0.2] 位于左下角。 [0.9, 0.9] 进入右上角。您也可以将其制作成更精细的 7x7 网格。

现在,您将迭代所有 [r,g] 值并计算有多少值进入哪个 bin。结果可能是这样的

  30  |  12
 ------------
  4   |  12

这是直方图 H。

现在您可以通过将值转换为百分比来标准化它。这是通过除以总数来完成的。例如。 30/(30+12+4+12) 为第一个。最终结果是一个 float 组。

  0.5 |  0.2
 ------------
  0.0 |  0.2

关于java - 创建直方图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33839019/

相关文章:

java - JSqlParser 如何拆分一个表达式

java - 如何将 String 转换为 int 并使用 Comparator 进行比较

java - 使用 java 驱动程序提高 Mongodb 的性能

java - 多态性能做什么而继承不能?

arrays - Swift:如何在 TableView 内的 CollectionView 中显示解析的 JSON 数据?

python - 在numpy矩阵中获取每行的多数元素

arrays - 如何将 json 数组转换为文本数组?

javascript - Js Number.parseInt 函数中的一些奇怪行为,有人可以解释一下吗?

java - 类文件名中的 $1 是什么?

java - 方法重载练习