CIAdditionCompositing giving incorrect effect
Asked Answered
B

1

6

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: Original image

Average of 2 images: 2x average

Average of 8 images: 8x average

Average of 20 images: 20x average

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.

Baecher answered 18/12, 2019 at 8:18 Comment(1)
I suspect that your darkening technique (whether CI or Lightroom) is not operating in linear color space. If there's a non-linear gamma curve, then multiplying component values by a fraction does not yield an image that is that fraction as light. So, adding them together wouldn't do what you expect. I would recommend doing some tests with images of known color representation values (i.e. looking at actual raster bitmap values) with various color spaces to interpret those values. See if the multiply gives a result as you expect. Similarly, see if addition does.Hendershot
F
0

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.

Fellows answered 23/1, 2021 at 13:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.