java:具有颜色分量和直方图间隔的二维数组
我创建了一种应该从图像绘制直方图的方法...我有一个二维数组:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
老实说,我没有通读你的plotHistogram,但据我了解,myHistogram 将有 3 或 4 个数组,每个数组又包含一个包含 256 个项目的数组。其中每一个都是一个必须用 0 初始化的计数器。
然后定义以下 4 个常量:
...
// 然后在每个像素上给红色、绿色、蓝色赋值并增加相应的计数器。
等等...
当您处理整个图像时,您的 myHistogram 数组中应该有直方图。
顺便提一句。:
而不是这样做:
我会定义一个颜色数组,例如
`final Color[]plotColours = new Colors[] {Color.RED, Color.GREEN, Color.BLUE};
比
我来自的地方更能做到:
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:
...
// then on each pixel you give values to to red, green, blue and increment the corresponding counter.
etc...
When you processed through the whole image you should have the histogram in your myHistogram array.
Btw.:
instead of doing this:
I would define an array of Colors like
`final Color[] plotColours = new Colors[] {Color.RED, Color.GREEN, Color.BLUE};
and than you can
where i is comming from:
好的。据我了解,您的 myImage 是一个 BufferedImage。
BufferedImage 的 Javadoc 在这里很有帮助。
据我了解,此方法返回的整数恰好是宽度*高度数量。每个整数包含有关一个像素的信息。这是可能的,因为它们将(使用
>>
运算符)RGBA 值移入一个int
值。因此,要提取精确的 RGBA,您必须执行以下操作:
其中,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 oneint
value.so to extract the exact RGBA you have to do something like:
where
rgb
is one int from the array returned bymyImage.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??? :)