java:具有颜色分量和直方图间隔的二维数组

发布于 2024-10-30 22:56:57 字数 2871 浏览 0 评论 0原文

我创建了一种应该从图像绘制直方图的方法...我有一个二维数组:

int[][] myHistogram=new int[colorComponentOfImage][bin256];  

然后我开始读取像素信息并提取颜色,如下所示:

int pixel[]=new int[width*height];
myImage.getRGB(0,0,width,height,pv,0,width);

现在我如何用我获得的颜色填充数组图像???或者我提取了错误的颜色?

提前

谢谢这是代码的其余部分(填充直方图数组的方法):

public void setHistogram(int[][] myHistogram) {
    this.hist = myHistogram;
    for (int i = 0; i < this.bin256; i++) {
        for (int j = 0; j < this.colorcomponents; j++) {
            this.max = (this.max > this.hist[j][i]) ? this.max : this.hist[j][i];
        }
    }
}

这是绘制直方图的方法:

public BufferedImage plotHistogram(int width, int height) {

    BufferedImage image = null;



    if (this.colorcomponents >= 3) {
        /**
         * extended RGB algorithm first channel:red second channel: green
         * third channel: blue fourth channel: the alpha value is being
         * ignored
         */
        image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
        Graphics2D graphics = image.createGraphics();

        Polygon[] poly = new Polygon[3];

        graphics.setColor(Color.white);
        graphics.fillRect(0, 0, width, height);

        /**
         * only first three bands are used
         */
        for (int i = 0; i < 3; i++) {
            poly[i] = new Polygon();
            if (i == RED) {
                graphics.setColor(Color.red);
            }

            else if (i == GREEN) {
                graphics.setColor(Color.green);
            }

            else if (i == BLUE) {
                graphics.setColor(Color.blue);
            }

            float xInterval = (float) width / (float) bins;
            float yInterval = (float) height / (float) max;
            poly[i].addPoint(0, height);

            for (int j = 0; j < bins; j++) {
                int x = (int) ((float) j * xInterval);
                int y = (int) ((float) this.hist[i][j] * yInterval);

                poly[i].addPoint(x, height - y);
            }

            poly[i].addPoint(width, height);
            graphics.fill(poly[i]);
        }

        Area red = new Area(poly[RED]);
        Area green = new Area(poly[GREEN]);
        Area blue = new Area(poly[BLUE]);

        red.intersect(green);
        green.intersect(blue);
        blue.intersect(new Area(poly[0]));

        graphics.setColor(new Color(255, 255, 0));
        graphics.fill(red);

        graphics.setColor(new Color(0, 255, 255));
        graphics.fill(green);

        graphics.setColor(new Color(255, 0, 255));
        graphics.fill(blue);

        graphics.setColor(Color.black);
        blue.intersect(new Area(poly[2]));
        graphics.fill(blue);
    }
    return image;
}

如果我调用plotHistogram,则数组 hist 为空...

i have created a method that should plot a histogram from an image...i have a 2 dimension array:

int[][] myHistogram=new int[colorComponentOfImage][bin256];  

than i started to read pixel information and extract color like this:

int pixel[]=new int[width*height];
myImage.getRGB(0,0,width,height,pv,0,width);

now how can i fill the array with the colors that i get from the image??? or i'm extracting wrong the colors??

thx in advance

p.s. this is the rest of the code(method to fill the Histogram array):

public void setHistogram(int[][] myHistogram) {
    this.hist = myHistogram;
    for (int i = 0; i < this.bin256; i++) {
        for (int j = 0; j < this.colorcomponents; j++) {
            this.max = (this.max > this.hist[j][i]) ? this.max : this.hist[j][i];
        }
    }
}

and this is the method to plot the histogram:

public BufferedImage plotHistogram(int width, int height) {

    BufferedImage image = null;



    if (this.colorcomponents >= 3) {
        /**
         * extended RGB algorithm first channel:red second channel: green
         * third channel: blue fourth channel: the alpha value is being
         * ignored
         */
        image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
        Graphics2D graphics = image.createGraphics();

        Polygon[] poly = new Polygon[3];

        graphics.setColor(Color.white);
        graphics.fillRect(0, 0, width, height);

        /**
         * only first three bands are used
         */
        for (int i = 0; i < 3; i++) {
            poly[i] = new Polygon();
            if (i == RED) {
                graphics.setColor(Color.red);
            }

            else if (i == GREEN) {
                graphics.setColor(Color.green);
            }

            else if (i == BLUE) {
                graphics.setColor(Color.blue);
            }

            float xInterval = (float) width / (float) bins;
            float yInterval = (float) height / (float) max;
            poly[i].addPoint(0, height);

            for (int j = 0; j < bins; j++) {
                int x = (int) ((float) j * xInterval);
                int y = (int) ((float) this.hist[i][j] * yInterval);

                poly[i].addPoint(x, height - y);
            }

            poly[i].addPoint(width, height);
            graphics.fill(poly[i]);
        }

        Area red = new Area(poly[RED]);
        Area green = new Area(poly[GREEN]);
        Area blue = new Area(poly[BLUE]);

        red.intersect(green);
        green.intersect(blue);
        blue.intersect(new Area(poly[0]));

        graphics.setColor(new Color(255, 255, 0));
        graphics.fill(red);

        graphics.setColor(new Color(0, 255, 255));
        graphics.fill(green);

        graphics.setColor(new Color(255, 0, 255));
        graphics.fill(blue);

        graphics.setColor(Color.black);
        blue.intersect(new Area(poly[2]));
        graphics.fill(blue);
    }
    return image;
}

