色度键的平滑边缘-CoreImage

我使用下面的代码从图像中删除绿色背景。但是图像的边缘有绿色色调,一些像素被损坏。

我使用下面的代码从图像中删除绿色背景。但是图像的边缘有绿调,一些像素被损坏。

 func chromaKeyFilter(fromHue: CGFloat, toHue: CGFloat) -> CIFilter?
    {
        // 1
        let size = 64
        var cubeRGB = [Float]()
        // 2
        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)
                    // 3
                    let hue = getHue(red: red, green: green, blue: blue)
                    let alpha: CGFloat = (hue >= fromHue && hue <= toHue) ? 0: 1
                    // 4
                    cubeRGB.append(Float(red * alpha))
                    cubeRGB.append(Float(green * alpha))
                    cubeRGB.append(Float(blue * alpha))
                    cubeRGB.append(Float(alpha))
                }
            }
        }
  @IBAction func clicked(_ sender: Any) {
          let a = URL(fileURLWithPath:"green.png")
          let b = URL(fileURLWithPath:"back.jpg")
        let image1 = CIImage(contentsOf: a)
        let image2 = CIImage(contentsOf: b)
        let chromaCIFilter = self.chromaKeyFilter(fromHue: 0.3, toHue: 0.4)
        chromaCIFilter?.setValue(image1, forKey: kCIInputImageKey)
        let sourceCIImageWithoutBackground = chromaCIFilter?.outputImage
        /*let compositor = CIFilter(name:"CISourceOverCompositing")
        compositor?.setValue(sourceCIImageWithoutBackground, forKey: kCIInputImageKey)
        compositor?.setValue(image2, forKey: kCIInputBackgroundImageKey)
        let compositedCIImage = compositor?.outputImage*/
        var rep: NSCIImageRep = NSCIImageRep(ciImage: sourceCIImageWithoutBackground!)
        var nsImage: NSImage = NSImage(size: rep.size)
        nsImage.addRepresentation(rep)
        let url = URL(fileURLWithPath:"file.png")
        nsImage.pngWrite(to: url)
        super.viewDidLoad()
    }

输入:

enter image description here

Output: enter image description here

Update: enter image description here

Update 2: enter image description here

Update 3: enter image description here

7

用于色度键控的专业工具通常包括所谓的溢出抑制器。溢出抑制器查找包含少量色度键颜色的像素,并将颜色向相反方向移动。因此,绿色像素将向品红色移动。这减少了您经常在键控素材周围看到的绿色条纹。

例如,您可以找到当前像素的色相与fromHuetoHue之间的角距离,并执行以下操作:

// Get the distance from the edges of the range, and convert to be between 0 and 1
var distance: CGFloat
if (fromHue <= hue) && (hue <= toHue) {
    distance = min(abs(hue - fromHue), abs(hue - toHue)) / ((toHue - fromHue) / 2.0)
} else {
    distance = 0.0
}
distance = 1.0 - distance
let alpha = sin(.pi * distance - .pi / 2.0) * 0.5 + 0.5

这将为您提供从范围边缘到范围中心的平滑变化。(请注意,我已经停止处理色调在 360 ° 处环绕的事实。这是您必须处理的事情。)衰减的图形如下所示:

A sine function scaled and offset so that it smoothly goes from 0 to 1 over the range 0 to 1

您可以做的另一件事是将键控限制为仅影响饱和度高于某个阈值并且值高于某个阈值的像素。对于非常暗和 / 或不饱和的颜色,您可能不想将其淘汰。例如,我认为这将有助于解决您在模型夹克上看到的问题。

3

My (live) keyer works like this (with the enhancements user1118321 describes) and using its yser I quickly noticed this is most likely not a true green screen image. It's one of many fake ones where the green screen seems to have been replaced with a saturated monochrome green. Though this may look nice, it introduces artefacts where the keyed subject (with fringes of the originally used green) meets the monochrome green. You can see a single green was used by looking at the histogram. Real green screen always have (in)visible shades of green. I was able to get a decent key but had to manually tweak some settings. With a NLE you can probably get much better results, but they will also be a lot more complex. So to get back to your issue, your code probably works as it is now (update #3), you just have to use a proper real life green screen image. enter image description here

