图像洪水填充后颜色不同
我一直在玩我在 Stack Overflow 上找到的图像洪水填充。
我认为代码不是问题。不过,如果你有一个更好的,我会很高兴看到(或者如果你知道有这种类型的图像处理的库,那就更好了)。
我的问题是,在我对此图像运行算法后,该人的头盔不是绿色而是浅灰色。
我已经在 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);
}
});
}
}
这是我在测试中使用的图像。 。
I have been playing with image flood fill that I found here on Stack Overflow.
I think the code is not the problem. Though if you have a better one I would be glad to see (or even better if you know a library which has this type of image manipulations).
My problem is that after I run the algorithm on this image the guy's helmet instead of being green is light grey.
I have tried it on a silly example created in Paint and it works fine.
Thus, I think there must be some image setting or something of that kind which changes the rgb value I set to it in the algorithm.
Do you have any suggestions to what should be set in the code (please see below)?
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);
}
});
}
}
This is the image I am using in tests. .
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你在那里得到了一个灰度图像。您不能在灰度图像上使用绿色。这就是为什么它变成浅灰色的原因。
您需要:
最后一个选项更安全,因为它不会在以后的图像上失败。这是我在网上找到的一些代码,据说可以转换为灰度。稍作修改,您就拥有了确保处理彩色图像所需的一切:
You got a grayscale image there. You cannot use green color on a grayscale image. That's why it turns up as light gray.
You need to either:
The last option is more safe as it will not fail on future images. Here is some code I found on the web that is said to do conversion to Grayscale. A small modification and you have what you need to ensure you are working on a color image: