2011-05-23 28 views
1

我一直玩圖像泛洪填充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); 
      } 
     }); 
    } 
} 

這是我在測試中使用的圖像。 The image I am using in tests

+0

對不起,但是什麼問題?也許我忽略了一些東西,但我沒看到你說出來。 – Bart 2011-05-23 16:03:52

+0

@對不起,你完全正確。現在我清楚地說明。問題在於,頭盔使用淺灰色填充,而不是使用替代顏色參數中給出的綠色填充。 – Boro 2011-05-23 16:25:48

+1

* setRGB *和* getRGB *除了令人驚訝的慢,還可以做很多事情,例如根據顏色模型修改* r,g,b *值。當我使用「像素「,我使用底層* int [] *支持的圖像,並從/ int * [*]中讀取/寫入整數。它不僅速度更快,而且您還可以確定您確實在閱讀並放置所需的* r,g,b *值。 – SyntaxT3rr0r 2011-05-23 16:27:31

回答

4

你在那裏得到了灰度圖像。您不能在灰度圖像上使用綠色。這就是爲什麼它變成淺灰色。

您需要:

  • 將圖像轉換爲RGB事先
  • 確保你讀/圖像中的Java轉換爲RGB

最後一個選項是更安全它不會在未來的圖像上失敗。以下是我在網上發現的一些代碼,據說可以轉換爲灰度。一個小的修改,你有你需要確保你正在處理彩色圖像:

public static BufferedImage convertToGrayscale(BufferedImage source) { 
    BufferedImageOp op = new ColorConvertOp(
     ColorSpace.getInstance(ColorSpace.CS_GRAY), null); 
    return op.filter(source, null); 
} 
+0

就是這樣。我已經忘記了明顯的。我一直在使用灰度圖像。謝謝:) – Boro 2011-05-23 17:02:25

+0

現在,我將圖像更改爲色階。但出於好奇,我將如何使用我的例子中的代碼。我試圖使用它在加載的圖像或洪泛後的圖像,我得到相同的空指針異常'異常在線程「AWT-EventQueue-0」java.lang.NullPointerException \t在java.awt.image.ComponentColorModel。 getDataElements(ComponentColorModel.java:1538) \t at java.awt.image.BufferedImage.setRGB(BufferedImage.java:971) \t at test.FloodFillTest.floodFill(FloodFillTest.java:59)......' – Boro 2011-05-23 17:17:00