图像填充后不同的颜色

| 我一直在玩在堆栈溢出中找到的图像填充。 我认为代码不是问题。虽然如果您有更好的软件,我将很高兴见到(或者,如果您知道具有这种图像处理类型的库,甚至会更好)。 我的问题是,在此图像上运行算法后,该人的头盔不是绿色,而是浅灰色。 我已经在Paint中创建的一个愚蠢的示例上进行了尝试,并且效果很好。 因此,我认为必须有某种图像设置或某种可以改变我在算法中设置的rgb值的图像设置。 您对代码中的设置有什么建议(请参见下文)?
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class FloodFillTest extends JPanel
{
    private static final long serialVersionUID = 1L;
    private BufferedImage bI;
    public FloodFillTest()
    {
        try
        {
            this.bI = ImageIO.read(new File(\"images/brother.png\"));
            Color targetColor = Color.WHITE;
            Color replacementColor = Color.GREEN;
            System.out.println(\"targetColor=\"+targetColor+\"; replacementColor=\"+replacementColor);
            floodFill(125, 90, targetColor, replacementColor, bI);      
            setPreferredSize(new Dimension(bI.getWidth(), bI.getHeight()));
            System.out.println(\"bI.getWidth()=\"+bI.getWidth()+\"; bI.getHeight()=\"+bI.getHeight());
        }catch(IOException ex)
        {
            Logger.getLogger(FloodFillTest.class.getName()).log(Level.SEVERE, null, ex);
        }
    }   

    /**
     * Fills a color in the image with a different color.
     * @param x x coordinate of starting point.
     * @param y y coordinate of starting point.
     * @param targetColor color we want to replace.
     * @param replacementColor the color which is used as the replacement.
     * @param image the image where we fill the color.
     */
    public static void floodFill(int x, int y, Color targetColor, Color replacementColor,
            BufferedImage image)
    {
        if(image.getRGB(x, y) != targetColor.getRGB()) 
            return;
        image.setRGB(x, y, replacementColor.getRGB());
        System.out.println(\"after set image.getRGB(x,y)=\"+ new Color(image.getRGB(x,y)).toString());
        floodFill(x - 1, y, targetColor, replacementColor, image);
        floodFill(x + 1, y, targetColor, replacementColor, image);
        floodFill(x, y - 1, targetColor, replacementColor, image);
        floodFill(x, y + 1, targetColor, replacementColor, image);
    }
    @Override
    protected void paintComponent(Graphics g)
    {
        super.paintComponent(g);        
        Graphics2D g2 = (Graphics2D) g;
        g2.drawImage(bI, 0,0, null);
    }   

    public static void main(String[] args)
    {
        SwingUtilities.invokeLater(new Runnable()
        {
            @Override
            public void run()
            {               
                System.out.println(\"Color.WHITE=\"+Color.WHITE +\"; Color.BLACK=\"+Color.BLACK);
                JPanel p = new FloodFillTest();
                p.setBackground(Color.CYAN);
                JPanel contentPane = new JPanel();
                contentPane.add(p);
                JFrame f = new JFrame();
                f.setContentPane(contentPane);
                f.setSize(800, 600);
                f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                f.setVisible(true);
            }
        });
    }
}
这是我在测试中使用的图像。 。     
已邀请:
您在那里有一个灰度图像。您不能在灰度图像上使用绿色。这就是为什么它变成浅灰色的原因。 您需要: 事先将图像转换为RGB 确保在Java中将图像读取/转换为RGB 最后一个选项更安全,因为它在以后的图像上不会失败。这是我在网上找到的一些代码,据说可以将它们转换为灰度。稍作修改,您便可以确保您正在处理彩色图像:
public static BufferedImage convertToGrayscale(BufferedImage source) { 
     BufferedImageOp op = new ColorConvertOp(
       ColorSpace.getInstance(ColorSpace.CS_GRAY), null); 
     return op.filter(source, null);
}
    

要回复问题请先登录注册