首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何将RGB值与颜色进行比较?

如何将RGB值与颜色进行比较?
EN

Stack Overflow用户
提问于 2014-05-20 04:57:00
回答 2查看 2.6K关注 0票数 0

我正在扫描BufferedImage中的像素,看看其中一些像素是否是特定的颜色。我尝试过这样做:

代码语言:javascript
运行
复制
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!");
    }
}

但是image.getRGB()将一个不同的值返回给Color.getRGB()。我怎么比较他们呢?

以下是一些数值的例子(第一个数字来自图像,第二个是我正在比较的颜色):

代码语言:javascript
运行
复制
0 : -8060928
-16777216 : -8060928
-3037354 : -8060928
-3037354 : -8060928
-16777216 : -8060928

以下是我如何获得图像的方法:

代码语言:javascript
运行
复制
playerOrig = ImageIO.read(getClass().getResourceAsStream("/Player/player.gif"));

我在Java 1.6中使用Eclipse

我打印出了图像的ColorModel,得到了以下内容:

代码语言:javascript
运行
复制
IndexColorModel: #pixelBits = 4 numComponents = 4 color space = java.awt.color.ICC_ColorSpace@45d6a56e transparency = 2 transIndex   = 11 has alpha = true isAlphaPre = false

然后,我打印出了ColorSpaceColor对象,并得到如下:

代码语言:javascript
运行
复制
java.awt.color.ICC_ColorSpace@45d6a56e

下面是图片:

EN

回答 2

Stack Overflow用户

发布于 2014-05-20 05:14:05

为我工作..。

注意:我在本地机器上测试了代码,它对我很好,就像使用默认的ColorModel__一样。我正在使用OSX和Java(TM) SE Runtime Environment (build 1.7.0_25-b15)祝您好运!

这是一个链接到我在GitHub.com上的整个Mavenized项目

代码语言:javascript
运行
复制
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());
        }
    }
}

下面是您发布的16 X 16文件的输出:

代码语言:javascript
运行
复制
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
票数 1
EN

Stack Overflow用户

发布于 2014-05-20 05:18:24

根据您提供的示例图像,您只检查图像的16x16网格,当图像大小为320x320像素时.

当我更新您的代码以包括图像的全部宽度和高度时,它工作得很好.

代码语言:javascript
运行
复制
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();
}

更新的

基于新的,更小的,代码仍然有效.

为了证明这一点,我写了一个简单的颜色替换算法。

代码语言:javascript
运行
复制
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();
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/23751298

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档