Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/image-processing/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 将图像转换为黑白(非灰度)_Java_Image Processing - Fatal编程技术网

Java 将图像转换为黑白(非灰度)

Java 将图像转换为黑白(非灰度),java,image-processing,Java,Image Processing,您好,我正在将一幅图像从彩色转换为纯黑白,结果是一幅深色图像。我不明白原因。以下是我的代码,它受到了其他代码的启发。 任何指导都是有益的 BufferedImage coloredImage = ImageIO.read(new File("/home/discusit/ninja.png")); BufferedImage blackNWhite = new BufferedImage(coloredImage.getWidth(),coloredImage.getHeight(),Buffe

您好,我正在将一幅图像从彩色转换为纯黑白,结果是一幅深色图像。我不明白原因。以下是我的代码,它受到了其他代码的启发。 任何指导都是有益的

BufferedImage coloredImage = ImageIO.read(new File("/home/discusit/ninja.png"));
BufferedImage blackNWhite = new BufferedImage(coloredImage.getWidth(),coloredImage.getHeight(),BufferedImage.TYPE_BYTE_BINARY);
Graphics2D graphics = blackNWhite.createGraphics();
graphics.drawImage(blackNWhite, 0, 0, null);
我不明白我做错了什么。任何其他使用开源库的想法都可以

工作:

BufferedImage coloredImage = ImageIO.read(new File("/home/abc/ninja.png"));
BufferedImage blackNWhite = new BufferedImage(coloredImage.getWidth(),coloredImage.getHeight(),BufferedImage.TYPE_BYTE_BINARY);
Graphics2D graphics = blackNWhite.createGraphics();
graphics.drawImage(coloredImage, 0, 0, null);

ImageIO.write(blackNWhite, "png", new File("/home/abc/newBlackNWhite.png"));

实际上,您并没有将彩色图像转换为黑白图像;您正在创建一个与旧图像大小相同的新空白图像。您实际上需要做一些事情来处理旧图像。

我认为这只是一个输入错误:

graphics.drawImage(blackNWhite, 0, 0, null);
blackNWhite
替换为要处理的图像

graphics.drawImage(coloredImage, 0, 0, null);
现在,
blackNWhite
包含了
coloredImage
的黑白版本。也许你需要另一个任务:

coloredImage = blackNWhite;

要真正将图像转换为黑白,可以迭代每个像素并平均该位置的颜色。比如说

for(int x=0;x<width;x++){
    for(int y=0;y<height;y++){
        Color color = getPixelAt(x,y);
        int newColor = (color.getRed()+color.getGreen()+color.getBlue())/3;
        Color newPixelColor = new Color(newColor,newColor,newColor);
        //set new pixel
    }
}
编辑:

如果“黑白”指的是只有黑色像素和白色像素的图像,则可以将平均值小于阈值的所有像素指定为黑色,将平均值大于阈值的所有像素指定为白色。像这样:

static final int BLACK = 0;
static final int WHITE = 255;
int threshold = 127;
if(newColor < threshold)
    newColor = BLACK;
else
    newColor = WHITE;
static final int BLACK=0;
静态最终整数白=255;
int阈值=127;
if(新颜色<阈值)
新颜色=黑色;
其他的
新颜色=白色;

如果您想控制所谓的阈值过程,这里有一个现成的代码片段。从128开始作为阈值,然后得到其他方法所做的

/**
*根据给定的阈值将图像转换为二进制图像
*@param image要转换的图像。仍然没有受到影响。
*@param threshold[0255]中的阈值
*@返回一个新的BuffereImage实例,其类型为_BYTE_GRAY,只有0和255个
*/
公共静态BuffereImage thresholdImage(BuffereImage,int threshold){
BuffereImage结果=新的BuffereImage(image.getWidth(),image.getHeight(),BuffereImage.TYPE_BYTE_GRAY);
result.getGraphics().drawImage(image,0,0,null);
WritableRaster raster=result.getRaster();
int[]像素=新int[image.getWidth()];
对于(int y=0;y
您可以使用Catalano框架,包含多个用于图像处理的过滤器,您可以使用阈值过滤器转换为黑白。见下文:

查看模型

import java.awt.*;
import java.awt.image.BufferedImage;

public class ImageTool {
    public static void toBlackAndWhite(BufferedImage img) {
        toBlackAndWhite(img, 50);
    }
    public static void toBlackAndWhite(BufferedImage img, int precision) {
        int w = img.getWidth();
        int h = img.getHeight();

        precision = (0 <= precision && precision <= 100) ? precision : 50;

        int limit = 255 * precision / 100;

        for(int i = 0, j; i < w; ++i) {
            for(j = 0; j < h; ++j) {
                Color color = new Color(img.getRGB(i, j));
                if(limit <= color.getRed() || limit <= color.getGreen() || limit <= color.getBlue()) {
                    img.setRGB(i, j, Color.WHITE.getRGB());
                } else {
                    img.setRGB(i, j, Color.BLACK.getRGB());
                }
            }
        }
    }
}

您将看到结果。

有许多方法仅使用黑白像素来表示图像,并用于不同的应用,如压缩、印象、艺术和分析

下面的示例使用并显示了三种不同的黑白图像表示方法。在半色调技术的情况下,会产生一种灰色阴影的错觉,但如果放大,您将只看到白色和黑色像素

输入:

阈值化:

半色调:

半色调缩放:

圆圈:


@约翰森902:不,等等!我误解了
createGraphics
;你的答案更好!我的也没有提到任何关于
createGraphics
。@user2357112我的错我放错了代码,我正在编辑一篇文章,现在放对了代码。。。抱歉给您带来不便。完成…:):)在写回图像时,我必须使用旧图像(彩色图像).:)@user2357112我正在向我的问题添加代码谢谢你的提示…:)非常感谢您提供的新信息,我不知道这个框架。它实际上是将其转换为灰度值,而不是黑白。对于后者,您需要对图像设置阈值。@Mathias谢谢,我不知道我是如何在标题中犯下这个错误的!我已经更正了我的答案。我发现颜色成分的权重很有趣。
FastBitmap fb = new FastBitmap(bufferedImage);

