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 开发者_StackOverflow中文版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?
becomes 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.
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:
Output:There are some handy image processing algorithms for doing this over at AForge.NET.
See BradleyLocalThresholding for adaptive thresholding.
精彩评论