java 二值化 代码_图像二值化(java代码)

import java.awt.Color;

import java.awt.image.BufferedImage;

import java.io.File;

import java.io.IOException;

import javax.imageio.ImageIO;

public class Binary {

public static void main(String[] args) throws IOException {

Image imagesoure = new Image();

System.out.println("This is the end");

}

}

class Image {

int width;

int height;

int area = width * height;

// BufferedImage outBinary=img;

public Image() throws IOException {

File file = new File("F:/77.jpg");

BufferedImage img = ImageIO.read(file);

BufferedImage outBinary = img;

width = img.getWidth();

height = img.getHeight();

int area = width * height;

int gray[][] = new int[width][height];

int u = 0;// 灰度平均值

int graybinary;

int graysum = 0;

int graymean = 0;

int grayfrontmean = 0;

int graybackmean = 0;

Color color;

int pixl[][] = new int[width][height];

int pixelsR;

int pixelsG;

int pixelsB;

int pixelGray;

int T = 0;

int front = 0;

int back = 0;

for (int i = 1; i < width; i++) { // 不算边界行和列,为避免越界

for (int j = 1; j < height; j++) {

pixl[i][j] = img.getRGB(i, j);

color = new Color(pixl[i][j]);

pixelsR = color.getRed();// R空间

pixelsB = color.getBlue();// G空间

pixelsG = color.getGreen();// B空间

pixelGray = (int) (0.3 * pixelsR + 0.59 * pixelsG + 0.11 * pixelsB);// 计算每个坐标点的灰度

gray[i][j] = (pixelGray << 16) + (pixelGray << 8) + (pixelGray);

graysum += pixelGray;

}

}

graymean = (int) (graysum / area);// 整个图的灰度平均值

u = graymean;

System.out.println(u);

for (int i = 0; i < width; i++) // 计算整个图的二值化阈值

{

for (int j = 0; j < height; j++) {

if (((gray[i][j]) & (0x0000ff)) < graymean) {

graybackmean += ((gray[i][j]) & (0x0000ff));

back++;

} else {

grayfrontmean += ((gray[i][j]) & (0x0000ff));

front++;

}

}

}

int frontvalue = (int) (grayfrontmean / front);// 前景中心

int backvalue = (int) (graybackmean / back);// 背景中心

float G[] = new float[frontvalue - backvalue + 1];// 方差数组

int s = 0;

System.out.println(front);

System.out.println(frontvalue);

System.out.println(backvalue);

for (int i1 = backvalue; i1 < frontvalue + 1; i1++)// 以前景中心和背景中心为区间采用大津法算法

{

back = 0;

front = 0;

grayfrontmean = 0;

graybackmean = 0;

for (int i = 0; i < width; i++) {

for (int j = 0; j < height; j++) {

if (((gray[i][j]) & (0x0000ff)) < (i1 + 1)) {

graybackmean += ((gray[i][j]) & (0x0000ff));

back++;

} else {

grayfrontmean += ((gray[i][j]) & (0x0000ff));

front++;

}

}

}

grayfrontmean = (int) (grayfrontmean / front);

graybackmean = (int) (graybackmean / back);

G[s] = (((float) back / area) * (graybackmean - u)

* (graybackmean - u) + ((float) front / area)

* (grayfrontmean - u) * (grayfrontmean - u));

s++;

}

float max = G[0];

int index = 0;

for (int i = 1; i < frontvalue - backvalue + 1; i++) {

if (max < G[i]) {

max = G[i];

index = i;

}

}

// System.out.println(G[index]);

// System.out.println(index);

for (int i = 0; i < width; i++) {

for (int j = 0; j < height; j++) {

if (((gray[i][j]) & (0x0000ff)) < (index + backvalue)) {

outBinary.setRGB(i, j, 0x000000);

} else {

outBinary.setRGB(i, j, 0xffffff);

}

}

}

File f = new File("F:/88.jpg");

ImageIO.write(outBinary, "jpg", f);

}

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值