iOS 12 + Mac OS X Mojave EXC_BAD_ACCESS on cvtColor
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.
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 thatUIImageis of RGBA whilst OpenCV operates on BGRA.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.
Can you please post the full error log?