java 用遗传算法解决图像二值化问题 找阈值

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 
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值