2015-11-02 203 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