java實現像素級圖片檔案處理

來源:互聯網
上載者:User

 java實現像素級圖片檔案處理 收藏
      朋友要求幫忙做一個圖片識別的小程式,因為要用在特定的環境下,所以決定採用java語言實現。首先用matlab實現了識別演算法的模擬,因為只是對特定的數字組合的識別,所以非常的簡單,放棄採用比較複雜的識別演算法,採用最普通的像素比較的識別演算法。(如果背景雜訊比較複雜,可以考慮先濾波後識別)在寫java程式的時候發現一些問題,網上關於圖片像素級操作的資料不是太多,有的還不是太正確,特此寫出自己的成果與大家分享。
核心類:BufferedImage,ImageIO
ImageIO類提供圖象讀寫介面,可以對URL,InputStream等操作,得到映像資訊十分的方便。
ImageIO在javax.imageio.*的包中,屬於jdk中的標準類。提供的方法有:
read()  例:BufferedImage imd=ImageIO.read(new File(file));
write() 例:ImageIO.write(imd, "JPEG", new File("C://test"+k+".gif"));
//具體方法可以尋找jdk doc
BufferedImage類是一個Image類的子類,與Image不同的是,它是在記憶體中建立和修改的,你可以顯示它也可以不顯示它,這就看你的具體需求了。這裡因為我用於映像的識別所以就不需要顯示出來了。你可以通過ImageIO的方法來讀取一個檔案到BufferedImage,也可以將其寫回一個檔案中去。類似的操作可以看前面的兩個方法。以及參考jdk doc
因為我要識別類似於身分識別驗證的一個數字串圖片,所以我考慮把這些數字分離出來,存在不同的映像內,這裡BufferedImage類提供一個很方便的辦法。
getSubimage(int left,int top,int width,int height)
例:    BufferedImage newim[]=new BufferedImage[4];
 newim[0]=imd.getSubimage(4,0,10,18);
 newim[1]=imd.getSubimage(13,0,10,18);
 newim[2]=imd.getSubimage(22,0,10,18);
 newim[3]=imd.getSubimage(31,0,10,18);
最後為了得到映像的像素,我們需要的就是得到像素的方法,這個方法有很多,這裡我介紹的是
getRGB(int x,int y) 得到特定像素點的RGB值。
例: pix=new int[10*18];pix[i*(10)+j]=newim[k].getRGB(j,i);
現在我們得到了像素,可以看出像素是一個一維數組,你如果不習慣可以考慮儲存在一個二維的數組中,然後就來實施你的看家演算法,什麼小波變換,拉普拉斯運算元,儘管來吧。怎麼樣是不是很方便呢?什麼你好像看不太懂,好給你一些來源程式好了,包括像素分解和識別演算法。

