자바 에서 ImageIO.write(bufferedImage 가 그림 을 출력 할 때 그림 의 변경 문제

6274 단어 자바
그림 의 이치 화,그 레이스 케 일 을 작성 할 때 우연히 방법 에서 ImageIO.write 출력 그림 을 사용 할 때 bufferedImage 에 값 을 부여 할 때의 문 구 를 발 견 했 습 니 다.아래 에 코드 를 붙 입 니 다.
 
 
package test;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;

public class test {

	public static void main(String[] args) throws IOException {
		// TODO Auto-generated method stub

        BufferedImage bufferedImage = ImageIO.read(new File("D:/adt-bundle-windows-x86_64-20140321/workspace/test/img/test6.jpg"));         

	
        BufferedImage bufferedImage2=grayimage( bufferedImage);
        BufferedImage bufferedImage3=erzh( bufferedImage);//     bufferedImage2 
        
        
        ImageIO.write(bufferedImage, "jpg", new File("D:/adt-bundle-windows-x86_64-20140321/workspace/test/img/yuantu.jpg"));
        ImageIO.write(bufferedImage2, "jpg", new File("D:/adt-bundle-windows-x86_64-20140321/workspace/test/img/huidutu.jpg"));
        ImageIO.write(bufferedImage3, "jpg", new File("D:/adt-bundle-windows-x86_64-20140321/workspace/test/img/ezh.jpg"));
	
	}
	
	public static BufferedImage grayimage(BufferedImage bufferedImage){
		
		int h = bufferedImage.getHeight();
	    int w = bufferedImage.getWidth();
	   //BufferedImage bufferedImage2 = new BufferedImage(w, h, BufferedImage. TYPE_3BYTE_BGR );
	    for (int x = 0; x < w; x++) 
	      {
	          for (int y = 0; y < h; y++) 
	          {
	            int argb = bufferedImage.getRGB(x, y);
	        	  int r = (argb >> 16) & 0xFF;
                  int g = (argb >> 8) & 0xFF;
                  int b = (argb >> 0) & 0xFF;
                  int grayPixel=(int)(r+g+b)/3;
	         	//        rgb   
                int rgb = (grayPixel*256 + grayPixel)*256+grayPixel;  
                  if(rgb>8388608)  //rgb     8388608,           256*256*256
                  {  
                      rgb = rgb - 16777216;  
                  }                                 
	         	bufferedImage.setRGB(x, y, rgb);
	         	//bufferedImage2.setRGB(x, y, rgb);
	         	   
	         	          	   
	          }
	       }
		//return  bufferedImage2;
		return  bufferedImage;
		

	}
	public static int[][] huidutujuzhen(BufferedImage bufferedImage)
	{
		//   
		bufferedImage=grayimage(bufferedImage);
		int h = bufferedImage.getHeight();
	    int w = bufferedImage.getWidth();
	    //      
	    int[][]gray=new int[h][w];       
	    for (int x = 0; x < w; x++) 
	    {
	        for (int y = 0; y < h; y++) 
	        {
	           int a = bufferedImage.getRGB(x, y);
	           gray[y][x]=a;
	                      
	        }         
	     }
		
		return gray;
		
	}
	public static BufferedImage erzh(BufferedImage bufferedImage){
        
       int h = bufferedImage.getHeight();
       int w = bufferedImage.getWidth();     		
       int[][] gray =  huidutujuzhen( bufferedImage);
      // BufferedImage bufferedImage2 = new BufferedImage(w, h, BufferedImage. TYPE_3BYTE_BGR );
              //           
                 int threshold = ostu(gray, w, h);
                 System.out.print(threshold);
  	         // BufferedImage binaryBufferedImage = ImageIO.read(new File("D:/adt-bundle-windows-x86-20130219/workspace/wenzi/src/huiduhua.jpg")); 

                //  BufferedImage binaryBufferedImage = new BufferedImage(w, h, BufferedImage.TYPE_BYTE_BINARY);//           TYPE_BYTE_GRAY、、TYPE_3BYTE_BGR
                           for (int x = 0; x < w; x++) 
                           {
                              for (int y = 0; y < h; y++)
                              {
                            	  int gry=gray[y][x]&0xff;
                                   if (gry > threshold) 
                                   {
                                       gray[y][x]=0xFFFFFF;
                                   } 
                                   else
                                   {
                                      gray[y][x]=0x000000;
                                   }                                                    	 
                                   bufferedImage.setRGB(x, y, gray[y][x] );
                               }
                          }
                               
                           return bufferedImage;
              	                                    
}
	
	 //OTSU        T                。            
    public static int ostu(int[][] gray, int w, int h) {
        int[] histData =new int[256];// new int[w * h];
        // Calculate histogram
        for (int x = 0; x < w; x++) {
            for (int y = 0; y < h; y++) {
                int red = 0xFF & gray[y][x];
                histData[red]++;
            }
        }
 
        // Total number of pixels
        int total = w * h;
 
        float sum = 0;   //     ,,,,,,         histdata    。
        for (int t = 0; t < 256; t++)
            sum += t * histData[t];
 
        float sumB = 0;
        int wB = 0;
        int wF = 0;
 
        float varMax = 0;
        int threshold = 0;
 
        for (int t = 0; t < 256; t++) {
            wB += histData[t]; // Weight Background//histData[t]    t        ,wB          
            if (wB == 0)
                continue;
 
            wF = total - wB; // Weight Foreground  wF     
            if (wF == 0)
                break;
 
            sumB += (float) (t * histData[t]);//   t     
 
            float mB = sumB / wB; // Mean Background          t             
            float mF = (sum - sumB) / wF; // Mean Foreground  sum        
 
            // Calculate Between Class Variance
            float varBetween = (float) wB * (float) wF * (mB - mF) * (mB - mF);
 
            // Check if new maximum found
            if (varBetween > varMax) {
                varMax = varBetween;
                threshold = t;
            }
        }
 
        return threshold;
    }

}

                       bufferedImage  ,                     ,    (      ,     ) ImageIO.write             BufferedImage bufferedImage2=grayimage( bufferedImage);    bufferedImage           ,     2       !


좋은 웹페이지 즐겨찾기