如何不使深灰色变成透明,从而从图像中删除背景 [英] How to not cause dark gray color to be transparent removing background from image

查看:124
本文介绍了如何不使深灰色变成透明,从而从图像中删除背景的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到一个问题,当我尝试从图像(在这种情况下为图像背景)中去除绿色,但所有深灰色(在我要保留的图像部分内)变成半透明的.我不确定为什么要提出以下建议:

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
    }
}

样本图片:

推荐答案

您的代码是正确的,但请记住,深灰色可能确实是非常深的绿色.

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

在这一行:

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

这篇关于如何不使深灰色变成透明,从而从图像中删除背景的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