如何比较RGB值和颜色? [英] How to Compare an RGB Value to Color?
问题描述
我正在扫描 BufferedImage
中的像素,以查看某些像素是否是特定颜色。我已经尝试这样做了以下:
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 out.println(Same Color Detected!);
}
}
但 image.getRGB )
向 Color.getRGB()
返回不同的值。
这些是值的一些示例(第一个数字来自图片,第二个是我比较的颜色):
0:-8060928
-16777216:-8060928
-3037354:-8060928
-3037354:-8060928
-16777216:-8060928
以下是我获取图片的方式:
playerOrig = ImageIO.read(getClass()。getResourceAsStream(/ Player / player.gif));
我使用Eclipse和Java 1.6
我打印出图像的 ColorModel
,并得到:
IndexColorModel:#pixelBits = 4 numComponents = 4 color space = java.awt.color.ICC_ColorSpace@45d6a56e transparency = 2 transIndex = 11 has alpha = true isAlphaPre = false
然后我打印出 Color
对象的 ColorSpace
并得到了这个:
java.awt.color.ICC_ColorSpace@45d6a56e
pre>
下面的图片:
解决方案
注意:我测试了我的本地机器上的代码,它对我来说很好,因为你有默认的
ColorModel
。我使用OSX和Java(TM)SE运行环境(版本1.7.0_25-b15)
好运!
这是一个链接到我在GitHub.com上的整个Mavenized项目
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 {
for(int y = 0; y {
final int irgb = img.getRGB(x,y);
if(irgb == match)
{
System.out.format(%d /%d =%d:%d\\\
,x,y,img.getRGB (x,y),match);
}
}
}
}
catch(IOException e)
{
System.out.println(e.getMessage ;
}
}
}
为您发布的
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
I'm scanning through the pixels in a
BufferedImage
to see if some of the pixels are a certain color. 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 toColor.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
I printed out the
ColorModel
of the image and got this: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 theColor
object and got this:java.awt.color.ICC_ColorSpace@45d6a56e
Here's the image:
解决方案Works for me ...
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
. I am using OSX andJava(TM) SE Runtime Environment (build 1.7.0_25-b15)
Good Luck!Here is a link to my entire Mavenized project on GitHub.com
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: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
这篇关于如何比较RGB值和颜色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!