简体   繁体   English

将RGB图像转换为1通道图像(黑/白)

[英]Convert RGB image to 1 channel image (black/white)

How can I convert RGB image to 1 channel image (black/white) using ios5? 如何使用ios5将RGB图像转换为1通道图像(黑/白)?

Input image is usually a photo of a book page. 输入图像通常是书页的照片。 Goal is to reduce the size of a photocopy by converting it to the 1 channel image. 目标是通过将复印件转换为1通道图像来缩小复印件的尺寸。

If I understand your question, you want to apply a black and white thresholding to the image based on a pixel's luminance. 如果我理解你的问题,你想根据像素的亮度对图像应用黑白阈值。 For a fast way of doing this, you could use my open source GPUImage project (supporting back to iOS 4.x) and a couple of the image processing operations it provides. 为了快速执行此操作,您可以使用我的开源GPUImage项目(支持iOS 4.x)以及它提供的一些图像处理操作。

In particular, the GPUImageLuminanceThresholdFilter and GPUImageAdaptiveThresholdFilter might be what you're looking for here. 特别是,GPUImageLuminanceThresholdFilter和GPUImageAdaptiveThresholdFilter可能就是您在这里寻找的。 The former turns a pixel to black or white based on a luminance threshold you set (the default is 50%). 前者根据您设置的亮度阈值将像素变为黑色或白色(默认值为50%)。 The latter takes the local average luminance into account when applying this threshold, which can produce better results for text on pages of a book. 后者在应用该阈值时考虑局部平均亮度,这可以对书籍的页面上的文本产生更好的结果。

Usage of these filters on a UIImage is fairly simple: 在UIImage上使用这些过滤器非常简单:

UIImage *inputImage = [UIImage imageNamed:@"book.jpg"];
GPUImageLuminanceThresholdFilter *thresholdFilter = [[GPUImageLuminanceThresholdFilter alloc] init];
UIImage *quickFilteredImage = [thresholdFilter imageByFilteringImage:inputImage];

These can be applied to a live camera feed and photos taken by the camera, as well. 这些也可以应用于实时相机馈送和相机拍摄的照片。

You can use Core Image to process your image to black & white. 您可以使用Core Image将图像处理为黑白图像。

use CIEdgeWork , this will convert your image to black and whie 使用CIEdgeWork ,这会将您的图像转换为黑色和白色

for more information on Core Image Programming, visit:- https://developer.apple.com/library/ios/#documentation/GraphicsImaging/Conceptual/CoreImaging/ci_tasks/ci_tasks.html#//apple_ref/doc/uid/TP30001185-CH3-TPXREF101 有关核心图像编程的更多信息,请访问: - https://developer.apple.com/library/ios/#documentation/GraphicsImaging/Conceptual/CoreImaging/ci_tasks/ci_tasks.html#//apple_ref/doc/uid/TP30001185- CH3-TPXREF101

The code you are looking for is probably this: 您正在寻找的代码可能是这样的:

CIContext *context = [CIContext contextWithOptions:nil]; // 1
CIImage *image = [CIImage imageWithContentsOfURL:myURL]; // 2
CIFilter *filter = [CIFilter filterWithName:@"CIEdgeWork"]; // 3
[filter setValue:image forKey:kCIInputImgeKey];
[filter setValue:[NSNumber numberWithFloat:0.8f] forKey:@"InputIntensity"];
CIImage *result = [filter valueForKey:kCIOutputImageKey]; // 4
CGImageRef cgImage = [context createCGImage:result fromRect:[result extent];

here is some sample code,maybe helpful : 这里有一些示例代码,可能有帮助:

@implementation UIImage (GrayImage)

-(UIImage*)grayImage
{
    int width = self.size.width;
    int height = self.size.height;

    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceGray();
    CGContextRef context = CGBitmapContextCreate
(nil,width,height,8,0,colorSpace,kCGImageAlphaNone);
    CGColorSpaceRelease(colorSpace);

    if (context == NULL) {
        return nil;
    }

    CGContextDrawImage(context,CGRectMake(0, 0, width, height), self.CGImage);
    CGImageRef cgImage = CGBitmapContextCreateImage(context);
    UIImage *grayImage = [UIImage imageWithCGImage:cgImage];
    CGImageRelease(cgImage);
    CGContextRelease(context);

    return grayImage;
}

@end

I just write it as a Category of UIImage, but not support png image which has transparent pixel or it will be black. 我只是将其写为UIImage的类别,但不支持具有透明像素的png图像或者它将是黑色的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM