1

How can I convert RGB image to 1 channel image (black/white) using ios5?

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.

Le_Coeur
  • 1,521
  • 5
  • 28
  • 44
  • Possible duplicate: http://stackoverflow.com/questions/1298867/convert-image-to-grayscale – Attila H Mar 27 '13 at 13:37
  • Grayscale is not exactly black/white image. I need only two values per pixel in the output image matrix and not 256 – Le_Coeur Mar 27 '13 at 13:45
  • AFAIK, grayscale is a single-channel image (too). You should have defined the question more accurately. – Attila H Mar 27 '13 at 13:57
  • Yes you are right, therefore I mentioned black/white and not grayscale :) But maybe it is a bit ambiguous, thanks! – Le_Coeur Mar 27 '13 at 15:11

3 Answers3

3

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.

In particular, the GPUImageLuminanceThresholdFilter and GPUImageAdaptiveThresholdFilter might be what you're looking for here. The former turns a pixel to black or white based on a luminance threshold you set (the default is 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 *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.

Brad Larson
  • 170,088
  • 45
  • 397
  • 571
0

You can use Core Image to process your image to black & white.

use CIEdgeWork , this will convert your image to black and whie

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

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];
Burhanuddin Sunelwala
  • 5,318
  • 3
  • 25
  • 51
  • 1
    CIEdgeWork does edge detection, not black and white thresholding, and is unavailable in iOS 5.x: http://stackoverflow.com/a/11820675/19679 – Brad Larson Mar 27 '13 at 16:24
0

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.

junkor
  • 373
  • 1
  • 11