将图像转换为黑白IOS? [英] Convert image to Black and white IOS?

查看:203
本文介绍了将图像转换为黑白IOS?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现了很多代码可以将图像转换为纯黑白。

I have found lots of code to convert image to pure black and white. but none this working.

我尝试了这段代码,但是将图像转换为灰度而不是黑白。

I have tried this code but its convert image to grayscale not black and white.

  -(UIImage *)convertOriginalImageToBWImage:(UIImage *)originalImage
{
    UIImage *newImage;
    CGColorSpaceRef colorSapce = CGColorSpaceCreateDeviceGray();
    CGContextRef context = CGBitmapContextCreate(nil, originalImage.size.width * originalImage.scale, originalImage.size.height * originalImage.scale, 8, originalImage.size.width * originalImage.scale, colorSapce, kCGImageAlphaNone);
    CGContextSetInterpolationQuality(context, kCGInterpolationHigh);
    CGContextSetShouldAntialias(context, NO);
    CGContextDrawImage(context, CGRectMake(0, 0, originalImage.size.width, originalImage.size.height), [originalImage CGImage]);

    CGImageRef bwImage = CGBitmapContextCreateImage(context);
    CGContextRelease(context);
    CGColorSpaceRelease(colorSapce);

    UIImage *resultImage = [UIImage imageWithCGImage:bwImage];
    CGImageRelease(bwImage);

    UIGraphicsBeginImageContextWithOptions(originalImage.size, NO, originalImage.scale);
    [resultImage drawInRect:CGRectMake(0.0, 0.0, originalImage.size.width, originalImage.size.height)];
    newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();


    return newImage;
}

结果图像-------------- ----------------------------->预期图像

Result image -------------------------------------------> Expected Image

-

推荐答案

您将必须阈值。由于您的输入图像是明亮背景上的深色文本,因此应该简单明了。当您对灰度图像设置阈值时,基本上是在说强度值高于阈值t的所有像素应为白色,而所有其他像素应为黑色 。这是一种标准的图像处理技术,通常在图像预处理中使用。

You will have to threshold the image after you have converted it to grayscale. Since your input image is dark text on a bright background, this should be straight forward. When you threshold a grayscale image, you are basically saying that "all pixels with intensity values above a threshold, t, should be white, while all other pixels should be black". This is a standard image processing technique, commonly used in image pre-processing.

如果您打算进行图像处理,我强烈建议Brad Larson的 GPUImage ,这是为此目的而由硬件驱动的Objective-C框架。

If you intend to do image processing, I highly recommend Brad Larson's GPUImage, which is a hardware-driven Objective-C framework made for the purpose. It comes with thresholding filters ready for use.

存在各种不同的阈值算法,但是如果您的输入图像始终与给定的示例相似,我认为没有理由使用更复杂的方法。但是,如果存在照明不均匀,噪声或其他干扰因素的风险,请使用自适应阈值。据我所知,GPUImage的阈值过滤器是自适应的。

There exist various different thresholding algorithms, but if your input images are always similar to the example given, I see no reason to use a more sophisticated approach. If, however, there's a risk of un-even illumination, noise or other disturbing factors, using adaptive thresholding or another dynamic algorithm is recommended. As far as I remember, GPUImage's tresholding filter is adaptive.

这篇关于将图像转换为黑白IOS?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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