将负片图像转换为正片

Bit*_*map 12 c++ java image

我有旧的底片,我扫描到我的电脑上.我想写一个小程序将负图像转换为正状态.

我知道有几个图像编辑器应用程序,我可以使用它来实现这种转换,但我正在研究如何通过一个小应用程序操纵像素自己转换它们.

有人能给我一个良好的开端吗?如果可能的话,示例代码也将非常受欢迎.

kba*_*kba 30

我刚刚写了一个实例.给出以下输入图像img.png.

img.png

输出将是一个新的形象invert-img.png

反转,img.png

import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;

class Convert
{
    public static void main(String[] args)
    {
        invertImage("img.png");
    }

    public static void invertImage(String imageName) {
        BufferedImage inputFile = null;
        try {
            inputFile = ImageIO.read(new File(imageName));
        } catch (IOException e) {
            e.printStackTrace();
        }

        for (int x = 0; x < inputFile.getWidth(); x++) {
            for (int y = 0; y < inputFile.getHeight(); y++) {
                int rgba = inputFile.getRGB(x, y);
                Color col = new Color(rgba, true);
                col = new Color(255 - col.getRed(),
                                255 - col.getGreen(),
                                255 - col.getBlue());
                inputFile.setRGB(x, y, col.getRGB());
            }
        }

        try {
            File outputFile = new File("invert-"+imageName);
            ImageIO.write(inputFile, "png", outputFile);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

如果要创建单色图像,可以将计算结果更改为col:

int MONO_THRESHOLD = 368;
if (col.getRed() + col.getGreen() + col.getBlue() > MONO_THRESHOLD)
    col = new Color(255, 255, 255);
else
    col = new Color(0, 0, 0);
Run Code Online (Sandbox Code Playgroud)

以上将为您提供以下图像

单色,img.png

您可以进行调整MONO_THRESHOLD以获得更令人满意的输出.增加数字会使像素变暗,反之亦然.