I am trying to create a Histogram using Core Image. I successfully got it to display (code below). However, the background of the histrogram is gray. How can I get it to be clear?
//Show Histogram
CVPixelBufferRef pixelBuffer = CMSampleBufferGetImageBuffer(sampleBuffer);
CFDictionaryRef attachments = CMCopyDictionaryOfAttachments(kCFAllocatorDefault, sampleBuffer, kCMAttachmentMode_ShouldPropagate);
CIImage *ciImage = [[CIImage alloc] initWithCVPixelBuffer:pixelBuffer options:(__bridge NSDictionary *)attachments];
NSUInteger count = 256;
count = count <= 256 ? count : 256;
count = count >= 1 ? count : 1;
NSDictionary *params = @{kCIInputImageKey: ciImage,
kCIInputExtentKey: [CIVector vectorWithCGRect:[ciImage extent]],
@"inputCount": @(count), @"inputScale": @(100),
};
CIFilter *filter = [CIFilter filterWithName:@"CIAreaHistogram"
withInputParameters:params];
CIImage *outImage = [filter outputImage];
//---------------------------------------------
CIContext *context = [CIContext contextWithOptions:nil];
NSDictionary *params2 = @{
kCIInputImageKey: outImage,
};
CIFilter *filter2 = [CIFilter filterWithName:@"CIHistogramDisplayFilter"
withInputParameters:params2];
CIImage *outputImage = [filter2 outputImage];
CGRect outExtent = [outputImage extent];
CGImageRef cgImage = [context createCGImage:outputImage
fromRect:outExtent];
UIImage *outImage2 = [UIImage imageWithCGImage:cgImage];
CGImageRelease(cgImage);
// resize
UIImage *resized = [self resizeImage:outImage2
withQuality:kCGInterpolationNone
rate:2.5];
As far as I know there is no parameter that can customize the background color of the histogram produced by this filter. It was meant to be this way. The same color even appears on Apple's documentation of the filter (look at the sample output).
I can think of two possible solutions:
CoreImage
code, you'll need to perform additional processing on the histogram image. A possible solution would be to go over its pixels and check for the specific gray color of the histogram background (137, 137, 137), then set them to be transparent. I don't know if that's the best solution, but it works.Going with the second solution, here's a method that will turn the gray color to transparent:
Now call this method on your final image: