CIAdditionCompositing产生不正确的效果 [英] CIAdditionCompositing giving incorrect effect

查看:135
本文介绍了CIAdditionCompositing产生不正确的效果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过平均其他几个图像来创建一个图像.为此,我首先使每个图像变暗,使其倍数等于我平均图像的数量:

  func darkenImage(乘数:CGFloat)->CIImage?{让divImage = CIImage(颜色:CIColor(红色:乘数,绿色:乘数,蓝色:乘数))let divImageResized = divImage.cropped(to:self.extent)//将乘数图像设置为与要变暗的图像相同的大小如果让divFilter = CIFilter(name:"CIMultiplyBlendMode",参数:["inputImage":self,"inputBackgroundImage":divImageResized]){返回divFilter.outputImage}打印(无法使图像变暗")返回零} 

此后,我将每个变暗的图像都添加在一起(将图像1和2一起添加,然后将结果与图像3一起添加,等等):

  func blend(图像:CIImage,blendMode:BlendMode)->CIImage?{如果让filter = CIFilter(name:blendMode.format){//blendMode.format是CIAdditionCompositingfilter.setDefaults()filter.setValue(self,forKey:"inputImage")filter.setValue(image,forKey:"inputBackgroundImage")让resultImage = filter.outputImage返回resultImage}返回零} 

该代码执行并生成新图像,但是我平均在一起的图像越多,阴影越暗.高光与每个单独的图像保持大约相同的亮度,但是较暗的部分只会变得越来越暗.有人知道什么地方可能出问题吗?

原始图片:

2张图像的平均值:

8张图像的平均值:

20张图像的平均值:

为了减少潜在问题的数量,我还尝试过在Lightroom中处理图像之前先使其变暗,然后仅应用 CIAdditionCompositing 过滤器.这给出了相同的结果,这使我认为 CIAdditionCompositing 可能不仅是在增加像素,而且使用了一些稍有不同的算法,但是我还没有找到任何文档.我还尝试过更改变暗系数,以查看是否发生了计算错误,但是如果我将图像变暗的程度较小,则再次将图像再次加在一起时,高光会曝光过度.

解决方案

这可能会有点迟,但是有些.

第一次尝试

我怀疑问题在于色域不是线性的,就像Ken Thomases提到的那样.不幸的是,使用"CISRGBToneCurveToLinear" 过滤器将所有图像转换为线性图像,并且在将所有图像堆叠在一起之后,使用"CILinearToSRGBToneCurve" 将其转换回无法解决该问题./p>

解决方案

使用曝光每次添加两张图像后都可以进行调整以将曝光减半,从而解决了该问题.要使曝光减半,您需要将f-stop降低1步,因此曝光值(EV)必须为-1.

此外,我添加中间图像只是因为有时 CIImage 中的过滤器堆栈太大时,我的旧手机有时会遇到麻烦.

if let evFilter = CIFilter(name: "CIExposureAdjust", parameters: ["inputImage":self, "inputEV":NSNumber(-1)]){返回evFilter.outputImage?.insertingIntermediate()} 

P.S .:请注意,要创建正确的结果,必须将图像彼此相加并减少一半的曝光,以使每个图像与所得图像具有相同的权重.只需在行中添加下一张图像并减少之后的曝光量,始终会使最新添加的图像占整个结果的权重为50%.

I am trying to create an image by averaging several other images. To achieve this, I first darken each image by a factor equivalent to the number of images I am averaging:

func darkenImage(by multiplier: CGFloat) -> CIImage? {
    let divImage = CIImage(color: CIColor(red: multiplier, green: multiplier, blue: multiplier))

    let divImageResized = divImage.cropped(to: self.extent) //Set multiplier image to same size as image to be darkened

    if let divFilter = CIFilter(name: "CIMultiplyBlendMode", parameters: ["inputImage":self, "inputBackgroundImage":divImageResized]) {

        return divFilter.outputImage
    }

    print("Failed to darken image")
    return nil
}

After this I take each darkened image and add them together (add image 1 and 2 together, then add the result together with image 3 etc):

func blend(with image: CIImage, blendMode: BlendMode) -> CIImage? { 
    if let filter = CIFilter(name: blendMode.format) { //blendMode.format is CIAdditionCompositing
        filter.setDefaults()

        filter.setValue(self, forKey: "inputImage")
        filter.setValue(image, forKey: "inputBackgroundImage")

        let resultImage = filter.outputImage

        return resultImage
    }

    return nil
}

This code executes and produces a new image, but the more images I average together, the darker the shadows gets. The highlights stay about the same brightness as each of the individual images, but the darker parts just gets darker and darker. Does anyone know what could be wrong?

Original image:

Average of 2 images:

Average of 8 images:

Average of 20 images:

To reduce the number of potential issues I have also tried to darken the images before hand in Lightroom and just apply the CIAdditionCompositing filter. This gives the same result, which makes me think that CIAdditionCompositing may not just be adding up pixels, but use some slightly different algorithm, but I haven't found any documentation on this. I have also tried changing the darkening multiplier to see if I did a calculation error, but if I darken the images less, the highlights becomes overexposed when adding the images together again.

解决方案

This may come a little late but some.

First try

I suspect the problem is that the color gamut is not linear, just like Ken Thomases mentioned. Unfortunately converting all images to be linear with the "CISRGBToneCurveToLinear" filter and after all images had been stacked converting them back with "CILinearToSRGBToneCurve" does not solve the issue.

Solution

using exposureAdjust to halve the exposure each time after adding two images did solve the issue. To halve the exposure you would need to decrease the f-stop by 1 step so the exposure value (EV) needs to be -1.

Additionally I added intermediate images just because I run into trouble sometimes on my old phone when the filter stack in an CIImage is too big.

if let evFilter = CIFilter(name: "CIExposureAdjust", parameters: ["inputImage":self, "inputEV":NSNumber(-1)]) {
    return evFilter.outputImage?.insertingIntermediate()
}

P.S.: Please note that to create a correct result the images need to be added to each other and halved in exposure so that each image has the same weight to the resulting image. simply adding the next image in line and reducing the exposure afterwards will always give the latest added image 50% weight to the overall result.

这篇关于CIAdditionCompositing产生不正确的效果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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