Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Make all black pixels transparent using CIImage

I'm trying to get something akin to a color mask using exclusively CIImage filters.

Basically, I'm looking to make all black pixels transparent. I don't mind what color all the non-black pixels end up having, so long as they have maximum alpha.

The code I'm familiar with in Core Graphics to achieve this would look like:

// Convert black sections to transparent using a color mask
const CGFloat maskingColors[6] = { 0, 0, 0, 0, 0, 0 };
CGImageRef result = CGImageCreateWithMaskingColors(image, maskingColors);

I've given it a shot in Core Image using a color matrix filter but it just sets result to nil since the CIVector values are greater than 1.0f.

// Create color matrix filter
CIFilter *matrix = [CIFilter filterWithName:@"CIColorMatrix"];

// Set filter properties
[matrix setValue:image forKey:kCIInputImageKey];
[matrix setValue:[CIVector vectorWithX:1.0f Y:0.0f Z:0.0f W:0.0f] forKey:@"inputRVector"];
[matrix setValue:[CIVector vectorWithX:0.0f Y:1.0f Z:0.0f W:0.0f] forKey:@"inputGVector"];
[matrix setValue:[CIVector vectorWithX:0.0f Y:0.0f Z:1.0f W:0.0f] forKey:@"inputBVector"];
[matrix setValue:[CIVector vectorWithX:255.0f Y:255.0f Z:255.0f W:0.0f] forKey:@"inputAVector"];

// Get the mapped image
CIImage *result = [matrix valueForKey:kCIOutputImageKey];

Note: For what it's worth, this is an OSX app.

like image 250
Chris Nolet Avatar asked Jul 29 '13 09:07

Chris Nolet


1 Answers

EDIT

Solution using a (simple) Custom CIFilter. This Method only works on OSX, as iOS does not support Custom Filters:

The CIFilter subclass (more or less a copy of Apple's How-To):

#import <QuartzCore/QuartzCore.h>

@interface MaskFilter : CIFilter {
    CIImage *inputImage;
}

@end

@implementation MaskFilter

static CIKernel *maskFilterKernel = nil;

+ (void)initialize {
    [CIFilter registerFilterName: @"MaskFilter"
                     constructor: (id)self
                 classAttributes: [NSDictionary dictionaryWithObjectsAndKeys:
                                   @"Mask Filter", kCIAttributeFilterDisplayName,
                                   [NSArray arrayWithObjects:
                                    kCICategoryColorAdjustment,
                                    kCICategoryStillImage, kCICategoryInterlaced,
                                    kCICategoryNonSquarePixels,nil], kCIAttributeFilterCategories,
                                   nil]
     ];
}

+ (CIFilter *)filterWithName:(NSString *)name {
    CIFilter  *filter;
    filter = [[self alloc] init];
    return filter;
}

- (id)init {
    if(maskFilterKernel == nil) {
        NSBundle *bundle = [NSBundle bundleForClass:[self class]];
        NSString *code = [NSString stringWithContentsOfFile:[bundle pathForResource:@"MaskFilter" ofType: @"cikernel"] encoding:NSUTF8StringEncoding error:nil];
        NSArray *kernels = [CIKernel kernelsWithString:code];
        maskFilterKernel = [kernels objectAtIndex:0];
    }
    return [super init];
}

- (CIImage *)outputImage {
    CISampler *src = [CISampler samplerWithImage: inputImage];
    return [self apply:maskFilterKernel, src, kCIApplyOptionDefinition, [src definition], nil];
}

@end

The Custom Kernel which does all the work (Filename: MaskFilter.cikernel)

kernel vec4 maskFilterKernel(sampler src) 
{
    vec4 t = sample(src, destCoord());
    t.w = (t.x == 0.0 ? (t.y == 0.0 ? (t.z == 0.0 ? 0.0 : 1.0) : 1.0) : 1.0);
    return t;
}

using the Filter works just like a build-in Filter:

CIFilter *filter = [MaskFilter filterWithName:@"MaskFilter"];
[filter setValue:ciImage forKey:@"inputImage"];
CIImage *mask = [filter valueForKey: @"outputImage"];

Adding a parameter to choose the color to use as mask-color should be no problem.

OLD ANSWER

With built-in methods and classes, I can only come up with this rather unelegant solution (which uses only Core Image filters):

// convert all colors so they are brighter or equal to 0.5, except black
CIFilter *binary = [CIFilter filterWithName:@"CIColorMatrix"];
[binary setValue:inputImage forKey:@"inputImage"];
[binary setValue:[CIVector vectorWithX:1 Y:1 Z:1 W:0] forKey:@"inputRVector"];
[binary setValue:[CIVector vectorWithX:1 Y:1 Z:1 W:0] forKey:@"inputGVector"];
[binary setValue:[CIVector vectorWithX:1 Y:1 Z:1 W:0] forKey:@"inputBVector"];
[binary setValue:[CIVector vectorWithX:0.49999 Y:0.49999 Z:0.49999 W:0] forKey:@"inputBiasVector"];
inputImage = [binary valueForKey:@"outputImage"];

// convert to black/white only colors
binary = [CIFilter filterWithName:@"CIColorPosterize"];
[binary setDefaults];
[binary setValue:inputImage forKey:@"inputImage"];
[binary setValue:@2 forKey:@"inputLevels"];
inputImage = [binary valueForKey:@"outputImage"];

// get mask
CIFilter *mask = [CIFilter filterWithName:@"CIMaskToAlpha"];
[mask setDefaults];
[mask setValue: inputImage forKey:@"inputImage"];
like image 73
Jonathan Cichon Avatar answered Nov 15 '22 08:11

Jonathan Cichon