本站系公益性非盈利分享网址,本文来自用户投稿,不代表码文网立场,如若转载,请注明出处

(661)
如何检测反应并发模式 (react 18 concurrent mode)
上一篇
C++函数和MPI编程
下一篇

相关推荐

  • css 图片旋转动画:Unlock the Power of CSS Image Rotations!

    示例示例CSS 图片旋转动画可以使用 CSS3 的 `` 属性来实现,具体代码如下:上面的代码中,`.rotate` 是一个 CSS 类,可以应用到图片上,`` 属性指定了动画的名称、持续时间和动画曲线,`@` 定义了动画的关键帧,`` 属性指定了旋转的角度。…

    2023-05-26 00:21:24
    0 68 43
  • cvpr论文格式:A Study of Image Classification Using Deep Convolutiona

    CVPR论文格式是一种用于准备有关计算机视觉和图像处理的学术论文的格式。它是由IEEE Computer Society推出的,旨在使论文准备过程更加高效和简便。CVPR论文格式要求论文应该以双栏形式排版,字体为10磅Times New Roman或11磅Computer Modern,行距为单倍行距,页边距为1英寸,段落间距为半行。此外,论文中的图片、表格和公式也应遵循特定的格式。…

    2023-01-28 11:26:31
    0 55 96
  • css调整图片大小:How to Resize Images with CSS

    使用css调整图片大小,需要使用width和height属性,具体代码如下:上面的代码中,width属性指定图片的宽度为200px,height属性指定图片的高度为150px,以此来调整图片的大小。…

    2023-01-03 08:33:41
    0 58 24
  • 有没有自动打卡签到的小程序:有没有办法用魔杖使用ImageMagick的小插图功能

    关于有没有自动打卡签到的小程序的问题,在wand oops中经常遇到,我一直在寻找一种将 ImageMagick 的vignette functionality与Wand一起使用的方法,但是在 Wand 的文档中找不到该怎么做。…

    2022-12-19 09:16:59
    0 66 86
  • 怎么培养编程思维:在培养皿上计数菌落(imagej colony counter)

    关于怎么培养编程思维的问题,在imagej colony counter中经常遇到,我有一堆皮氏培养皿充满了点,我想在 Matlab 中计数。…

    2022-12-07 07:44:31
    0 23 36
  • C盘变红了如何清理win7:为什么整个画面都变红了 (images of turning red)

    关于C盘变红了如何清理win7的问题,在images of turning red中经常遇到,我们一直在尝试修复这个程序几个小时,但似乎没有任何工作,我们只是无法弄清楚问题是什么。除了红色像素之外,它应该使整个图片为黑白。(https://imgur.com/a/gxRm3N1之后应该是这样)…

    2022-12-26 07:03:27
    0 97 48
  • Cpu使用率高有什么影响:随机高 CPU使用率(windows image acquisition high cpu)

    关于Cpu使用率高有什么影响的问题,在windows image acquisition high cpu中经常遇到,我注意到 Visual Studio Code 随机地具有非常高的 CPU 使用率。通常,这似乎与我有一个 SQL 服务器项目的窗口有关。下面是code--status当这最近发生时的输出。对这里可能发生的事情有什么见解?…

    2022-12-14 04:08:37
    0 65 76
  • 买铅笔c语言:使用imagemagick将图像转换为铅笔画(不是铅笔素描 )

    关于买铅笔c语言的问题,在turn an image into a drawing中经常遇到,我需要一个应用程序,它将任何图像转换为铅笔画 (Not sketch) 像艺术家一样。完全像 step2 从步骤1in this question。我已经尝试了以下选项。…

    2022-12-17 10:26:29
    0 71 56

发表评论

登录 后才能评论

评论列表(81条)