Hongli Zhang
Hongli Zhang

Reputation: 11

Has anyone tried how to use vision api(VNHomographicImageRegistrationRequest) in ios 11?

I am studying currency recognition problems which is related to the Vision SDK of iOS11. I'm having trouble handling VNHomographicImageRegistrationRequest, which determines the perspective warp matrix needed to align the content of two images. But I couldn't find how to send two images parameters into this API, can anyone help me?

Upvotes: 0

Views: 1288

Answers (2)

Hongli Zhang
Hongli Zhang

Reputation: 11

-(matrix_float3x3)predictWithVisionFromImage:(UIImage *)imageTarget toReferenceImage:(UIImage*)imageRefer{

    UIImage *scaledImageTarget = [imageTarget scaleToSize:CGSizeMake(224, 224)];
    CVPixelBufferRef bufferTarget = [imageTarget pixelBufferFromCGImage:scaledImageTarget];
    UIImage *scaledImageRefer = [imageRefer scaleToSize:CGSizeMake(224, 224)];
    CVPixelBufferRef bufferRefer = [imageRefer pixelBufferFromCGImage:scaledImageRefer];

    VNHomographicImageRegistrationRequest* request = [[VNHomographicImageRegistrationRequest alloc]initWithTargetedCVPixelBuffer:bufferTarget completionHandler:nil];

    VNHomographicImageRegistrationRequest* imageRequest = (VNHomographicImageRegistrationRequest*)request;
    VNImageRequestHandler* handler = [[VNImageRequestHandler alloc]initWithCVPixelBuffer:bufferRefer options:@{}];
    [handler performRequests:@[imageRequest] error:nil];
    NSArray* resultsArr = imageRequest.results;
    VNImageHomographicAlignmentObservation* firstObservation = [resultsArr firstObject];
    return firstObservation.warpTransform;
}

Upvotes: 0

nathan
nathan

Reputation: 9395

Apple's Vision framework flow is always the same: Request -> Handler -> Observation

Example:

// referenceAsset & asset2 can be:
//     CGImage - CIImage - URL - Data - CVPixelBuffer
// Check initializers for more info
let request = VNHomographicImageRegistrationRequest(targetedCGImage: asset2, options: [:])

let handler = VNSequenceRequestHandler()
try! handler.perform([request], on: referenceAsset)
if let results = request.results as? [VNImageHomographicAlignmentObservation] {
    print("Perspective warp found: \(results.count)")
    results.forEach { observation in
        // A matrix with 3 rows and 3 columns.
        print(observation.warpTransform)
    }
}

Upvotes: 1

Related Questions