2014-01-13 27 views
2

我想爲OCR準備圖像,我用GPUImage做到這一點,代碼做工精細,直到我的裁剪圖像裁剪!我得到了壞結果之後......ios GPUImage,使用小尺寸圖像處理圖像效果不佳?

作物面積: https://www.dropbox.com/s/e3mlp25sl6m55yk/IMG_0709.PNG

壞結果=( https://www.dropbox.com/s/wtxw7li6paltx21/IMG_0710.PNG

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

    //first off, try to grayscale the image using iOS core Image routine 
    UIImage * grayScaledImg = [self grayImage:sourceImage]; 

    GPUImagePicture *imageSource = [[GPUImagePicture alloc] initWithImage:grayScaledImg]; 

    GPUImageAdaptiveThresholdFilter *stillImageFilter = [[GPUImageAdaptiveThresholdFilter alloc] init]; 
    stillImageFilter.blurRadiusInPixels = 8.0; 
    [stillImageFilter prepareForImageCapture]; 

    [imageSource addTarget:stillImageFilter]; 
    [imageSource processImage]; 
    UIImage *retImage = [stillImageFilter imageFromCurrentlyProcessedOutput]; 

    [imageSource removeAllTargets]; 

    return retImage; 
} 


+ (UIImage *) grayImage :(UIImage *)inputImage 
{ 
    // Create a graphic context. 
    UIGraphicsBeginImageContextWithOptions(inputImage.size, NO, 1.0); 
    CGRect imageRect = CGRectMake(0, 0, inputImage.size.width, inputImage.size.height); 

    // Draw the image with the luminosity blend mode. 
    // On top of a white background, this will give a black and white image. 
    [inputImage drawInRect:imageRect blendMode:kCGBlendModeLuminosity alpha:1.0]; 

    // Get the resulting image. 
    UIImage *outputImage = UIGraphicsGetImageFromCurrentImageContext(); 
    UIGraphicsEndImageContext(); 
    return outputImage; 
} 

UPDATE:

在此期間,當可以裁剪圖像,這樣做最近 多的寬度爲8個像素,你應該看到正確的結果

感謝ü@Brad拉爾森!我調整圖像的寬度與8最接近的倍數,得到我想要的

-(UIImage*)imageWithMultiple8ImageWidth:(UIImage*)image 
{ 
    float fixSize = next8(image.size.width); 

    CGSize newSize = CGSizeMake(fixSize, image.size.height); 
    UIGraphicsBeginImageContext(newSize); 
    [image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)]; 
    UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext(); 
    UIGraphicsEndImageContext(); 

    return newImage; 
} 

float next8(float n) { 

    int bits = (int)n & 7; // give us the distance to the previous 8 
    if (bits == 0) 
     return (float)n; 
    return (float)n + (8-bits); 
} 

回答

2

之前,我甚至去的核心問題在這裏,我要指出的是,GPUImageAdaptiveThresholdFilter已經做了轉換爲灰度作爲第一步,所以上面的代碼-grayImage:是不必要的,只會減慢速度。您可以刪除所有代碼,並將您的輸入圖像直接傳遞給自適應閾值過濾器。

我相信這裏的問題是對GPUImagePicture在圖像數據中拉取方式的最近一系列更改。看起來,不是8像素寬的倍數的圖像在導入時最終看起來像上面那樣。有人提出了一些修正,但是如果從存儲庫(而不是CocoaPods,相對於GitHub存儲庫經常過期)的最新代碼仍然這樣做,可能需要完成一些更多的工作。

與此同時,當您裁剪圖像時,請按8個像素的最接近倍數寬度進行裁剪,然後您應看到正確的結果。

+0

thanx快速重播!我使用CocoaPods的代碼,我將檢查最後的gitHub版本並留下反饋! '-grayImage:'這是我的不好,已經刪除它=)) – 3a4oT

+0

我使用的是最新版本 - 0.1.2!謝謝你!我更新我的帖子! – 3a4oT

+0

@ 3a4oT - 是的,看起來它仍然需要修復。我會研究這個。 –