How to not cause dark gray color to be transparent removing background from image

379 Views Asked by At

I'm having an issue where when I try to remove the green from an image (in this case the image background) but all the dark grays (within the part of the image I want to keep) become semi-transparent. I am unsure why, would like some advice on how to:

    func chromaKeyFilter(fromHue: CGFloat, toHue: CGFloat) -> CIFilter? {
        let size = 64
        var cubeRGB = [Float]()

        for z in 0 ..< size {
            let blue = CGFloat(z) / CGFloat(size-1)
            for y in 0 ..< size {
                let green = CGFloat(y) / CGFloat(size-1)
                for x in 0 ..< size {
                    let red = CGFloat(x) / CGFloat(size-1)

                    let color = UIColor(red: red, green: green, blue: blue, alpha: 1)
                    let hueColor = color.hsbColor
                    let alpha: CGFloat = (hueColor.hue >= fromHue && hueColor.hue <= toHue) ? 0 : 1
                    cubeRGB.append(Float(red * alpha))
                    cubeRGB.append(Float(green * alpha))
                    cubeRGB.append(Float(blue * alpha))
                    cubeRGB.append(Float(alpha))
                }
            }
        }

        let data = Data(bytes: cubeRGB, count: cubeRGB.count * MemoryLayout<Float>.size)
        let params: [String: Any] = ["inputCubeDimension": size, "inputCubeData": data]
        return CIFilter(name: "CIColorCube", parameters: params)
    }

    func filterPixels(foregroundCIImage: CIImage) -> CIImage {
        let chromaCIFilter = self.chromaKeyFilter(fromHue: 0.33, toHue: 0.34)
         chromaCIFilter?.setValue(foregroundCIImage, forKey: kCIInputImageKey)
         let sourceCIImageWithoutBackground = chromaCIFilter?.outputImage
         var image = CIImage()
         if let filteredImage = sourceCIImageWithoutBackground {
             image = filteredImage
         }
         return image
     }
}


extension UIColor {
    /// Decomposes UIColor to its HSBA components
    var hsbColor: HSBColor {
        var h: CGFloat = 0, s: CGFloat = 0, b: CGFloat = 0, a: CGFloat = 0
        self.getHue(&h, saturation: &s, brightness: &b, alpha: &a)
        return HSBColor(hue: h, saturation: s, brightness: b, alpha: a)
    }

    /// Holds the CGFloat values of HSBA components of a color
    public struct HSBColor {
        var hue: CGFloat
        var saturation: CGFloat
        var brightness: CGFloat
        var alpha: CGFloat
    }
}

Sample image:

enter image description here

1

There are 1 best solutions below

3
On BEST ANSWER

Your code is correct, but remember that a dark gray could really be a very dark green.

On this line:

let alpha: CGFloat = (hueColor.hue >= fromHue && hueColor.hue <= toHue) ? 0 : 1

I would take brightness/saturation into account. For example

let alpha: CGFloat = (hueColor.saturation > 0.1 && hueColor.hue >= fromHue && hueColor.hue <= toHue) ? 0 : 1