BradLarson / GPUImage

An open source iOS framework for GPU-based image and video processing
http://www.sunsetlakesoftware.com/2012/02/12/introducing-gpuimage-framework
BSD 3-Clause "New" or "Revised" License
20.25k stars 4.61k forks source link

GPUImageLuminanceThresholdFilter - How to handle binarization of image with varying luminance #1163

Open vividcode opened 11 years ago

vividcode commented 11 years ago

I have these images:

Image 1: sample7

Image 2: sample5

I can't seem to find proper threshold value to handle Image 1 entirely for binarization. If ever I find, Image 2 becomes almost invisible.

I am using GPUImageLuminanceThresholdFilter code from this answer - http://stackoverflow.com/a/10939150/1506363.

One logical way seems to be split them into half to handle with separate threshold values. But then there arises the question of how be adaptive about it. My ultimate aim is to perform OCR and that is where most of my efforts are focused, so some easier binarization solution is highly appreciated.

vividcode commented 11 years ago

Posted question on SO: http://stackoverflow.com/questions/18630842/ios-binarization-for-ocr

vividcode commented 11 years ago

Final code I am using is below - and it provides good result for image 1, however image 2 looks noisy, and doesn't go well with OCR:

+(UIImage *)binarize : (UIImage *) sourceImage
{
    UIImage * grayScaledImg = [self toGrayscale:sourceImage];   //grayscale using core image technique
    GPUImagePicture *imageSource = [[GPUImagePicture alloc] initWithImage:grayScaledImg];
    GPUImageAdaptiveThresholdFilter *stillImageFilter = [[GPUImageAdaptiveThresholdFilter alloc] init];
    stillImageFilter.blurSize = 3.0;    

    [imageSource addTarget:stillImageFilter];   
    [imageSource processImage];        

    UIImage *imageWithAppliedThreshold = [stillImageFilter imageFromCurrentlyProcessedOutput];
    //  UIImage *destImage = [thresholdFilter imageByFilteringImage:grayScaledImg];
    return imageWithAppliedThreshold;
}