0

I'm using the filter CIAreaMinMax to get the brightest and darkest colour information from an image.

Normally the filter should output an image with two pixels (brightest and darkest). However, when I implement this filter to an image that contains two similar colours, then the result will be incorrect. The symptom of the incorrect result is, the two pixels' red channel has been switched, but G and B value has no problem.

The test image I am using is here, an png image only contains two colors:

RGB(37,62,88), GRB(10,132,255).

After processed by the code, it will output a png image that contains two pixels:

RGB(10,62,88), GRB(37,132,255).

In below is the test code for the swift playground:

import Cocoa
import CoreImage
import CoreGraphics

func saveImage(_ image: NSImage, atUrl url: URL) {
  let cgImage = image.cgImage(forProposedRect: nil, context: nil, hints: nil)!
  let newRep = NSBitmapImageRep(cgImage: cgImage)
  newRep.size = image.size
  let pngData = newRep.representation(using: .png, properties: [:])!
  try! pngData.write(to: url)
}

var sourceImage = CIImage.init(contentsOf: URL.init(fileURLWithPath: "/Users/ABC/Downloads/test.png"))!

let filter = CIFilter(name: "CIAreaMinMax")!
filter.setValue(sourceImage, forKey: kCIInputImageKey)
let civ = CIVector.init(x: sourceImage.extent.minX, y: sourceImage.extent.minY, z: sourceImage.extent.width, w: sourceImage.extent.height)
filter.setValue(civ, forKey: kCIInputExtentKey)

var filteredImage = filter.outputImage!
let context = CIContext(options: [.workingColorSpace: kCFNull!])

let filteredCGImageRef = context.createCGImage(
filteredImage,
from: filteredImage.extent)
let output = NSImage(cgImage: filteredCGImageRef!, size: NSSize.init(width: filteredImage.extent.width, height: filteredImage.extent.height))
saveImage(output, atUrl: URL.init(fileURLWithPath: "/Users/ABC/Downloads/output.png"))

2 Answers 2

1

From the documentation (emphasize mine):

Calculates the per-component minimum and maximum value for the specified area in an image.

That means the red value of the minimum pixel will contain the smallest red value among all pixels in the area. Same for the other channels. So the actual color of the pixel with the smallest red value will not be preserved.

2
  • Thanks for the answer Frank ! So do you know there is any existing filter or functions to achieve the result I want?
    – Yuqi Jin
    Apr 3, 2021 at 3:15
  • 1
    Well, "minimum and maximum color" are rather loose terms. When is a color "smaller" than another? You probably want some measure of luminance and find the min and max of it. But it really depends on your use case. Apr 3, 2021 at 15:29
1

This can be done with a bit of a trick.

  1. First, take your image and apply a custom CIColorKernel that puts luma in the alpha channel like this:
kernel vec4 prepImage (__sample s, vec3 lumaWeights)
{
    return vec4(s.rgb, dot(s.rgb, lumaWeights));
}
  1. Apply the CIAreaMinimumAlpha filter to the above image.
  2. Render the resulting 1x1 pixel image to an unpremultiplied CIRenderDestination.
2
  • Nice! Can we put lumaWeights generation inside the filter?
    – Yuqi Jin
    Apr 10, 2021 at 1:02
  • you can hard-code the weights in the kernel if you would prefer: Apr 13, 2021 at 1:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.