gpt4 book ai didi

java - 在java中为缓冲图像提供颜色

转载 作者:行者123 更新时间:2023-12-01 22:48:33 24 4
gpt4 key购买 nike

我有一个看起来像这样的缓冲图像。 enter image description here

如何给它上色?我是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;
}

最佳答案

尝试更改计算颜色之间差异的代码,不要将每个颜色 channel 设置为相同并生成灰度,而是尝试将每个颜色 channel 设置为各自的差异,而不是将差异相加在每个颜色 channel 中。换句话说,试试这个:

// 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
}
}
<小时/>

编辑 - 2014 年 8 月 1 日

您提到,如果相应点的两个像素之间存在任何差异,我们应该将此颜色设置为黄色(或紫色/洋红色)。我们可以很容易地做到这一点。只需计算所有 channel 之间的附加差异,如果它不等于 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
}
}

关于java - 在java中为缓冲图像提供颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25029189/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com