如何在Java中找到图像的阈值?

时间:2018-09-11 14:30:48

标签: java

这是我的第一篇文章,所以如果我错过了一些事情,请原谅。基本上,我提供了将普通图像转换为灰度图像的代码,然后需要将灰度图像转换为阈值图像,但是在执行此操作之前,我需要先确定阈值本身的值,但似乎无法确定怎么做。

import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.*;
import javax.imageio.ImageIO;
import javax.swing.JFrame;

public class GrayScale {
BufferedImage image;
int width;
int height;

public GrayScale() {
    try {
        File input = new File("mysteryBird.jpg");
        image = ImageIO.read(input);
        width = image.getWidth();
        height = image.getHeight();
        int[] hist = new int[256];
        for (int i = 0; i < height; i++) {
            for (int j = 0; j < width; j++) {
                Color c = new Color(image.getRGB(j, i));
                int red = (int) (c.getRed() * 0.299);
                int green = (int) (c.getGreen() * 0.587);
                int blue = (int) (c.getBlue() * 0.114);
                Color newColor = new Color(red + green + blue, red + green + blue, red + green + blue);
                image.setRGB(j, i, newColor.getRGB());
            }
        }
        File ouptut = new File("grayscale.jpg");
        ImageIO.write(image, "jpg", ouptut);
    } catch (Exception e) {
    }
}

0 个答案:

没有答案