[英]How to Compare an RGB Value to Color?
I'm scanning through the pixels in a BufferedImage
to see if some of the pixels are a certain color. 我正在扫描
BufferedImage
的像素,看看是否某些像素是某种颜色。 I've tried this by doing the following: 我通过执行以下操作尝试了此操作:
for(int x = 0; x < 16; x++) {
for(int y = 0; y < 16; y++) {
if(image.getRGB(x, y) == new Color(209, 167, 86).getRGB()) System.out.println("Same Color Detected!");
}
}
But image.getRGB()
returns a different value to Color.getRGB()
. 但
image.getRGB()
向Color.getRGB()
返回不同的值。 How can I compare them? 我该如何比较它们?
These are some examples of the values (first digit is from image, second the colour I'm comparing): 这些是值的一些示例(第一个数字来自图像,第二个数字是我正在比较的颜色):
0 : -8060928
-16777216 : -8060928
-3037354 : -8060928
-3037354 : -8060928
-16777216 : -8060928
Here's how I'm getting the image: 这是我得到图像的方式:
playerOrig = ImageIO.read(getClass().getResourceAsStream("/Player/player.gif"));
I'm using Eclipse with Java 1.6 我正在使用Eclipse和Java 1.6
I printed out the ColorModel
of the image and got this: 我打印出图像的
ColorModel
,得到了这个:
IndexColorModel: #pixelBits = 4 numComponents = 4 color space = java.awt.color.ICC_ColorSpace@45d6a56e transparency = 2 transIndex = 11 has alpha = true isAlphaPre = false
I then printed out the ColorSpace
of the Color
object and got this: 然后我打印出
Color
对象的ColorSpace
并得到了这个:
java.awt.color.ICC_ColorSpace@45d6a56e
Here's the image: 这是图像:
NOTE: well I tested the code on my local machine and it works for me just fine as you have it with the default ColorModel
. 注意:我在我的本地机器上测试了代码,它对我来说很合适,因为你使用默认的
ColorModel
。 I am using OSX and Java(TM) SE Runtime Environment (build 1.7.0_25-b15)
Good Luck! 我正在使用OSX和
Java(TM) SE Runtime Environment (build 1.7.0_25-b15)
祝你好运!
import javax.imageio.ImageIO;
import java.awt.Color;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
public class Q23751298
{
public static void main(String[] args)
{
try
{
//final BufferedImage img = ImageIO.read(new File("/Users/jhr/Pictures/fu4FM.gif"));
final BufferedImage img = ImageIO.read(Q23751298.class.getResource("/fu4FM.gif"));
final int match = new Color(209, 167, 86).getRGB();
for (int x = 0; x < img.getWidth(); x++)
{
for (int y = 0; y < img.getHeight(); y++)
{
final int irgb = img.getRGB(x, y);
if (irgb == match)
{
System.out.format("%d/%d = %d : %d\n", x, y, img.getRGB(x, y), match);
}
}
}
}
catch (IOException e)
{
System.out.println(e.getMessage());
}
}
}
Here is the output I get for the 16 X 16
file you posted: 以下是我发布的
16 X 16
文件的输出结果:
4/9 = -3037354 : -3037354
4/10 = -3037354 : -3037354
4/11 = -3037354 : -3037354
6/4 = -3037354 : -3037354
7/4 = -3037354 : -3037354
7/5 = -3037354 : -3037354
7/6 = -3037354 : -3037354
7/7 = -3037354 : -3037354
8/4 = -3037354 : -3037354
8/5 = -3037354 : -3037354
8/6 = -3037354 : -3037354
8/7 = -3037354 : -3037354
9/4 = -3037354 : -3037354
9/7 = -3037354 : -3037354
10/5 = -3037354 : -3037354
10/6 = -3037354 : -3037354
12/10 = -3037354 : -3037354
12/11 = -3037354 : -3037354
Based on the sample image you've supplied, you're only inspecting a 16x16 grid of the image, when the image is 320x320 pixels in size... 根据您提供的样本图像,当图像尺寸为320x320像素时,您只检查图像的16x16网格...
When I update your code to include the full width and height of the image, it works just fine... 当我更新你的代码以包括图像的整个宽度和高度时,它工作得很好......
try {
BufferedImage img = ImageIO.read(...);
Color match = new Color(209, 167, 86);
for (int x = 0; x < img.getWidth(); x++) {
for (int y = 0; y < img.getHeight(); y++) {
if (img.getRGB(x, y) == match.getRGB()) {
System.out.println("Same Color Detected!");
}
}
}
} catch (IOException ex) {
ex.printStackTrace();
}
Updated 更新
Based on the new, smaller, example the code still works... 基于新的,更小的示例代码仍然有效......
To prove it, I wrote a simple color replacement algorithim... 为了证明这一点,我写了一个简单的颜色替换算法......
try {
BufferedImage img = ImageIO.read(new File("8bit.gif"));
BufferedImage replaced = new BufferedImage(img.getWidth(), img.getHeight(), BufferedImage.TYPE_INT_ARGB);
Color match = new Color(209, 167, 86);
Color with = new Color(0, 255, 0);
for (int x = 0; x < img.getWidth(); x++) {
for (int y = 0; y < img.getHeight(); y++) {
int pixel = img.getRGB(x, y);
if (pixel == match.getRGB()) {
System.out.println("Same Color Detected!");
replaced.setRGB(x, y, with.getRGB());
} else {
replaced.setRGB(x, y, pixel);
}
}
}
ImageIO.write(replaced, "png", new File("replaced.png"));
JOptionPane.showMessageDialog(null, new JLabel(new ImageIcon(replaced)));
} catch (IOException ex) {
ex.printStackTrace();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.