我正在尝试为OCR准备图像,我使用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;
}
更新:
在此期间,裁剪图像时,请将其放到最近的位置 宽度为8像素的倍数,你应该看到正确的结果
谢谢你@Brad Larson!我将图像宽度调整为最接近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);
}
答案 0 :(得分:2)
在我解决这里的核心问题之前,我应该指出GPUImageAdaptiveThresholdFilter作为第一步已经转换为灰度,因此上面的-grayImage:
代码是不必要的,只会减慢速度。您可以删除所有代码,只需将输入图像直接传递给自适应阈值过滤器。
我认为这里的问题是最近对GPUImagePicture提取图像数据的方式进行了一系列更改。看起来不是8像素宽的倍数的图像在导入时最终看起来像上面那样。有人提出了一些修复方法,但是如果来自存储库的最新代码(不是CocoaPods,它通常是相对于GitHub存储库过时的)仍在执行此操作,则可能需要完成更多工作。
在此期间,当您裁剪图像时,请将其设置为最接近8像素宽的倍数,您应该会看到正确的结果。