一 讀取bmp圖片數據
// 獲取待檢測圖像
public void getBMPImage(String source) throws Exception {
clearNData(); //清除數據保存區
FileInputStream fs = null;
try {
fs = new FileInputStream(source);
int bfLen =
byte bf[] = new byte[bfLen];
fs
int biLen =
byte bi[] = new byte[biLen];
fs
// 源圖寬度
nWidth = (((int) bi[
| (((int) bi[
| (((int) bi[
// 源圖高度
nHeight = (((int) bi[
| (((int) bi[
| (((int) bi[
// 位數
nBitCount = (((int) bi[
// 源圖大小
int nSizeImage = (((int) bi[
| (((int) bi[
| (((int) bi[
// 對
if (nBitCount ==
int nPad = (nSizeImage / nHeight)
nData = new int[nHeight * nWidth];
nB=new int[nHeight * nWidth];
nR=new int[nHeight * nWidth];
nG=new int[nHeight * nWidth];
byte bRGB[] = new byte[(nWidth + nPad) *
fs
int nIndex =
for (int j =
for (int i =
nData[nWidth * (nHeight
| (((int) bRGB[nIndex +
| (((int) bRGB[nIndex +
| (int) bRGB[nIndex] &
nB[nWidth * (nHeight
nG[nWidth * (nHeight
nR[nWidth * (nHeight
nIndex +=
}
nIndex += nPad;
}
// Toolkit kit = Toolkit
// image = kit
// nData
/*
//調試數據的讀取
FileWriter fw = new FileWriter(
PrintWriter out = new PrintWriter(fw);
for(int j=
for(int i=
out
+nR[nWidth * (nHeight
+nG[nWidth * (nHeight
+nB[nWidth * (nHeight
}
out
}
out
*/
}
}
catch (Exception e) {
e
throw new Exception(e);
}
finally {
if (fs != null) {
fs
}
}
// return image;
}
二 由r g b 獲取灰度數組
public int[] getBrightnessData(int rData[]
int brightnessData[]=new int[rData
if(rData
|| bData
return brightnessData;
}
else {
for(int i=
double temp=
brightnessData[i]=(int)(temp)+((temp
}
return brightnessData;
}
}
三 直方圖均衡化
public int [] equilibrateGray(int[] PixelsGray
{
int gray;
int length=PixelsGray
int FrequenceGray[]=new int[length];
int SumGray[]=new int[
int ImageDestination[]=new int[length];
for(int i =
{
gray=PixelsGray[i];
FrequenceGray[gray]++;
}
// 灰度均衡化
SumGray[
for(int i=
SumGray[i]=SumGray[i
}
for(int i=
SumGray[i]=(int)(SumGray[i]*
}
for(int i=
{
for(int j=
{
int k=i*width+j;
ImageDestination[k]=
}
}
return ImageDestination;
}
四 laplace
public int[] laplace
int filterData[]=new int[data
int min=
int max=
for(int i=
for(int j=
if(i==
filterData[i*width+j]=data[i*width+j];
else
filterData[i*width+j]=
if(filterData[i*width+j]<min)
min=filterData[i*width+j];
if(filterData[i*width+j]>max)
max=filterData[i*width+j];
}
}
// System
// System
for(int i=
filterData[i]=(filterData[i]
}
return filterData;
}
五 laplace
public int[] laplaceHigh
int filterData[]=new int[data
int min=
int max=
for(int i=
for(int j=
if(i==
filterData[i*width+j]=(int)((
else
filterData[i*width+j]=(int)((
if(filterData[i*width+j]<min)
min=filterData[i*width+j];
if(filterData[i*width+j]>max)
max=filterData[i*width+j];
}
}
for(int i=
filterData[i]=(filterData[i]
}
return filterData;
}
六 局部阈值處理
// 局部阈值處理
/*原理
T(x
取一個寬度為w的矩形框
統計框內數據
這個算法的優點是 速度快
缺點是 niblack
*/
public int[] localThresholdProcess(int []data
int[] processData=new int[data
for(int i=
processData[i]=
}
if(data
return processData;
int wNum=width/w;
int hNum=height/h;
int delt[]=new int[w*h];
//System
for(int j=
for(int i=
//for(int j=
// for(int i=
for(int n=
for(int k=
delt[n*w+k]=data[(j*h+n)*width+i*w+k];
//System
}
//System
/*
for(int n=
for(int k=
System
}
System
*/
delt=thresholdProcess(delt
for(int n=
for(int k=
processData[(j*h+n)*width+i*w+k]=delt[n*w+k];
// System
}
//System
/*
for(int n=
for(int k=
System
}
System
*/
}
}
return processData;
}
七 全局阈值處理
public int[] thresholdProcess(int []data
int [] processData=new int[data
if(data
return processData;
else{
double sum=
double average=
double variance=
double threshold;
if( gate!=
threshold=gate;
}
else{
for(int i=
sum+=data[i];
}
average=sum/(width*height);
for(int i=
variance+=(data[i]
}
variance=Math
threshold=average
}
for(int i=
if(data[i]>threshold)
processData[i]=
else
processData[i]=
}
return processData;
}
}
八 垂直邊緣檢測
public int[] verticleEdgeCheck(int []data
int filterData[]=new int[data
int min=
int max=
if(data
return filterData;
try{
for(int i=
for(int j=
if(i==
||j==
filterData[i*width+j]=data[i*width+j];
}
else{
double average;
//中心的九個像素點
//average=data[i*width+j]
average=data[i*width+j]
filterData[i*width+j]=(int)(average);
}
if(filterData[i*width+j]<min)
min=filterData[i*width+j];
if(filterData[i*width+j]>max)
max=filterData[i*width+j];
}
}
for(int i=
filterData[i]=(filterData[i]
}
}
catch (Exception e)
{
e
throw new Exception(e);
}
return filterData;
}
九 圖像平滑
public int[] filter(int []data
int filterData[]=new int[data
int min=
int max=
if(data
return filterData;
try{
for(int i=
for(int j=
if(i==
||j==
filterData[i*width+j]=data[i*width+j];
}
else{
double average;
//中心的九個像素點
average=(data[i*width+j]+data[i*width+j
+data[(i
+data[(i+
filterData[i*width+j]=(int)(average);
}
if(filterData[i*width+j]<min)
min=filterData[i*width+j];
if(filterData[i*width+j]>max)
max=filterData[i*width+j];
}
}
for(int i=
filterData[i]=(filterData[i]
}
}
catch (Exception e)
{
e
throw new Exception(e);
}
return filterData;
}
From:http://tw.wingwit.com/Article/program/Java/hx/201311/26286.html