I’m trying to add some color correction to images in my Mac app using Core Image filters. First, I’m looking at allowing a custom white-balance, to remove a cast from an image. It looked like the CIWhitePointAdjust
was exactly what I’m looking for, but after trying it out, I’m not sure whether I’m using it wrong, or if it doesn’t do what I thought.
Starting with this image, yellowed with age, and with a thin strip of white at its right edge:
I apply the filter, like so:
NSColor *whiteSample = // Chosen from the speech bubble's background
CIColor *whiteInputColor = [[CIColor alloc] initWithColor:whiteSample];
layer.filters = @[
[CIFilter filterWithName:@"CIWhitePointAdjust"
withInputParameters:@{kCIInputColorKey: whiteInputColor}]
];
and get this image back:
Note that it looks darker and yellower than the original (the opposite of my desired effect). I had been expecting an effect more like doing an Auto Color in Photoshop, like so:
Am I using CIWhitePointAdjust
incorrectly, or is it the wrong tool for this job? If another filter or combination of filters would work better, I’d love to know.
Since I’m manipulating images that are already in CALayer
objects, Core Image filters definitely seem like the right choice, but if this can only feasibly be done through another means, I’m open to that.
Update
A helpful answer on the Signal Processing site give me the name of what I’m trying to implement. In broad terms, it’s called Histogram Equalization. I’m trying to figure out if there’s a way to perform that process using Core Image filter(s) and so far, it’s not looking too hopeful (without me writing my own).
3
Answers
Try getting a list of possible filters with autoAdjustmentFilters or autoAdjustmentFiltersWithOptions… probably the latter so that you can exclude the filters that require face detection, if you plan to autoadjust cartoon scans.
OK, so I think I’ve nailed it! Here’s what I’ve clobbered together from https://stackoverflow.com/a/30447041/734860 and a couple of glue bits:
The merit of the solution goes all to https://stackoverflow.com/users/4735340/james-bush, but I’ve looked so long without success for an image-oriented solution (as opposed to the video processing discussed in that question) that I think a ready-made reply such as this one is relevant. Looking for ‘AutoLevels’ on OS X or iOS got me nowhere, I hope this may be useful to someone else.
Swift 5