0

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 {}
Cœur
  • 37,241
  • 25
  • 195
  • 267
chris.bennett
  • 69
  • 1
  • 6
  • 1
    Arbitrary images for those investigating: [15 MB](https://commons.wikimedia.org/wiki/File:FullMoon2020.tif) and [160 MB](https://commons.wikimedia.org/wiki/File:The_Carson_Mansion_is_a_large_Victorian_house_located_in_Old_Town,_Eureka,_California_LCCN2013634803.tif) – Cœur Jun 01 '23 at 14:54
  • @Cœur Thanks for your comments! The plot thickens: it works fine with both of the arbitrary images you've provided above, however if I take the 160 MB file and crop it in Photoshop to the following dimensions: 4000x5200 and use it for either of the CIImages, then it gives me the "abort trap (6)" message again. – chris.bennett Jun 01 '23 at 15:21
  • @Cœur Absolutely. I'm not sure what the preferred way is to reference external images, but hopefully [this link on Dropbox](https://www.dropbox.com/sh/u0bnjbqs1e3dno7/AAB2s9AVaUtVaA_jwufuTw-Qa?dl=0) is ok? Any of the three images I posted there cause the error message, which I'm more and more convinced is a bug in Apple's framework, since it's so arbitrary in terms of which images don't work. For example, if I rotate these images 90 degrees and re-run the script, it's fine. When I rotate them back, it has the error message again, so it's not anything to do with the format. – chris.bennett Jun 03 '23 at 01:56
  • 1
    Confirm it's likely an Apple bug in https://developer.apple.com/documentation/vision/vnimagerequesthandler/2880297-perform in Xcode 14E222b: `/Users/User/Developer/MyPlayground.playground error: Execution was interrupted, reason: signal SIGABRT. The process has been left at the point where it was interrupted, use "thread return -x" to return to the state before expression evaluation.` – Cœur Jun 04 '23 at 19:24
  • @Cœur Glad to know it wasn't user-error, and hopefully they fix it in an upcoming release. Thanks very much for your time! – chris.bennett Jun 05 '23 at 01:43
  • Not reproducing the crash anymore on macOS 14 Sonoma with Xcode 15 beta. I do not know if an app built with Xcode 15 would crash or not on this code when running on macOS 13 Ventura: up to you to try it maybe. – Cœur Jun 06 '23 at 14:32

0 Answers0