如何在Java中找到颜色的不同色调?

如何在Java中找到颜色的不同色调?,java,colors,image-manipulation,bufferedimage,Java,Colors,Image Manipulation,Bufferedimage,如果我有一个数字的RBG代码,例如-16777216(黑色),我如何使用此颜色代码找到其他类似的黑色色调 我试图通过将所有非-16777216的像素标记为白色,将图像转换为单色。然而,通常会发现不同颜色的黑色,但它们会丢失,因为它们不完全匹配 编辑:我有点麻烦。当我尝试使用此颜色查找黑色阴影时,我可以在将其他像素转换为白色时忽略它们,这是我的结果: 资料来源: 结果: 代码: 封装测试; 导入java.awt.Color; 导入java.awt.image.buffereImage; 导入j

如果我有一个数字的RBG代码,例如
-16777216
(黑色),我如何使用此颜色代码找到其他类似的黑色色调

我试图通过将所有非
-16777216
的像素标记为白色,将图像转换为单色。然而,通常会发现不同颜色的黑色,但它们会丢失,因为它们不完全匹配

编辑:我有点麻烦。当我尝试使用此颜色查找黑色阴影时,我可以在将其他像素转换为白色时忽略它们,这是我的结果:

资料来源:

结果:

代码:

封装测试;
导入java.awt.Color;
导入java.awt.image.buffereImage;
导入java.io.File;
导入java.net.URL;
导入javax.imageio.imageio;
公开课考试
{       
公共静态void main(字符串[]args)
{
尝试
{
BuffereImage source=ImageIO.read(新URL(“http://i.imgur.com/UgdqfUY.png"));
//-16777216=黑色:
BuffereImage dest=makeMonologyRefast(源,-16777216);
文件结果=新文件(“D:/result.png”);
写入(dest,“png”,结果);
}
捕获(例外e)
{
e、 printStackTrace();;
}
}
公共静态BuffereImage MakeNomonicleFast(BuffereImage源,int前台)
{        
int background=-1;//白色;
颜色fg=新颜色(前景);
int color=0;
对于(int y=0;y(rP公差您可以使用这种“用差异公差寻找颜色”的方法

public static boolean isIncluded(Color target, Color pixel, int tolerance) {
    int rT = target.getRed();
    int gT = target.getGreen();
    int bT = target.getBlue();
    int rP = pixel.getRed();
    int gP = pixel.getGreen();
    int bP = pixel.getBlue();
    return(
        (rP-tolerance<=rT) && (rT<=rP+tolerance) &&
        (gP-tolerance<=gT) && (gT<=gP+tolerance) &&
        (bP-tolerance<=bT) && (bT<=bP+tolerance) );
}

根据问题中的代码,公差为150,我看到了这一点


您可以使用这种“带差异容差的颜色查找”方法

public static boolean isIncluded(Color target, Color pixel, int tolerance) {
    int rT = target.getRed();
    int gT = target.getGreen();
    int bT = target.getBlue();
    int rP = pixel.getRed();
    int gP = pixel.getGreen();
    int bP = pixel.getBlue();
    return(
        (rP-tolerance<=rT) && (rT<=rP+tolerance) &&
        (gP-tolerance<=gT) && (gT<=gP+tolerance) &&
        (bP-tolerance<=bT) && (bT<=bP+tolerance) );
}

根据问题中的代码,公差为150,我看到了这一点


一般来说,我认为方法是使用上所述的sRGB到灰度转换公式,然后选择一个特定的“灰色”值作为黑白之间的边界。(选择由您决定…)

但是如果你已经有了代表灰度点的RGB值,你应该会发现它们都有相等的红、绿和蓝值。如果确实是这样,那么你只需要选择RGB的一个颜色分量,并将其与所选“灰色”的相同颜色值进行比较

如果您需要区分黑色、灰色和白色的多种色调,请选择多种边界“颜色”


编辑:我遇到了一点麻烦。当我尝试使用此颜色查找黑色的阴影,以便在将其他像素转换为白色时可以忽略它们时,这是我的结果:


你看到的是消除混叠的效果。实际上,图像中几乎没有“纯”黑色。在人眼看来,很多黑色的东西实际上是黑色的,或者不是深灰色。你需要使边界颜色(即“黑色”和“非黑色”之间的边界)更多灰色。

一般来说,我认为方法是使用上所述的sRGB到灰度转换公式,然后选择一个特定的“灰色”值作为黑白边界。(选择由您决定…)

但是如果你已经有了代表灰度点的RGB值,你应该会发现它们都有相等的红、绿和蓝值。如果确实是这样,那么你只需要选择RGB的一个颜色分量,并将其与所选“灰色”的相同颜色值进行比较

如果您需要区分黑色、灰色和白色的多种色调,请选择多种边界“颜色”


编辑:我遇到了一点麻烦。当我尝试使用此颜色查找黑色的阴影,以便在将其他像素转换为白色时可以忽略它们时,这是我的结果:


你看到的是消除混叠的效果。实际上,图像中几乎没有“纯”黑色。在人眼看来,很多黑色的东西实际上是黑色的,或者不是深灰色。你需要使边界颜色(即“黑色”和“非黑色”之间的边界)更多灰色。

有没有关于设置公差的提示,以捕捉所有黑色阴影,例如忽略灰色?好吧,这个例子确实如此,对于“黑色”和“灰色”的某些定义。这就是你的评论的原因。.模糊。By“gray”DYM a
Color(20,20,20)
是“灰色”吗?是一种R/g/B中的一个被一个移到另两个的颜色(例如<代码>颜色(0,0,1)< /代码>)仍然是“黑色”?这里使用什么确切的规则来确定“黑色”和“灰色”?仅仅是你在视觉上看到的东西,会考虑黑色和灰色。我不知道RGB值是什么。我有点麻烦,请看我编辑的问题的细节/代码。很多代码(慢)是因为试图确定色差的轮廓。平滑锯齿状路径的代码(上面的链接)也会更快,但您根本不需要在这里这样做。只需1)创建一个新图像2)对于原始图像的宽度X高度(例如,每个像素),确定它是否“在黑色范围内相等”
import java.awt.*;
import java.awt.image.BufferedImage;
import java.awt.geom.Area;
import javax.imageio.ImageIO;
import java.io.File;
import java.util.Date;
import javax.swing.*;

/* Motorcycle image courtesy of ShutterStock
http://www.shutterstock.com/pic-13585165/stock-vector-travel-motorcycle-silhouette.html */
class ImageOutline {

    public static Area getOutline(BufferedImage image, Color color, boolean include, int tolerance) {
        Area area = new Area();
        for (int x=0; x<image.getWidth(); x++) {
            for (int y=0; y<image.getHeight(); y++) {
                Color pixel = new Color(image.getRGB(x,y));
                if (include) {
                    if (isIncluded(color, pixel, tolerance)) {
                        Rectangle r = new Rectangle(x,y,1,1);
                        area.add(new Area(r));
                    }
                } else {
                    if (!isIncluded(color, pixel, tolerance)) {
                        Rectangle r = new Rectangle(x,y,1,1);
                        area.add(new Area(r));
                    }
                }
            }
        }
        return area;
    }

    public static boolean isIncluded(Color target, Color pixel, int tolerance) {
        int rT = target.getRed();
        int gT = target.getGreen();
        int bT = target.getBlue();
        int rP = pixel.getRed();
        int gP = pixel.getGreen();
        int bP = pixel.getBlue();
        return(
            (rP-tolerance<=rT) && (rT<=rP+tolerance) &&
            (gP-tolerance<=gT) && (gT<=gP+tolerance) &&
            (bP-tolerance<=bT) && (bT<=bP+tolerance) );
    }

    public static BufferedImage drawOutline(int w, int h, Area area) {
        final BufferedImage result = new BufferedImage(
            w,
            h,
            BufferedImage.TYPE_INT_RGB);
        Graphics2D g = result.createGraphics();

        g.setColor(Color.white);
        g.fillRect(0,0,w,h);

        g.setClip(area);
        g.setColor(Color.red);
        g.fillRect(0,0,w,h);

        g.setClip(null);
        g.setStroke(new BasicStroke(1));
        g.setColor(Color.blue);
        g.draw(area);

        return result;
    }

    public static BufferedImage createAndWrite(
        BufferedImage image,
        Color color,
        boolean include,
        int tolerance,
        String name)
        throws Exception {
        int w = image.getWidth();
        int h = image.getHeight();

        System.out.println("Get Area: " + new Date() + " - " + name);
        Area area = getOutline(image, color, include, tolerance);
        System.out.println("Got Area: " + new Date() + " - " + name);

        final BufferedImage result = drawOutline(w,h,area);
        displayAndWriteImage(result, name);

        return result;
    }

    public static void displayAndWriteImage(BufferedImage image, String fileName) throws Exception {
        ImageIO.write(image, "png", new File(fileName));
        JOptionPane.showMessageDialog(null, new JLabel(new ImageIcon(image)));
    }

    public static void main(String[] args) throws Exception {
        final BufferedImage outline = ImageIO.read(new File("motorcycle.jpg"));
        BufferedImage crop = outline.getSubimage(17,35,420,270);
        displayAndWriteImage(crop, "motorcycle-01.png");

        BufferedImage crude = createAndWrite(crop, Color.white, false, 60, "motorcycle-02.png");

        BufferedImage combo = createAndWrite(crude, Color.red, true, 0, "motorcycle-03.png");
    }
}