是的,我知道使用heroku ps:scale web=1
CIAreaAverate
来获取像素的平均颜色。
我正在尝试使用CIFilter
创建一些替代方案,以查看是否可以更快地完成一些任务。
我正在向上下文渲染Accelerate Framework
。为此,我有这个CIImage
...
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)
包含交错的BGRA字节。
要获得R,G和B的平均值,我要做的就是这样:
bitmap
但是,这个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
循环像预期的那样缓慢。
我正在考虑使用类似for
的功能
Accelerate
但是此函数要求vDSP_meanvD(bitmap, 2, &r, vDSP_Length(numberOfPixels))
是bitmap
...的数组...
我可以将UnsafePointer<Double>
转换为该变量,但这需要一个bitmap
循环,这很慢...
有什么方法可以提取R,G和B像素,并使用一些加速的东西来获得它们的平均值?
答案 0 :(得分:1)
您可以使用vDSP_vfltu8(_:_:_:_:_:)
将bitmap
转换为单精度浮点值:
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))
然后使用vDSP_meanv(_:_:_:_:)
:
var blue: Float = 0
vDSP_meanv(blueFloats,
vDSP_Stride(1),
&blue,
vDSP_Length(blueFloats.count))
print("blue =", blue) //2.0
关于红军:
//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
答案 1 :(得分:1)
就像ielyamani所说的那样,您可以使用vDSP_vfltu8
有效地建立Float
的缓冲区。
但是,除了跨过该数组四次之外,还可以使用cblas_sgemv
(或cblas_sgemm
)在一次调用中计算所有四个平均值:
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)