将 UIImage 转换为黑白而不是灰度以使用 tesseract [英] Converting a UIImage black'n white and not grayscale for using tesseract

查看:38
本文介绍了将 UIImage 转换为黑白而不是灰度以使用 tesseract的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的 iPhone 应用程序中使用了 tesseract.

I'm using tesseract in my iPhone application.

我在我的图像上尝试了几个过滤器以将其转换为灰度图像,但是我希望得到设置阈值的结果,以便图像内的唯一像素是黑色或白色.

I tried several filters on my image for converting it to a grayscale image, however I'd like to have the result where a threshold is being set so that the only pixels which are inside the image are black or white.

我成功地使用了苹果灰度过滤器,它给出了适当的结果.然而,它仍然是一个 16 位图像(如果我错了,请纠正我).我目前使用的过滤如下:

I succeeded with using apples grayscale filter which gives the appropriate result. However it's still a 16 bit image (correct me if I'm wrong). The filtering which I'm using at the moment is as follows:

- (UIImage *) grayishImage:(UIImage *)i {

    // Create a graphic context.
    UIGraphicsBeginImageContextWithOptions(i.size, YES, 1.0);
    CGRect imageRect = CGRectMake(0, 0, i.size.width, i.size.height);
// Draw the image with the luminosity blend mode.
[i drawInRect:imageRect blendMode:kCGBlendModeLuminosity alpha:1.0];
    // Get the resulting image.
    UIImage *filteredImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    return filteredImage;
}

谁能给我提供过滤器来获得纯黑白像素而不是灰度图像?

Can anyone supply me with the filter to get pure black and white pixels and not grayscale images?

推荐答案

可能最快的方法是使用 OpenGL ES 2.0 着色器将阈值应用于您的图像.我的 GPUImage 框架对此进行了封装,因此您无需担心幕后的更多技术问题.

Probably the fastest way to do this would be to use OpenGL ES 2.0 shaders to apply the threshold to your image. My GPUImage framework encapsulates this so that you don't need to worry about the more technical aspects behind the scenes.

使用 GPUImage,您可以使用 GPUImageLuminanceThresholdFilter 和如下代码获取 UIImage 的阈值版本:

Using GPUImage, you could obtain a thresholded version of your UIImage using a GPUImageLuminanceThresholdFilter and code like the following:

GPUImagePicture *stillImageSource = [[GPUImagePicture alloc] initWithImage:inputImage];
GPUImageLuminanceThresholdFilter *stillImageFilter = [[GPUImageLuminanceThresholdFilter alloc] init];
stillImageFilter.threshold = 0.5;
[stillImageSource addTarget:stillImageFilter];
[stillImageFilter useNextFrameForImageCapture];
[stillImageSource processImage];

UIImage *imageWithAppliedThreshold = [stillImageFilter imageFromCurrentFramebuffer];

您可以将彩色图像传递给它,因为这会自动从每个像素中提取亮度并将阈值应用于该像素.高于阈值的任何像素变为白色,低于阈值的任何像素变为黑色.您可以调整阈值以满足您的特定条件.

You can just pass your color image into this, because this automatically extracts the luminance from each pixel and applies the threshold to that. Any pixel above the threshold goes to white, and any one below that is black. You can adjust the threshold to meet your particular conditions.

但是,对于您要传递到 Tesseract 的更好的选择是我的 GPUImageAdaptiveThresholdFilter,它可以像 GPUImageLuminanceThresholdFilter 一样使用,只是没有阈值.自适应阈值处理基于当前像素周围的 9 像素区域执行阈值操作,并针对局部照明条件进行调整.这是专门为帮助 OCR 应用程序而设计的,所以它可能是这里的方法.

However, an even better choice for something you're going to pass into Tesseract would be my GPUImageAdaptiveThresholdFilter, which can be used in the same way as the GPUImageLuminanceThresholdFilter, only without a threshold value. The adaptive thresholding does a thresholding operation based on a 9 pixel region around the current pixel, adjusting for local lighting conditions. This is specifically designed to help with OCR applications, so it might be the way to go here.

可以在这个答案中找到来自这两种过滤器的示例图像.

Examples images from both types of filters can be found in this answer.

请注意,通过 UIImage 的往返比处理原始数据要慢,因此这些过滤器在处理直接视频或电影源时要快得多,并且可以针对这些输入实时运行.我还有一个原始像素数据输出,与 Tesseract 一起使用可能会更快.

Note that the roundtrip through UIImage is slower than handling raw data, so these filters are much faster when acting on direct video or movie sources, and can run in realtime for those inputs. I also have a raw pixel data output, which might be faster for use with Tesseract.

这篇关于将 UIImage 转换为黑白而不是灰度以使用 tesseract的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