我正在使用新的Apple Vision API的VNImageTranslationAlignmentObservation来获取返回的CGAffineTransform。这个想法是,你向它传递两个可以合并在一起的图像,它返回CGAffineTransform,以便你可以这样做。我已经设法使代码正常工作,以便返回CGAffineTransform,但是经过大量阅读后,我不知所措,因为我如何将两个图像与信息合并。
我的代码在这里:
import UIKit
import Vision
class ImageTranslation {
let referenceImage: CGImage!
let floatingImage: CGImage!
let imageTranslationRequest: VNTranslationalImageRegistrationRequest!
init(referenceImage: CGImage, floatingImage: CGImage) {
self.referenceImage = referenceImage
self.floatingImage = floatingImage
self.imageTranslationRequest = VNTranslationalImageRegistrationRequest(targetedCGImage: floatingImage, completionHandler: nil)
}
func handleImageTranslationRequest() -> UIImage {
var alignmentTransform: CGAffineTransform!
let vnImage = VNSequenceRequestHandler()
try? vnImage.perform([imageTranslationRequest], on: referenceImage)
if let results = imageTranslationRequest.results as? [VNImageTranslationAlignmentObservation] {
print("Image Transformations found (results.count)")
results.forEach { result in
alignmentTransform = result.alignmentTransform
print(alignmentTransform)
}
}
return applyTransformation(alignmentTransform)
}
private func applyTransformation(_ transform: CGAffineTransform) -> UIImage {
let image = UIImage(cgImage: referenceImage)
return image
}
}
我得到的打印转换就像这样CGAffineTransform(a: 1.0, b: 0.0, c: 0.0, d: 1.0, tx: 672.0, ty: 894.0)
我如何应用这两个传入的两个图像?
我一直在玩一些例子(仅限纵向图像,但也应该适用于横向(,并且已经奏效了:
func mergeImages(first image1:UIImage, second image2:UIImage, transformation: CGAffineTransform) -> UIImage {
let size = CGSize(width: image1.size.width + image2.size.width - (image2.size.width - transformation.tx), height: image1.size.height + image2.size.height - (image2.size.height - transformation.ty))
let renderer = UIGraphicsImageRenderer(size: size)
return renderer.image { context in
let pointImg2 = CGPoint.zero.applying(transformation)
image2.draw(at: pointImg2)
let pointImg1 = CGPoint.zero
image1.draw(at: pointImg1)
}
}
如果它不起作用,请告诉我(如果可以,请上传您的示例图像(,我会修复它。