Search code examples
iosswiftaccelerate-frameworkvdspcblas

Averaging the color of pixels with Accelerate


Yes, I know about using CIAreaAverate CIFilter to get the average color of pixels.

I am trying to create some alternative using Accelerate Framework to see if I can come with something faster.

I am rendering a CIImage to a context. For that purpose I have this CIImage extension...

let device: MTLDevice = MTLCreateSystemDefaultDevice()!
let context = CIContext.init(mtlDevice: device, options: [.workingColorSpace: kCFNull])

let w = self.extent.width
let h = self.extent.height
let size = w * h * 4

var bitmap = [UInt8](repeating: 0, count:Int(size))


context.render(self,
                      toBitmap: &bitmap,
                      rowBytes: 4 * Int(w),
                      bounds: self.extent,
                      format: .BGRA8,
                      colorSpace: nil)

At this point I have bitmap containing the BGRA bytes interleaved.

To get the average of R, G and B, all I have to do is something like this:

var averageBlue : Int = 0

for x in stride(from:0, through: bitmap.count-4, by: 4) {
  let value = bitmap[Int(x)]
  averageBlue += Int(value)
}

averageBlue /= numberOfPixels

but this for loop is slow as hell, as expected.

I was thinking about using some Accelerate function like

vDSP_meanvD(bitmap, 2, &r, vDSP_Length(numberOfPixels))

but this function requires bitmap to be an array of UnsafePointer<Double>...

I could convert bitmap to that, but that would require a for loop, that is slow...

Is there any way to extract those R, G and B pixels and have their individual averages using some accelerate stuff going on?


Solution

  • Like ielyamani’s said, you can use vDSP_vfltu8 to build that buffer of Float efficiently.

    But rather than striding through that array four times, you can also use cblas_sgemv (or cblas_sgemm) to calculate all four averages in a single call:

    let pixelCount: Int = width * height
    let channelsPerPixel: Int = 4
    
    let m: Int32 = Int32(channelsPerPixel)
    let n: Int32 = Int32(pixelCount)
    let lda = m
    
    var a = [Float](repeating: 0, count: pixelCount * channelsPerPixel)
    
    vDSP_vfltu8(pixelBuffer, vDSP_Stride(1), &a, vDSP_Stride(1), vDSP_Length(pixelCount * channelsPerPixel))
    
    var x = [Float](repeating: 1 / Float(pixelCount), count: pixelCount)
    
    var y = [Float](repeating: 0, count: channelsPerPixel)
    
    cblas_sgemv(CblasColMajor, CblasNoTrans, m, n, 1, &a, lda, &x, 1, 1, &y, 1)
    
    print(y)