将BufferedImage转换为二进制文件,使图像完全变黑

时间:2015-11-18 18:41:16

标签: java ocr bufferedimage grayscale

我正在尝试创建一个OCR来读取旧相机实时的7段。这是我的预处理(忽略随机导入尝试了一堆选项)现在我正在将图像从橙色和灰色更改为负并将其更改为灰度,一旦我尝试将灰度级更改为二进制,它只会使黑色我无法用这段代码来解决这个问题。

import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.color.*;
import java.awt.image.BufferedImage;
import java.awt.image.ColorConvertOp;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;

public class PreProcess2 {

public static void main(String [] args) {
    BufferedImage img = null;
    File dir = new File ("Directory/file/example photo");
    File[] dirList = dir.listFiles();
    for (int i = 1; i < dirList.length; i++) {
        try{
            img = ImageIO.read(dirList[i].getAbsoluteFile());
            int d = 2;
            int iw = img.getWidth();
            int ih = img.getHeight();
            int xb = ((((iw/d)/d)+(iw/d)));
            int yb = ((((ih/d)/d)+(ih/d)));


            try {
                //Isolates Width
                for (int x = xb ; x <= (iw-1); x++){
                    //Isolates Height
                    for (int y = yb; y <= (ih-1); y++){
                        int p = img.getRGB(x, y);

                        int a = (p>>24)&0xff;
                        int r = (p>>16)&0xff;
                        int g = (p>>8)&0xff;
                        int b = p&0xff;

                        //subtract RGB from 255
                        r = 255 - r;
                        g = 255 - g; 
                        b = 255 - b;

                        //set new RGB from 255
                        p = (a<<24) | (r<<16) | (g<<8) | b; 
                        img.setRGB(x, y, p);
                        }
                    }
                    //Prints out if pixel is out of bounds
            }catch (ArrayIndexOutOfBoundsException e) {
                System.out.println("out of bounds");
                }
            BufferedImage result = new BufferedImage(
                    img.getWidth(),
                    img.getHeight(),
                    BufferedImage.TYPE_BYTE_GRAY);
            Graphics g = result.getGraphics();
            g.drawImage(img, 0, 0, null);
            g.dispose();

            BufferedImage gray = new BufferedImage(result.getWidth(), result.getHeight(),
                    BufferedImage.TYPE_BYTE_BINARY);

            BufferedImage croppedImage = gray.getSubimage(615, 445, 110, 35);
            //Saves file to processed file location
            File outputfile = new File(dir +"/Processed/" + dirList[i].getName());
            ImageIO.write(croppedImage, "jpg", outputfile);

            //Print out if not able to retrieve file
        } catch (IOException e) {
            System.out.println("Done");
        }
    }
}
}

Blacked out

Negative

更新

BufferedImage binary = new BufferedImage(iw, ih, BufferedImage.TYPE_BYTE_BINARY);
                Graphics biG = binary.getGraphics();
                biG.drawImage(gray, 0, 0, null);
                biG.dispose();

在同事的帮助下找到了部分答案,现在我需要弄清楚如何清理图像中的噪音,任何想法?

enter image description here

0 个答案:

没有答案
相关问题