Convert image to black and white iOS?
I found a lot of code to convert an image to black and black. but nobody works.
I tried this code, but its grayscale converted image is 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
-
source to share
You will have to threshold the image after converting it to grayscale. Since your input image is dark text on a bright background, this should be straightforward. When you render a grayscale image, you are basically saying that "all pixels with intensity values above the threshold t should be white and all other pixels should be black." It is a standard image processing technology commonly used in image preprocessing.
If you intend to do image processing, I highly recommend Brad Larson GPUImage , which is a hardware-based Objective-C framework with a target. It comes with threshold filters ready to use.
There are various different thresholding algorithms, but if your input images always look like the above example, I see no reason to use a more complex approach. If, however, there is a risk of odd lighting, noise or other disturbances, use an adaptive threshold or other dynamic algorithm. As far as I remember, the GPUImage trsholding filter is responsive.
source to share
I know this is too late for an answer, but it might be helpful for others 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
source to share