I've written a script that uses Apple's Vision framework to align two images and save the composite as a new image. Oddly, it works perfectly when the images are small, but when they are larger, it crashes with "abort trap (6)", which isn't an error message I'd expect to see, even if there was a built-in limit to the image size. The images aren't even particularly large, only a couple of MB each. Sample images reproducing the crash.
The following is just the relevant code to recreate the crash on macOS 13 Ventura with Xcode 14, which is being caused by the last line: [handler performRequests:@[request] error:NULL];
Why would this be happening?
#import <CoreImage/CoreImage.h>
#import <Vision/Vision.h>
int main(int argc, char *argv[])
{
@autoreleasepool
{
NSString *imagePath1 = @"/path/to/some_image.tif";
CIImage *image1 = [CIImage imageWithContentsOfURL:[NSURL fileURLWithPath:imagePath1]];
NSString *imagePath2 = @"/path/to/other_image.tif";
CIImage *image2 = [CIImage imageWithContentsOfURL:[NSURL fileURLWithPath:imagePath2]];
VNImageRequestHandler *handler = [[VNImageRequestHandler alloc] initWithCIImage:image1 options:@{}];
VNHomographicImageRegistrationRequest *request = [[VNHomographicImageRegistrationRequest alloc] initWithTargetedCIImage:image2 options:@{}];
[handler performRequests:@[request] error:NULL];
}
}
Same crash in Swift:
import CoreImage
import Vision
let imagePath1 = "/path/to/some_image.tif"
let image1 = CIImage(contentsOf: URL(fileURLWithPath: imagePath1))!
let imagePath2 = "/path/to/other_image.tif"
let image2 = CIImage(contentsOf: URL(fileURLWithPath: imagePath2))!
let handler = VNImageRequestHandler(ciImage: image1)
let request = VNHomographicImageRegistrationRequest(targetedCIImage: image2)
do { try handler.perform([request]) }
catch {}