4

I'm trying to get 5 main/dominant colors from a CVPixelBuffer, and I need it to be as quick and efficient as possible.

I've tried Pixelating with CIFilter & Resize it, and only than go through the Pixels Data, but it's still pretty slow.

Also, how should I manage the tasks flow regarding to Threads Managing?

Any tips on working with CVPixelBuffer will be Great!

Alon Nasi
  • 41
  • 2

1 Answers1

3

This is a K-Means problem and Core Image provides just the filter to solve it! The following code creates a 5x1 CGImage of the five dominant colors in the CIImage:

import CoreImage
import CoreImage.CIFilterBuiltins

let ciImage = CIImage(cvImageBuffer: /* your image buffer */)

let filter = CIFilter.kMeans()
filter.count = 5

filter.inputImage = ciImage

let context = CIContext()

if let output = filter.outputImage,
   let palette = context.createCGImage(output, from: output.extent) {
    
    // palette is a 5 x 1 `CGImage` that contains the five dominant colors
}

Flex Monkey
  • 3,583
  • 17
  • 19