我有一个看起来像这样的缓冲图像。
我如何赋予它颜色?我是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;
}
答案 0 :(得分:2)
尝试更改计算颜色之间差异的代码,而不是将每个颜色通道设置为相同并生成灰度,尝试将每个颜色通道设置为各自的差异,而不是将差异加起来在每个颜色通道中。换句话说,试试这个:
// Modified - Changed to int as pixels are ints
int rDiff, gDiff, bDiff;
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;
rDiff = Math.abs(r1 - r2); // Change
gDiff = Math.abs(g1 - g2);
bDiff = Math.abs(b1 - b2);
result = (rDiff << 16) | (gDiff << 8) | bDiff;
outImg.setRGB(j, i, result); // Set result
}
}
您提到如果相应位置的两个像素之间存在任何差异,我们应将此颜色设置为黄色(或紫色/洋红色)。我们可以很容易地做到这一点。只需计算所有通道之间的相加差异,如果它不等于0,则将输出颜色设置为黄色(或紫色/洋红色)。否则,只需保持颜色。如果没有差异,我也会假设你要保持图像1 的像素相同。如果您想保持图像2 中的像素相同,只需更改代码中的变量即可。换句话说:
// Modified - Changed to int as pixels are ints
int 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);
// Check for any differences
if (diff != 0) {
r1 = 255; // If there is, set output to yellow
g1 = 255;
b1 = 0;
/* // Use this for purple / magenta
r1 = 255;
g1 = 0;
b1 = 255; */
}
// If there are no differences, r1, g1 and b1
// will contain the original colours for image 1
// If there is, then r1, g1, b1 will be set to yellow
// Set output pixel
result = (r1 << 16) | (g1 << 8) | b1;
outImg.setRGB(j, i, result); // Set result
}
}
答案 1 :(得分:1)
您可以尝试 this
BufferedImage yourFile =
您可以创建颜色模型(例如,参见链接)
private static ColorModel createColorModel(int n) {
// Create a simple color model with all values mapping to
// a single shade of gray
// nb. this could be improved by reusing the byte arrays
byte[] r = new byte[16];
byte[] g = new byte[16];
byte[] b = new byte[16];
for (int i = 0; i < r.length; i++) {
r[i] = (byte) n;
g[i] = (byte) n;
b[i] = (byte) n;
}
return new IndexColorModel(4, 16, r, g, b);
}