1 Reply
      Latest reply on Feb 6, 2020 9:35 AM by finngaida
      kart Level 1 Level 1 (0 points)

        I'm trying to align two images using Vision's VNImageHomographicAlignmentObservation, as below:

        let request = VNHomographicImageRegistrationRequest(targetedCIImage: targetImage!) { request, error in
        let observation = request.results!.first as! VNImageHomographicAlignmentObservation
        let (col0, col1, col2) = observation.warpTransform.columns
        let values: [CGFloat] = [CGFloat(col0.x), CGFloat(col1.x), CGFloat(col2.x), CGFloat(col0.y), CGFloat(col1.y), CGFloat(col2.y), CGFloat(col0.z), CGFloat(col1.z), CGFloat(col2.z)]
        let vector = CIVector(values: values, count: values.count)

        currentImage = warpKernel.apply( extent: targetImage!.extent,
                                         roiCallback: { _, rect in
                                            return rect.insetBy(dx: -100, dy: -100) },
                                         image: currentImage,
                                         arguments: [vector])!
        targetImage = targetImage!.applyingFilter("CIAdditionCompositing", parameters: ["inputBackgroundImage": currentImage, "homographyMatrix": vector]) }
        do { try visionHandler.perform([request], on: currentImage) }
        catch { verifyNoError(error) }

        fileprivate let warpKernel = CIWarpKernel(source: """
          kernel vec2 warp(mat3 homographyMatrix) {
            vec3 homogeneousDestCoord = vec3(destCoord(), 1.0);
            vec3 homogeneousSrcCoord = homographyMatrix * homogeneousDestCoord;
            return homogeneousSrcCoord.xy / max(homogeneousSrcCoord.z, 0.000001);
           } """)!

        verifyNoError() is a simple helper function that stops execution if an error occurs.

        But this gives me:

        Error Domain=com.apple.vis Code=11 "encountered unknown exception" 

        Does anyone know why this is happening, and what I can do to fix it?

        I tried again, and that didn't work either. I modified the ROI callback to return the entire source rectangle by insetting by -4000, but that didn't work.

        This is on iPhone X running iOS 11.3.

        PS: There's also VNTranslationalImageRegistrationRequest, but that didn't work, either.