当在正确方位的照片上进行人脸检测时,如何变换坐标系?

时间:2019-02-18 04:36:14

标签: ios swift core-graphics face-detection core-image

CoreImage的人脸检测可以用于向上定位的照片(将y转换为-y,并将y移至-image.height),但是我不知道该如何做正确定位时变换坐标系。

我尝试了很多变换,例如旋转等,但是都没有问题。

let ciImageSize = personciImage.extent.size
var transform: CGAffineTransform!
switch image.imageOrientation {
case .up:
  transform = CGAffineTransform(scaleX: 1, y: -1)
  transform = transform.translatedBy(x: 0, y: -ciImageSize.height)
case .right:
  transform = CGAffineTransform(a: 0, b: -1, c: -1, d: 0, tx: ciImageSize.width, ty: ciImageSize.height)
default:
  transform = CGAffineTransform(scaleX: 1, y: -1)
  transform = transform.translatedBy(x: 0, y: -ciImageSize.height)
}
// Apply the transform to convert the coordinates
var faceViewBounds = faceFeature.bounds.applying(transform)
// Calculate the actual position and size of the rectangle in the image view
let viewSize = imageView.bounds.size
let scale = min(viewSize.width / ciImageSize.width,
                  viewSize.height / ciImageSize.height)
let offsetX = (viewSize.width - ciImageSize.width * scale) / 2
let offsetY = (viewSize.height - ciImageSize.height * scale) / 2
faceViewBounds = faceViewBounds.applying(CGAffineTransform(scaleX: scale, y: scale))
faceViewBounds.origin.x += offsetX
faceViewBounds.origin.y += offsetY

let haha = UIImageView(frame: faceViewBounds)
haha.image = UIImage(named: "haha-2")
imageView.addSubview(haha)

笑脸是遮盖在检测到的脸上的面具。但是在正确的方向上,转换总是错误的。

good and bad

0 个答案:

没有答案