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,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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 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;
  }
 

}

点赞