Sunday, 18 August 2019

Average color of a CIImage ... a faster method

I have a CIImage that is 258x258. I apply a filter CIAreaAverage to it, in order to get the green value of the average color of that image.

I am using this function to get that...

// channel 0,1,2 = red, green, blue
func averageColorOfImage (_ inputImage: CIImage, _ channel: Int) -> Double {

  let extentVector = CIVector(x: inputImage.extent.origin.x, y: inputImage.extent.origin.y, z: inputImage.extent.size.width, w: inputImage.extent.size.height)

  guard let filter = CIFilter(name: "CIAreaAverage", parameters: [kCIInputImageKey: inputImage, kCIInputExtentKey: extentVector]) else { return 0 }
  guard let outputImage = Static.filter!.outputImage else { return 0 }

  var bitmap = [UInt8](repeating: 0, count: 4)
  let context = CIContext(options: [.workingColorSpace: kCFNull])
  context.render(outputImage, toBitmap: &bitmap, rowBytes: 4, bounds: CGRect(x: 0, y: 0, width: 1, height: 1), format: .RGBA8, colorSpace: nil)

  return Double(bitmap[channel]) / Double(255)
}

This runs relatively fast but I am trying to see if there is a way to get that color fast.

I remember that Core Image has this custom "core filter" stuff.

Is there a way to do get the green value of the average color of an image using custom core filter?



from Average color of a CIImage ... a faster method

No comments:

Post a Comment