Reputation: 231
I am trying to play with the Image Alignment Analysis part of the new Vision API but i am struggling with the initialisation of VNTranslationalImageRegistrationRequest. My code is as follows:
import UIKit
import Vision
class ImageTranslation {
var sourceImage: UIImage!
lazy var imageTranslationRequest: VNTranslationalImageRegistrationRequest = {
//This line fails let translationRequest = VNTranslationalImageRegistrationRequest(targetedCGImage: sourceImage.cgImage, completionHandler: self.handleImageTranslationRequest)
return translationRequest
}()
func handleImageTranslationRequest(request: VNRequest, error: Error?) {
guard let observations = request.results as? [VNImageTranslationAlignmentObservation]
else { print("unexpected result type from VNDetectRectanglesRequest")
return
}
guard observations.first != nil else {
return
}
DispatchQueue.main.async {
observations.forEach { observation in
let transform = observation.alignmentTransform
print(transform)
}
}
}
}
But on the marked line above i keep getting the following error and am unsure how to fix it. Instance member 'sourceImage' cannot be used on type 'ImageTranslation'
Can anyone point me in the right direction? Thanks
Upvotes: 3
Views: 1711
Reputation: 1885
An example how to use VNTranslationalImageRegistrationRequest on two images to compute the translation between the two images:
import UIKit
import Vision
class ImageTranslation {
func translateImages(for referenceImage: UIImage,
floatingImage: UIImage,
completion: @escaping (_ alignmentTransform : CGAffineTransform?) -> Void) {
let translationRequest = VNTranslationalImageRegistrationRequest(targetedCGImage: floatingImage.cgImage!) { (request: VNRequest, error: Error?) in
completion((request.results?.first as? VNImageTranslationAlignmentObservation)?.alignmentTransform)
}
let vnImage = VNSequenceRequestHandler()
try? vnImage.perform([translationRequest], on: referenceImage.cgImage!)
}
}
Upvotes: 6