image类对图像处理
import java.awt.image.BufferedImage;
public class Image {
public int h; //高
public int w; //宽
public int[] data; //像素
public boolean gray; //是否为灰度图像
public Image(BufferedImage img) {
this.h = img.getHeight();
this.w = img.getWidth();
this.data = img.getRGB(0, 0, w, h, null, 0, w);
this.gray = false;
toGray(); //灰度化
}
public void toGray(){
if(!gray){
this.gray = true;
for (int y = 0; y < h; y++) {
for (int x = 0; x < w; x++) {
int c = this.data[x + y * w];
int R = (c >> 16) & 0xFF;
int G = (c >> 8) & 0xFF;
int B = (c >> 0) & 0xFF;
this.data[x + y * w] = (int)(0.3f*R + 0.59f*G + 0.11f*B); //to gray
}
}
}
}
public int[] hist(){
toGray();
int[] hist = new int[256];
int len = h*w;
for(int i=0;i<len;i++)
hist[data[i]]++;
return hist;
}
}
用遗传算法找阈值
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Random;
public class OPRA {
public Image image;
public int gene_length = 8;//编码基因长度
private String gene;
public String getGene() {
return gene;
}
public void setGene(String gene) {
this.gene