Grayscale g = new Grayscale();
g.applyInPlace(fb);

Threshold t = new Threshold(150);
t.applyInPlace(fb);

//Show the results
JOptionPane.showMessageDialog(null, fb.toIcon());

//or if u prefer retrieve the bufferedImage you need to do
bufferedImage = fb.toBufferedImage();
import java.awt.*;
import java.awt.image.BufferedImage;

public class ImageTool {
    public static void toBlackAndWhite(BufferedImage img) {
        toBlackAndWhite(img, 50);
    }
    public static void toBlackAndWhite(BufferedImage img, int precision) {
        int w = img.getWidth();
        int h = img.getHeight();

        precision = (0 <= precision && precision <= 100) ? precision : 50;

        int limit = 255 * precision / 100;

        for(int i = 0, j; i < w; ++i) {
            for(j = 0; j < h; ++j) {
                Color color = new Color(img.getRGB(i, j));
                if(limit <= color.getRed() || limit <= color.getGreen() || limit <= color.getBlue()) {
                    img.setRGB(i, j, Color.WHITE.getRGB());
                } else {
                    img.setRGB(i, j, Color.BLACK.getRGB());
                }
            }
        }
    }
}
for(Integer i : new Integer[] {0, 30, 70, 100}) {
    BufferedImage img = ImageIO.read(new File("in.png"));
    ImageTool.toBlackAndWhite(img, i);
    ImageIO.write(img, "png", new File("out_" + i + ".png"));
}
import static marvin.MarvinPluginCollection.*;

public class BlackAndWhiteExamples {

    public BlackAndWhiteExamples(){
        MarvinImage original = MarvinImageIO.loadImage("./res/lena3.jpg");
        MarvinImage output = original.clone();
        thresholding(original, output, 190);
        // 1. Thresholding
        MarvinImageIO.saveImage(output, "./res/lena3_thresholding.png");
        halftoneErrorDiffusion(original, output);
        // 2. Halftoning
        MarvinImageIO.saveImage(output, "./res/lena3_error_diffusion.png");
        // 3. Circles
        halftoneCircles(original, output, 15, 0, 0);
        MarvinImageIO.saveImage(output, "./res/lena3_circles.png");
    }

    public static void main(String[] args) {    new BlackAndWhiteExamples();    }
}