问题
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
-
回答1:
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.
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.
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.
回答2:
I know this is too late for answering but it might be useful for others who are looking for this code
UIImage *image = [UIImage imageNamed:@"Image.jpg"];
UIImageView *imageView = [[UIImageView alloc] init];
imageView.image = image;
UIGraphicsBeginImageContextWithOptions(imageView.size, YES, 1.0);
CGRect imageRect = CGRectMake(0, 0, imageView.size.width, imageView.size.height);
// Draw the image with the luminosity blend mode.
[image drawInRect:imageRect blendMode:kCGBlendModeLuminosity alpha:1.0];
// Get the resulting image.
UIImage *filteredImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
imageView.image = filteredImage;
Thank You
来源:https://stackoverflow.com/questions/22972441/convert-image-to-black-and-white-ios