java:具有颜色分量和直方图间隔的二维数组
我创建了一种方法,可以从图像中绘制直方图……我有一个二维数组:java:具有颜色分量和直方图间隔的二维数组,java,arrays,2d,bufferedimage,Java,Arrays,2d,Bufferedimage,我创建了一种方法,可以从图像中绘制直方图……我有一个二维数组: int[][] myHistogram=new int[colorComponentOfImage][bin256]; 然后我开始阅读像素信息并提取颜色,如下所示: int pixel[]=new int[width*height]; myImage.getRGB(0,0,width,height,pv,0,width); 现在我如何用从图像中获得的颜色填充数组???或者我提取错误的颜色 提前thx p、 这是代码的其余部分
int[][] myHistogram=new int[colorComponentOfImage][bin256];
然后我开始阅读像素信息并提取颜色,如下所示:
int pixel[]=new int[width*height];
myImage.getRGB(0,0,width,height,pv,0,width);
现在我如何用从图像中获得的颜色填充数组???或者我提取错误的颜色
提前thx
p、 这是代码的其余部分(填充直方图数组的方法):
public void setHistogram(int[]myHistogram){
this.hist=myHistogram;
for(int i=0;ithis.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;
}
public buffereImage Plot直方图(整数宽度、整数高度){
BuffereImage图像=空;
如果(this.colorcomponents>=3){
/**
*扩展RGB算法第一通道:红色第二通道:绿色
*第三通道:蓝色第四通道:alpha值为
*忽略
*/
图像=新的BuffereImage(宽度、高度、BuffereImage.TYPE_INT_RGB);
Graphics2D graphics=image.createGraphics();
多边形[]多边形=新多边形[3];
图形.设置颜色(颜色.白色);
fillRect(0,0,宽度,高度);
/**
*仅使用前三个波段
*/
对于(int i=0;i<3;i++){
poly[i]=新多边形();
如果(i==红色){
图形.设置颜色(颜色.红色);
}
否则如果(i==绿色){
图形.设置颜色(颜色.绿色);
}
else if(i==蓝色){
图形。设置颜色(颜色。蓝色);
}
浮动xInterval=(浮动)宽度/(浮动)存储箱;
浮动yInterval=(浮动)高度/(浮动)最大值;
多边形[i]。添加点(0,高度);
对于(int j=0;j
如果调用plotHistogram,则数组hist为空…确定。据我所知,您的myImage是一个
buffereImage
。
据我所知,该方法精确地返回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(…)
返回的数组中的一个int
也许您应该考虑使用GeTrGB(int x,int y)方法并处理返回的int值,如上所述。< /P>
是清楚还是我太啰嗦了??:) 老实说,我没有通读您的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);
}
我会定义一组颜色,比如
`最终颜色[]绘图颜色=新颜色[]{Color.RED,Color.GREEN,Color.BLUE}
比你能做的还要多
graphics.setColor(plotColours[i]);
我来自哪里:
/**
* only first three bands are used
*/
for (int i = 0; i < 3; i++) { ...
/**
*仅使用前三个波段
*/
对于(inti=0;i<3;i++){。。。
你想填充哪个数组,颜色是什么格式?@justkt我编辑了第一篇文章…请参阅我的setHistogram方法)我所知道的是,我应该首先用myHistogram作为参数调用setHistogram,然后调用plotHistogram。如果我按照你告诉我的那样提取颜色,我如何将它们保存在myHistogram[][]??thxthx用于帮助…因此首先我应该创建一个循环并初始化两个数组中的元素,使用空值,如myHistogram[i][j]=0??如何从第一个像素开始,然后以最后一个像素结束,这样我就可以为每个像素赋予颜色?我是否应该将宽度和高度相乘,然后获得像素总数???thx
/**
* only first three bands are used
*/
for (int i = 0; i < 3; i++) { ...