【发布时间】:2014-07-30 05:10:08
【问题描述】:
我有一个看起来像这样的缓冲图像。
如何给它上色?我是java图像处理的新手,任何帮助将不胜感激。这是生成此图片的代码示例。
public BufferedImage getDifferenceImage(BufferedImage img1, BufferedImage img2) {
int width1 = img1.getWidth(); // Change - getWidth() and getHeight() for BufferedImage
int width2 = img2.getWidth(); // take no arguments
int height1 = img1.getHeight();
int height2 = img2.getHeight();
if ((width1 != width2) || (height1 != height2)) {
System.err.println("Error: Images dimensions mismatch");
System.exit(1);
}
// NEW - Create output Buffered image of type RGB
BufferedImage outImg = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
// Modified
long diff;
int result; // Stores output pixel
for (int i = 0; i < height1; i++) {
for (int j = 0; j < width1; j++) {
int rgb1 = img1.getRGB(j, i);
int rgb2 = img2.getRGB(j, i);
int r1 = (rgb1 >> 16) & 0xff;
int g1 = (rgb1 >> 8) & 0xff;
int b1 = (rgb1) & 0xff;
int r2 = (rgb2 >> 16) & 0xff;
int g2 = (rgb2 >> 8) & 0xff;
int b2 = (rgb2) & 0xff;
diff = Math.abs(r1 - r2); // Change
diff += Math.abs(g1 - g2);
diff += Math.abs(b1 - b2);
diff /= 255; // Change - Ensure result is between 0 - 255
// Make the difference image gray scale
// The RGB components are all the same
result = (diff << 16) | (diff << 8) | diff;
outImg.setRGB(j, i, result); // Set result
}
}
// Now return
return outImg;
}
【问题讨论】:
-
原版是什么样子的?你如何创建/加载它?
-
您是从文件加载图像还是以编程方式生成图像(或两者兼而有之)?该图像是否具有透明背景,还是如您发布的图像中所示的字面意思?您需要处理图像本身还是设置背景颜色然后在其上绘制图像就足够了?
-
我试图找出两张图片之间的区别。这是试图突出这种差异的结果图像。
-
您能否详细说明图像之间的“差异”是什么意思?另外,您究竟想如何为该图像添加颜色?您想用颜色突出显示其中的一部分(这将涉及用具有 alpha 值的颜色在图像的一部分上绘制)?
-
是的。我只是想让它可见。太黑了。
标签: java image image-processing bufferedimage pixels