我一直玩圖像泛洪填充I found here on 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);
}
});
}
}
這是我在測試中使用的圖像。 。
對不起,但是什麼問題?也許我忽略了一些東西,但我沒看到你說出來。 – Bart 2011-05-23 16:03:52
@對不起,你完全正確。現在我清楚地說明。問題在於,頭盔使用淺灰色填充,而不是使用替代顏色參數中給出的綠色填充。 – Boro 2011-05-23 16:25:48
* setRGB *和* getRGB *除了令人驚訝的慢,還可以做很多事情,例如根據顏色模型修改* r,g,b *值。當我使用「像素「,我使用底層* int [] *支持的圖像,並從/ int * [*]中讀取/寫入整數。它不僅速度更快,而且您還可以確定您確實在閱讀並放置所需的* r,g,b *值。 – SyntaxT3rr0r 2011-05-23 16:27:31