Haoest
Haoest

Reputation: 13906

How to convert a gray image of a letter to binary without losing the main contour?

I have a small gray-scale image with a number on it. In order to try my OCR method on it I have to convert it into binary.

If I use cvThreshold with 127 as threshold the image looks all screwed up because of the gradient around the skeletons of the number. I tried but could not find any image sharpening functions to use before applying the threshold.

Does anyone have pointers please?

original gray scale image becomes enter image description here which is crude.

EDIT: by binary, I mean binary image, where a pixel in the image is either 0 (black) or 255 (white).

EDIT2: Oh, looking at the revision log made me giggle.

Upvotes: 2

Views: 812

Answers (2)

karlphillip
karlphillip

Reputation: 93458

Play with the threshold variable first. You may find a result that satisfies you without having to add any more processing to your application. What about using 120, 110 or 100?

Anyway, I didn't got the same output as you using 127 as parameter. Maybe your are doing something different on your side. Check my code:

IplImage* input_img = cvLoadImage("6.png", CV_LOAD_IMAGE_UNCHANGED);
if(!input_img)
{
    std::cout << "ERROR: Failed to load input image" << std::endl;
    return -1;
}

cvThreshold(input_img, input_img, 127, 255, CV_THRESH_BINARY);

if( !cvSaveImage("out.png", input_img) )
{
    std::cout << "ERROR: Failed to write image file" << std::endl;
}    

cvReleaseImage(&input_img);

Input: enter image description here Output: enter image description here

Upvotes: 6

James Allen
James Allen

Reputation: 819

There are some handy image processing algorithms for doing this over at AForge.NET.

See BradleyLocalThresholding for adaptive thresholding.

Upvotes: 2

Related Questions