if i call the plotHistogram the array hist is empty...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

暗喜 2024-11-06 22:56:57

老实说,我没有通读你的plotHistogram,但据我了解,myHistogram 将有 3 或 4 个数组,每个数组又包含一个包含 256 个项目的数组。其中每一个都是一个必须用 0 初始化的计数器。

然后定义以下 4 个常量:

final int RED_CHANNEL = 0;
final int BLUE_CHANNEL = 1;
final int GREEN_CHANNEL = 2;
final int ALPHA_CHANNEL = 3;

...
// 然后在每个像素上给红色、绿色、蓝色赋值并增加相应的计数器。

myHistogram[RED_CHANNEL][red]++;
myHistogram[GREEN_CHANNEL][green]++;

等等...

当您处理整个图像时,您的 myHistogram 数组中应该有直方图。

顺便提一句。:
而不是这样做:

  if (i == RED) {
    graphics.setColor(Color.red);
  } else if (i == GREEN) {
    graphics.setColor(Color.green);
  }else if (i == BLUE) {
    graphics.setColor(Color.blue);
  }

我会定义一个颜色数组,例如

`final Color[]plotColours = new Colors[] {Color.RED, Color.GREEN, Color.BLUE};

graphics.setColor(plotColours[i]);

我来自的地方更能做到:

/**
* only first three bands are used
*/
for (int i = 0; i < 3; i++) { ...

Honestly I didn't read through your plotHistogram, but in my understanding myHistogram will have 3 or 4 arrays, each containing one more array with 256 items in it. Each of this is a counter which has to be initialized with 0s.

Then you define the following 4 constants:

final int RED_CHANNEL = 0;
final int BLUE_CHANNEL = 1;
final int GREEN_CHANNEL = 2;
final int ALPHA_CHANNEL = 3;

...
// then on each pixel you give values to to red, green, blue and increment the corresponding counter.

myHistogram[RED_CHANNEL][red]++;
myHistogram[GREEN_CHANNEL][green]++;

etc...

When you processed through the whole image you should have the histogram in your myHistogram array.

Btw.:
instead of doing this:

  if (i == RED) {
    graphics.setColor(Color.red);
  } else if (i == GREEN) {
    graphics.setColor(Color.green);
  }else if (i == BLUE) {
    graphics.setColor(Color.blue);
  }

I would define an array of Colors like

`final Color[] plotColours = new Colors[] {Color.RED, Color.GREEN, Color.BLUE};

and than you can

graphics.setColor(plotColours[i]);

where i is comming from:

/**
* only first three bands are used
*/
for (int i = 0; i < 3; i++) { ...
没有伤那来痛 2024-11-06 22:56:57

好的。据我了解,您的 myImage 是一个 BufferedImage。
BufferedImage 的 Javadoc 在这里很有帮助。
据我了解,此方法返回的整数恰好是宽度*高度数量。每个整数包含有关一个像素的信息。这是可能的,因为它们将(使用 >> 运算符)RGBA 值移入一个 int 值。
因此,要提取精确的 RGBA,您必须执行以下操作:

int alpha = ((rgb >> 24) & 0xff); 
int red = ((rgb >> 16) & 0xff); 
int green = ((rgb >> 8) & 0xff); 
int blue = ((rgb ) & 0xff); 

其中,rgb 是 myImage.getRGB(...) 返回的数组中的一个 int;

也许您应该考虑使用 getRGB(int x, int y) 方法并如上所述处理返回的 int 值。

是我说的太清楚了还是太啰嗦了??? :)

Ok. As I understand your myImage is an BufferedImage.
Javadoc of BufferedImage is helpful here.
As I understand this method returns exactly width*height amount of integers. Each integer contains information about one pixel. It is possible 'cause they shift in (with the >> operator) the RGBA values into one int value.
so to extract the exact RGBA you have to do something like:

int alpha = ((rgb >> 24) & 0xff); 
int red = ((rgb >> 16) & 0xff); 
int green = ((rgb >> 8) & 0xff); 
int blue = ((rgb ) & 0xff); 

where rgb is one int from the array returned by myImage.getRGB(...);

Maybe you should consider using the getRGB(int x, int y) method and process the returned int values as described above.

Is it clear or was I was too verbose??? :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文