0

I want to recognize the numbers in the Pic1.

I did some work on it and it returns to pic2

Here is my code:

package captchadecproj;

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

/*
 * @author Mr__Hamid
 */



public class NewClass {

public static void main(String args[]) throws IOException {
    int width = 110;
    int heigth = 40;
    BufferedImage image1 = new BufferedImage(width, heigth, BufferedImage.TYPE_INT_RGB);
    BufferedImage num1 = new BufferedImage(width, heigth, BufferedImage.TYPE_INT_RGB);
    BufferedImage image = null;
    File f = null;
    try {
        f = new File("E:\\Desktop 2\\Captcha Project\\CaptchaDecoder\\captchaDecProj\\167.png");
        image = new BufferedImage(width, heigth, BufferedImage.TYPE_INT_ARGB);
        image = ImageIO.read(f);
        System.out.println("Read!");
    } catch (IOException e) {
        System.out.println("Error" + e);
    }
    int[] pixel = null;
    for (int y = 0; y < image.getHeight(); y++) {
        for (int x = 0; x < image.getWidth(); x++) {
            pixel = image.getRaster().getPixel(x, y, new int[3]);
            if (pixel[0] < 30 & pixel[1] > 130 & pixel[2] < 110 & pixel[2] > 60) {
                image1.setRGB(x, y, Integer.parseInt("ffffff".trim(), 16));
                System.out.println(pixel[0] + " - " + pixel[1] + " - " + pixel[2] + " - " + (image.getWidth() * y + x));
            } else {
                image1.setRGB(x, y, 1);
                System.out.println(pixel[0] + " - " + pixel[1] + " - " + pixel[2] + " - " + (image.getWidth() * y + x));
            }

        }
    }

    try {
        f = new File("D:\\Original.jpg");
        ImageIO.write(image, "jpg", f);
        f = new File("D:\\black&White.jpg");
        ImageIO.write(image1, "jpg", f);
        System.out.println("Writed");
    } catch (IOException e) {
        System.out.println("Error" + e);
    }
}
}

I have two questions:

How can I split these numbers?

How can I recognize which one is my number?

For example in the uploaded pic: 7, 1, 6

Lewis Norton
  • 6,911
  • 1
  • 19
  • 29
Wooopsa
  • 320
  • 1
  • 6
  • 22
  • `How can I split these numbers?` convert your image to bw (wite background, black text) and then split on column with smallest amount of black (0 pixels) `How can I recognize which one is my number?` – user902383 Jun 30 '15 at 09:14
  • My code converts the image to bw(White text and black background). then what? – Wooopsa Jun 30 '15 at 09:28
  • then create histogram, count amount of non-background colour in each column. then extract from your image only areas with non-background colour. this will be your section with number (or any other character) – user902383 Jun 30 '15 at 09:43
  • would you mind share some code? – Wooopsa Jun 30 '15 at 10:26

1 Answers1

0

This is answer for first question, which is how to split numbers. I recommend you something, is to convert your image to two dimensional array, then all operations will be perform much quicker than when you use BufferedImage.

BufferedImage image = ImageIO.read(new URL("https://i.stack.imgur.com/QaTj5.jpg"));
    int startPos = 0, lastValue = 0;
    Set<Integer> colours = new HashSet<>();
    for (int x = 0; x < image.getWidth(); x++) {
        int histValue = 0;

        for (int y = 0; y < image.getHeight(); y++) {
            colours.add(image.getRGB(x, y) );
            if (image.getRGB(x, y) == 0xffffFFFF) {
                histValue++;
            }
        }

        if (histValue == 0 && lastValue == 0) {
            startPos = x;
        } else if (histValue == 0 && lastValue != 0) {
            BufferedImage segment = image.getSubimage(startPos, 0, x
                    - startPos, image.getHeight());
            ImageIO.write(segment, "jpg", new File("Segment" + startPos
                    + ".jpg"));

        }
        lastValue = histValue; 
    }
    if (lastValue!=0){
        BufferedImage segment = image.getSubimage(startPos, 0, image.getWidth()
                - startPos, image.getHeight());
        ImageIO.write(segment, "jpg", new File("Segment" + startPos
                + ".jpg"));
    }

Now all what you need to do is to find some decent algorithm for ocr.

user902383
  • 8,420
  • 8
  • 43
  • 63
  • The Splited Pictures contain gray pixels. but i set them as white. Color myWhite = new Color(255, 255, 255); int rgb = myWhite.getRGB(); image1.setRGB(x, y, rgb); How Can i get a picture with only black and white pixels? – Wooopsa Jul 01 '15 at 09:46
  • @Hamid have a look here http://stackoverflow.com/questions/18932415/how-to-convert-a-bufferedimage-to-black-and-white or here http://stackoverflow.com/questions/14513542/convert-image-to-black-white – user902383 Jul 01 '15 at 10:16
  • @Hamid by addition there are some issues with your code, first, as you converting image to jpg, because compression algorithm you are loosing exact colours, other thing is your colours. black is 0 not 1. but as i suggest, dont work with image, work with array. when i was doing something with image processing, i had 2d int array, where 1 was white and 0 was black – user902383 Jul 01 '15 at 10:19
  • Thank you for helping me out. but i don't get what you from working with array. would you mind share some code? – Wooopsa Jul 01 '15 at 18:57
  • @Hamid what i meant is instead setting colour in image, create helper 2 dimensional array, and update value there . so instead doing `image1.setRGB(x, y,newValue);` you will do `helper[x][y] = newValue` by doing that simple change in one of my applications i increased framerate from one frame fer 4 seconds to 15frames per second – user902383 Jul 02 '15 at 08:31
  • what is the helper's type you created here, dude? Sorry for the questions :( – Wooopsa Jul 02 '15 at 12:25
  • @Hamid as i said, 2d int array `int[][] helper = new int[image.getWidth()][image.getHeight]` it is much quicker and easier – user902383 Jul 02 '15 at 13:10
  • @Hamid and one more thing `Integer.parseInt("ffffff".trim(), 16)` you can write as `0xffffff` – user902383 Jul 02 '15 at 13:33
  • how can i remove the black parts from up and down of my numbers? – Wooopsa Jul 11 '15 at 07:01
  • @Hamid trim each segment and remove rows from top and bottom which does not contain any bright pixel – user902383 Jul 13 '15 at 08:32