2012-10-24 70 views
1

我想將從相機拍攝的圖像轉換爲黑白色並打印出來。此圖像僅取自文檔或紙張。 所以我用瞭如下代碼:如何刪除光線和陰影通過GPUImage篩選器

{ 

GPUImagePicture *stillImageSource = [[GPUImagePicture alloc] initWithImage: srcImg ];    

GPUImageAdaptiveThresholdFilter *stillImageFilter = [[GPUImageAdaptiveThresholdFilter alloc] init]; 
stillImageFilter.blurSize = 1; 


[stillImageSource addTarget:stillImageFilter]; 
[stillImageSource processImage]; 

UIImage *outputImg = [stillImageFilter imageFromCurrentlyProcessedOutput]; 
[stillImageSource release]; 
[stillImageFilter release]; 


} 

但輸出圖像有這麼多的鋸齒,因爲圖像是完全二值化,它只是需要局部二值化。所以我首先將源圖像轉換爲灰度,然後給出最大對比度,圖像看起來非常好,但光影無法移除。 任何想法,謝謝。

回答

0

嘗試GPUImageAdaptiveThresholdFilter後應用GPUImageLuminanceThresholdFilter,並與 'blurSize' 屬性發揮

-(UIImage *)doBinarize:(UIImage *)sourceImage 
{ 

GPUImagePicture *imageSource = sourceImage; 

    GPUImageAdaptiveThresholdFilter *stillImageFilter = [[GPUImageAdaptiveThresholdFilter alloc] init]; 
    stillImageFilter.blurRadiusInPixels = 8.0;//play with it 

    GPUImageLuminanceThresholdFilter *stillImageFilter2 = [[GPUImageLuminanceThresholdFilter alloc] init]; 
    stillImageFilter2.threshold = 0.5f;// 
    [stillImageFilter addFilter:stillImageFilter2]; 

    [stillImageFilter prepareForImageCapture]; 



    [imageSource addTarget:stillImageFilter]; 

    [imageSource processImage]; 


    UIImage *retImage = [stillImageFilter imageFromCurrentlyProcessedOutput]; 

    [imageSource removeAllTargets]; 

    return retImage; 
}