我创建了一个方法,应该从图像中绘制直方图...我有一个2维数组:
int[][] myHistogram=new int[colorComponentOfImage][bin256];
比我开始读取像素信息并提取这样的颜色:
int pixel[]=new int[width*height];
myImage.getRGB(0,0,width,height,pv,0,width);
现在如何用我从图像中获得的颜色填充数组?或者我错误地提取颜色??
事先提前P.S。这是代码的其余部分(填充直方图数组的方法):
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为空......
答案 0 :(得分:2)
老实说,我没有读过你的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++) { ...
答案 1 :(得分:0)
确定。据我所知,你的myImage是BufferedImage
。
Javadoc of BufferedImage is helpful here.
据我所知,此方法返回 width * height 整数量。每个整数包含有关一个像素的信息。这可能是因为他们(使用>>
运算符)将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(...)
;
也许您应该考虑使用getRGB(int x,int y)方法并处理返回的int值,如上所述。
是否清楚还是我太冗长了? :)