2015-11-02 200 views
3

我想找到圖像的每個像素,然後我會得到總像素值,然後我會找到平均值。我比較每個像素值與我得到的平均值,如果它> 255,像素值將變爲1(代表黑色),如果< 255則變爲0(代表白色)。之後,我設置新的RGB顏色,並繪製輸出圖像。 Input在java中的圖像平均閾值

基於我的理念,我認爲輸出圖像是黑白圖像,但它只是顯示黑色。 Output

public class Imej { 
    public void mapping(BufferedImage image) throws IOException { 
     BufferedImage binary = new BufferedImage(image.getWidth(), 
      image.getHeight(), BufferedImage.TYPE_BYTE_BINARY); 
     int i, j; 
     int w = image.getWidth(); 
     int h = image.getHeight(); 

     image.setRGB(i, j, new Color(pixel[i][j]).getRGB()); 
     ImageIO.write(binary,"png",output); 
    } 
} 

這是readimage

public void readimage() { 
    BufferedImage image = null; 
    File f = null; 
    try { 
     image = ImageIO.read(new File(/** path **/)); 
     //System.out.println(image); 
     mapping(image); 
    } catch (Exception e) { 
     e.printStackTrace(); 
     System.exit(1); 
    } 
} 

public static void main(String[] args) { 
    Imej a = new Imej(); 
    a.readimage(); 
} 
+0

你的意思'225',或者你的意思'255'?或者,你的意思是'w'還是'h'取決於上下文? –

+0

是255 ......... – kenreal

+0

您的當前*輸入圖像爲300x300。 –

回答

1

您應該創建一個平均函數,它返回像素的平均值。類似於int mean(int[][] pixels, int i, int j)。然後,你應該改變的第一行中的if語句是if (mean(pixel, i, j) > mean) { //...

 if (pixel[i][j] > mean) 
       pixel[i][j]=1; 

應該

 if (mean(pixel, i, j) > mean) 
       pixel[i][j]=0xFFFFFF; 
+0

我試圖改變,但結果仍然相同..或者仍然有另一個錯誤.. – kenreal

+0

謝謝dude.i可以創建黑白圖像..不會創建功能.. – kenreal