0

I am trying to replace the black/dark gray with a pure black image in this image using GPUImageChromaKeyBlendFilter but it seems to be replacing the whites too:

enter image description here

enter image description here

Here's my code:

stillImageSource = [[GPUImagePicture alloc] initWithImage:[UIImage imageNamed:@"head.jpg"]];
blackImageSource = [[GPUImagePicture alloc] initWithImage:[UIImage imageNamed:@"black.jpg"]];

filter = [[GPUImageChromaKeyBlendFilter alloc] init];

 [(GPUImageChromaKeyBlendFilter *)filter setColorToReplaceRed:0.27 green:1.0 blue:1.0];
[(GPUImageChromaKeyBlendFilter *)filter setThresholdSensitivity:self.myslider.value];

[stillImageSource addTarget:filter];
[blackImageSource addTarget:filter];


[filter addTarget:self.mygpuimageview];
[stillImageSource processImage];
[blackImageSource processImage];

How can I make this work?

I notice in this post, Brad mentions the issue is due to removing luminance:

https://github.com/BradLarson/GPUImage/issues/2256

"This has to do with the color-matching calculation within the chroma keying filter. It attempts to remove the luminance component and only match on chrominance, so for white or black colors it has a hard time matching the target color due to the lack of chrominance. You could try digging into the chroma keying shader and seeing you you could improve the color matching algorithm in there."

Is there a way to keep luminance in account too when keying? I am very new to GPUImage and image manipulation in general so I have no idea how to even modify the code for luminance.

1 Answers1

0

Hey Pranoy C I had same problem with GPUImageChromaKeyBlendFilter, so instead I made custom function which removes special pixels from background, these special pixels can be defined by you on the basis of RGBA and you can play with them, I'm attaching my code below,

CODE -

#import "UIImage+BackgroundRemove.h"

#define Mask8(x) ( (x) & 0xFF )
#define R(x) ( Mask8(x) )
#define G(x) ( Mask8(x >> 8 ) )
#define B(x) ( Mask8(x >> 16) )
#define A(x) ( Mask8(x >> 24) )
#define RGBAMake(r, g, b, a) ( Mask8(r) | Mask8(g) << 8 | Mask8(b) << 16 | Mask8(a) << 24 )

@implementation UIImage (BackgroundRemoval)


- (UIImage *)removeBlackBackGroundFromImage {


    CGImageRef inputCGImage = self.CGImage;

    UInt32 * inputPixels;

    NSUInteger inputWidth = CGImageGetWidth(inputCGImage);
    NSUInteger inputHeight = CGImageGetHeight(inputCGImage);

    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();

    NSUInteger bytesPerPixel = 4;
    NSUInteger bitsPerComponent = 8;

    NSUInteger inputBytesPerRow = bytesPerPixel * inputWidth;

    inputPixels = (UInt32 *)calloc(inputHeight * inputWidth, sizeof(UInt32));

    CGContextRef context = CGBitmapContextCreate(inputPixels, inputWidth, inputHeight,
                                                 bitsPerComponent, inputBytesPerRow, colorSpace,
                                                 kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big);

    CGContextDrawImage(context, CGRectMake(0, 0, inputWidth, inputHeight), inputCGImage);

    for (NSUInteger j = 0; j < inputHeight; j++) {


        for (NSUInteger i = 0; i < inputWidth; i++) {
            UInt32 * currentPixel = inputPixels + (j * inputWidth) + i;
            UInt32 color = *currentPixel;

            if (R(color) == 0 &&
                G(color) == 0 &&
                B(color) == 0)  {
                *currentPixel = RGBAMake(0,0,0, A(0));
            }else{
                continue;
            }
        }
    }

    CGImageRef newCGImage = CGBitmapContextCreateImage(context);
    UIImage * imageToReturn = [UIImage imageWithCGImage:newCGImage];
    CGImageRelease(newCGImage);

    CGColorSpaceRelease(colorSpace);
    CGContextRelease(context);
    free(inputPixels);
    return imageToReturn;

}

Here I'm removing black color from background but in your case you can add black color to those pixels which are of certain colors, this is what I used :P

Hope It Helps

Shivam Gaur
  • 491
  • 5
  • 16