做数字图像处理的实验,同学大部分都是用MATLAB了,因为网上查到的关于java实现的代码寥寥无几,但是我就偏要用java实现。不过还是以网上的代码作为参考的,但是他们对于bufferedimage的用法没有说。我自己啃源码,啃了两小时才弄出来。废话不多说,直接上代码。
TransF类用来实现均衡化:
import java.awt.image.BufferedImage;
public class TransF {
double[] count = new double[256];//记录灰度值个数
double[] Count = new double[256];//统计新图的灰度值
double[] NSK=new double[256];//变换函数
public BufferedImage run(BufferedImage image) {
double H,S,I;
int width = image.getWidth();
int height = image.getHeight();
BufferedImage result = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
for (int j = 0; j < height; j++) {
for(int i=0; i<width; i++) {
Object data=image.getRaster().getDataElements(i,j,null);
int red = image.getColorModel().getRed(data);
int blue = image.getColorModel().getBlue(data);
int green = image.getColorModel().getGreen(data);
I= ((double)(red+green+blue)/(double)3);
if (count[(int)I]==0){
count[(int)I]=1;
}else {
count[(int)I]+=1;
}
}
}
double[] Pr=new double[256];//计算各灰度值出现概率
for (int i=0;i<256;i++){
Pr[i]=(double)count[i]/(double)(width*height);
}
double[] sk = new double[256];//概率累计
for (int i=0;i<256;i++){
for (int j=0;j<=i;j++){
sk[i]+=Pr[j];
}
}
for (int i=0;i<256;i++){
NSK[i]=(255*sk[i]+0.5);
}
int[] rgb=new int[3];
for (int j=0;j<height;j++){