原始碼
/*
 * Created on 2005-11-29
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
package com.syvin.image;

import java.awt.*;
import java.awt.image.*;
import java.io.FileOutputStream;
import java.io.*;
import java.io.InputStream;
import java.net.URL;
import javax.imageio.*;
public class MyImage{
   BufferedImage imd;//待識別映像
 
 private int iw,ih;//映像寬和高
 
 public final static String path="D://jyy//app//tomcat//webapps//userlogon//a.jpg";

  static public void main(String args[]) {
   try{
   MyImage app = new MyImage();//構造一個類
  
   String s=app.getImageNum("C://無標題.bmp");//得到識別字串
   System.out.println("recognize result"+s);
   byte[] by=s.getBytes();
   File f=new File("C://testfile.txt");
   FileOutputStream fos=new FileOutputStream(f);//寫入一個結果檔案
   fos.write(by);
   fos.close();
   }catch(Exception e){
    e.printStackTrace();
   }
  }

 //建構函式
  public MyImage() throws IOException {
  
    super("Image Test");
    try{
    }catch(Exception e){
     e.printStackTrace();
    }
  }
 //得到映像的值
  public String getImageNum(String file){
  
   StringBuffer sb=new StringBuffer("");
   try{
   imd=ImageIO.read(new File(file));//用ImageIO的靜態方法讀取映像
 BufferedImage newim[]=new BufferedImage[4];
 int []x=new int[4];
        //將映像分成四塊,因為要處理的檔案有四個數字。
 newim[0]=imd.getSubimage(4,0,10,18);
 newim[1]=imd.getSubimage(13,0,10,18);
 newim[2]=imd.getSubimage(22,0,10,18);
 newim[3]=imd.getSubimage(31,0,10,18);
 
 for(int k=0;k<4;k++){
 

 x[k]=0;

 ImageIO.write(newim[k], "JPEG", new File("C://test"+k+".gif"));
 this.iw=newim[k].getWidth(null);
 this.ih=newim[k].getHeight(null);
 pix=new int[iw*ih];

 //因為是二值映像,這裡的方法將像素讀取出來的同時,轉換為0,1的映像數組。
 for(int i=0;i<ih;i++){
  for(int j=0;j<iw;j++){
   pix[i*(iw)+j]=newim[k].getRGB(j,i);
   if(pix[i*(iw)+j]==-1)
    pix[i*(iw)+j]=0;
   else pix[i*(iw)+j]=1;
  
   x[k]=x[k]+pix[i*(iw)+j];

  }

 }
 //得到像匹配的數字。
 int r=this.getMatchNum(pix);
 sb.append(r);
 System.out.println("x="+x[k]);
 }
   }catch(Exception e){
    e.printStackTrace();
   }
 return sb.toString();
}
//數字模板 0-9
  static  int[][] value={
   //num 0;
   {0,0,0,0,0,0,0,0,0,0,
    0,0,0,0,0,0,0,0,0,0,
 0,0,0,0,0,0,0,0,0,0,
 0,0,0,0,0,0,0,0,0,0,
 0,0,0,0,1,1,0,0,0,0,
 0,0,1,1,1,1,1,0,0,0,
 0,0,1,1,0,0,1,1,0,0,
 0,1,1,0,0,0,0,1,1,0,
 0,1,1,0,0,0,0,1,1,0,
 0,1,1,0,0,0,0,1,1,0,
 0,1,1,0,0,0,0,1,1,0,
 0,0,1,1,0,0,1,1,0,0,
 0,0,0,1,1,1,1,0,0,0,
 0,0,0,0,1,1,0,0,0,0,
 0,0,0,0,0,0,0,0,0,0,
 0,0,0,0,0,0,0,0,0,0,
 0,0,0,0,0,0,0,0,0,0,
 0,0,0,0,0,0,0,0,0,0
    },
   //num 1
   {0,0,0,0,0,0,0,0,0,0,
   0,0,0,0,0,0,0,0,0,0,
 0,0,0,0,0,0,0,0,0,0,
 0,0,0,0,0,0,0,0,0,0,
 0,0,0,0,0,1,1,0,0,0,
 0,0,0,0,1,1,1,0,0,0,
 0,0,0,1,1,1,1,0,0,0,
 0,0,0,0,0,1,1,0,0,0,
 0,0,0,0,0,1,1,0,0,0,
 0,0,0,0,0,1,1,0,0,0,
 0,0,0,0,0,1,1,0,0,0,
 0,0,0,0,0,1,1,0,0,0,
 0,0,0,0,0,1,1,0,0,0,
 1,1,1,1,1,1,1,1,1,0,
 0,0,0,0,0,0,0,0,0,0,
 0,0,0,0,0,0,0,0,0,0,
 0,0,0,0,0,0,0,0,0,0,
 0,0,0,0,0,0,0,0,0,0
 },
 //num2
 {
 0,0,0,0,0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,
 0,0,1,0,0,0,0,1,1,0,
 0,0,1,1,0,0,1,1,0,0,
 0,0,0,1,1,1,1,0,0,0,
 0,0,0,0,0,0,0,0,0,0,
 0,0,0,0,0,0,0,0,0,0,
 0,0,0,0,0,0,0,0,0,0,
 0,0,0,0,0,0,0,0,0,0
 }};
 
 
  //映像像素相減取絕對值得到最小熵的結果。
  public int getMatchNum(int[] pix){
   int result=-1;
   int temp=100;
   int x;
   for(int k=0;k<=9;k++){
     x=0;
    for(int i=0;i<pix.length;i++){
     x=x+Math.abs(pix[i]-value[k][i]);
   
    }
    /*for(int a=0;a<18;a++){
     for(int b=0;b<10;b++){
      System.out.print(pix[a*10+b]+"-"+value[k][a*10+b]+"|");
    
     }
     System.out.println();
   
    }*/
   
    if(x<temp)
    {
     temp=x;
     result=k;
    }
   
   }
 
   return result;
  }
 

}

本文來自CSDN部落格,轉載請標明出處:http://blog.csdn.net/jinyykiller/archive/2005/12/29/565584.aspx

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.