Get 5 main colors from CVPixelBuffer MOST efficiently and quickly

139 Views Asked by At

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!

1

There are 1 best solutions below

1
Flex Monkey On

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
}