Averaging the color of pixels with Accelerate

2019-08-06 13:17发布

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?

2条回答
仙女界的扛把子
2楼-- · 2019-08-06 14:15

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)
查看更多
Explosion°爆炸
3楼-- · 2019-08-06 14:22

You can convert bitmap to single-precision floating-point values using vDSP_vfltu8(_:_:_:_:_:) :

let bitmap: [UInt8] = [1, 10,  50,  0,
                       2, 20, 150,  5,
                       3, 30, 250, 10]

//Blue
var blueFloats = [Float](repeating: 0, count: bitmap.count/4)

vDSP_vfltu8(bitmap,
            vDSP_Stride(4),
            &blueFloats,
            vDSP_Stride(1),
            vDSP_Length(blueFloats.count))

And then use vDSP_meanv(_:_:_:_:) :

var blue: Float = 0

vDSP_meanv(blueFloats,
           vDSP_Stride(1),
           &blue,
           vDSP_Length(blueFloats.count))

print("blue =", blue)     //2.0

As to the reds :

//Red
var redFloats = [Float](repeating: 0, count: bitmap.count/4)

vDSP_vfltu8(UnsafePointer.init(bitmap).advanced(by: 2),
            vDSP_Stride(4),
            &redFloats,
            vDSP_Stride(1),
            vDSP_Length(redFloats.count))

var red: Float = 0

vDSP_meanv(redFloats,
           vDSP_Stride(1),
           &red,
           vDSP_Length(redFloats.count))

print("red =", red) //150.0
查看更多
登录 后发表回答