iOS 12 + Mac OS X Mojave EXC_BAD_ACCESS on cvtColor

asked 2018-10-04 11:58:21 -0600

kingst gravatar image

updated 2018-10-04 12:13:47 -0600

I've been trying to integrate OpenCV into my iOS project, but have been having trouble with a null pointer exception. I installed version 3.4.2 using Cocoapods, and the following code snippet works fine:

#import <opencv2/opencv.hpp>
#import <opencv2/imgproc/imgproc.hpp>
#import <opencv2/imgcodecs/ios.h>

#import "OpenCVWrapper.h"

@implementation OpenCVWrapper

+ (UIImage *) process:(UIImage *) image {
    cv::Mat m;
    UIImageToMat(image, m);

    // this works fine

    return MatToUIImage(m);
}

But when I try to do a simple color conversion (or any type of manipulation for that matter) I get an EXC_BAD_ACCESS and it crashes my app

#import <opencv2/opencv.hpp>
#import <opencv2/imgproc/imgproc.hpp>
#import <opencv2/imgcodecs/ios.h>

#import "OpenCVWrapper.h"

@implementation OpenCVWrapper

+ (UIImage *) process:(UIImage *) image {
    cv::Mat m;
    UIImageToMat(image, m);

    cv::Mat gray;
    cv::cvtColor(m, gray, cv::COLOR_BGR2GRAY); // crashes here

    return MatToUIImage(gray);
}

I tried version 2 of opencv and tried stepping through the assembly, but wasn't able to figure out what was causing the problem.

edit retag flag offensive close merge delete

Comments

Have you maybe tried initializing gray? Try filling it first with zeros then do the color conversion. I had a weird issue like this as well awhile back with iOS. cv::Mat gray = cv::Mat::zeros(m.size(), CV_8UC1) And also be aware that UIImage is of RGBA whilst OpenCV operates on BGRA.

eshirima gravatar imageeshirima ( 2018-10-04 17:11:08 -0600 )edit

Thank you for the suggestion. I tried it out but still got the same error.

When you convert a UIImage to a cv::Mat will it be in the BGRA format? I had assumed that during the conversion that opencv would change the format to it's native form.

kingst gravatar imagekingst ( 2018-10-04 18:46:53 -0600 )edit

Can you please post the full error log?

eshirima gravatar imageeshirima ( 2018-10-05 11:20:16 -0600 )edit