ChromaKey filter not filtering desired color CIImage
Asked Answered
T

1

1

I am trying to make some colors of the image transparent. Below are the images that I have.

Lets say that I want to remove the bold red color from the image and have it transparent. I am viewing my image as PDF, therefore the transparent color would be if the background would match with the pink on the side. I am using the code from Apple documentation which I slightly modified in the following way:

// inside 3rd loop 

let hue = getHue(red: red, green: green, blue: blue)
let wantedHue = getHue(red: myPixel.redComponent, green: myPixel.greenComponent, blue: myPixel.blueComponent)
let isHueInRange = hue >= wantedHue - 0.1 && hue <= wantedHue + 0.1
let alpha:CGFloat = isHueInRange  ? 0 : 1

Here is the result I get. As you can see, there is some color left and the background is not fully transparent. I made these modifications, because I need to be able to dynamically remove the background color of the image (my images won't have any humans or other complex objects in it. It will most likely be text and some rectangles. No color mixing. Just still colors.)

So what I do is finding the first pixel of the image and get its color. When I have the color I get its hue, but I manually set allowed range to be 0.2. I am assuming that the image won't contain any similar color to the one I have.

EDIT: The original color is: rgb(200, 39, 39) - hsv(200, 80.5, 78.4)

The residue color is: rgb(246, 215, 210) - hsv(352, 14.6, 96.5)

The image I have:

The image I have

The image I get after applying the filter:

The image I get after applying the filter

Toadfish answered 28/10, 2022 at 4:42 Comment(0)
C
1

To remove red colour, if the hue is between 0.9 and 0.1 (approximately) alpha should be zero.

Use the following and it will work.

let hue = getHue(red: red, green: green, blue: blue)
var alpha : CGFloat = 1.0
if (hue < 0.1 && hue >= 0.0) || (hue > 0.9 && hue <= 1.0){
    alpha = 0.0
}                      

I think the problem with your code is it never consider the range 0.9 to 1.0. It always consider some range from 0.0xxx to 0.1xxxx.

Constructionist answered 1/11, 2022 at 14:24 Comment(1)
Well now this is stupid and I feel ashamed that I did not see this :sweatsmile: Bounty is yours buddy thanks for the answer :thumbsup:Toadfish

© 2022 - 2025 — McMap. All rights reserved.