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)
笑脸是遮盖在检测到的脸上的面具。但是在正确的方向上,转换总是错误的。