开发者

java:2 dimensional array with color components and histogram interval

开发者 https://www.devze.com 2023-02-22 06:44 出处:网络
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];

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...


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++) { ...


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??? :)

0

精彩评论

暂无评论...
验证码 换一张
取 消