将 CIImage 坐标从 CIFaceFeature 转换为 CGRect



我正在尝试在用户的脸上放置一个方形矩形,我正在使用CIFaceFeature在全屏(self.view.frame(视频源上实时识别。但是,我从CIFaceFeature.bounds获得的坐标来自与视图使用的坐标系不同的坐标系。我尝试从这个和其他例子中转换这些坐标。但是由于我没有,所以我在视频源上运行它,我没有可以传递到CIImage的图像以简化坐标转换。下面是我的配置示例,知道如何转换为可用的CGRect吗?

func captureOutput(_ output: AVCaptureOutput, didOutput sampleBuffer: CMSampleBuffer, from connection: AVCaptureConnection) {
let imageBuffer = CMSampleBufferGetImageBuffer(sampleBuffer)
let opaqueBuffer = Unmanaged<CVImageBuffer>.passUnretained(imageBuffer!).toOpaque()
let pixelBuffer = Unmanaged<CVPixelBuffer>.fromOpaque(opaqueBuffer).takeUnretainedValue()
let sourceImage = CIImage(cvPixelBuffer: pixelBuffer)
let features = self.faceDetector!.features(in: sourceImage, options: options)
if (features.count != 0) {
let faceImage = sourceImage
let faceDetector = CIDetector(ofType: CIDetectorTypeFace, context: nil, options: [CIDetectorAccuracy: CIDetectorAccuracyHigh])
let faces = faceDetector?.features(in: faceImage) as! [CIFaceFeature]
let transformScale = CGAffineTransform(scaleX: 1, y: -1)
let transform = transformScale.translatedBy(x: 0, y: -faceImage.extent.height)
for feature in features as! [CIFaceFeature] {
faceBounds = feature.bounds
var fb = faceBounds?.applying(transform)
// imageViewSize is the screen frame
let scale = min(imageViewSize.width / fb!.width,
imageViewSize.height / fb!.height)
let dx = (imageViewSize.width - fb!.width * scale) / 2
let dy = (imageViewSize.height - fb!.height * scale) / 2
fb?.applying(CGAffineTransform(scaleX: scale, y: scale))
fb?.origin.x += dx
fb?.origin.y += dy
realFaceRect = fb // COMPLETELY WRONG :'(
}
}

如果有人遇到同样的问题。这是一个简单的解决方案

let imgHeight = CGFloat(CVPixelBufferGetHeight(pixelBuffer))
let ratio = self.view.frame.width / self.visage!.imgHeight
func convertFrame(frame: CGRect, ratio: CGFloat) -> CGRect {
let x = frame.origin.y * ratio
let y = frame.origin.x * ratio
let width = frame.height * ratio
let height = frame.width * ratio
return CGRect(x: x, y: y, width: width, height: height)
}
func convertPoint(point: CGPoint, ratio: CGFloat) -> CGPoint {
let x = point.y * ratio
let y = point.x * ratio
return CGPoint(x: x, y: y)
}

相关内容

  • 没有找到相关文章

最新更新