我正在尝试创建相机输出的直方图。我创建了一个AVCaptureVideoDataOutputSampleBufferDelegate类来处理来自摄像机的视频。下面是我处理视频样本的captureOutput()函数。
// AVCaptureVideoDataOutputSampleBufferDelegate
func captureOutput(_ captureOutput: AVCaptureOutput, didOutput sampleBuffer: CMSampleBuffer, from connection: AVCaptureConnection) {
if let buffer = CMSampleBufferGetImageBuffer(sampleBuffer) {
processUsingCIImage(pixelBuffer: buffer)
}
}
func processUsingCIImage(pixelBuffer: CVPixelBuffer) {
let image = CIImage(cvPixelBuffer: pixelBuffer)
let params : [String : Any] = [kCIInputImageKey: image,
kCIInputExtentKey: CIVector.init(cgRect: image.extent),
"inputCount":16,
"inputScale":200]
let filter = CIFilter(name:"CIAreaHistogram", withInputParameters:params)
if let outputImage = filter?.outputImage {
let width = Int(outputImage.extent.width)
let height = Int(outputImage.extent.height)
var data = Data(capacity:width*height*4)
let context = CIContext()
data.withUnsafeMutableBytes { (data: UnsafeMutablePointer<UInt8>) in
context.render(outputImage,
toBitmap: data,
rowBytes:width*4,
bounds:outputImage.extent,
format:kCIFormatRGBA8,
colorSpace:nil)
for i in 0..<width {
print("R:\(data[i*4]) G:\(data[i*4+1]) B:\(data[i*4+2])")
}
}
}
}
相机预览图像总是输出
并不重要R:0 G:0 B:0
对于直方图的所有值,除了最后一个看起来依赖于图像的值。我在这做错了什么?