OpenCV加载cascadeClassifier EXC_BAD_ACCESS崩溃

时间:2016-03-16 20:14:22

标签: ios opencv

我正在使用“官方”opencv2.framework并且已经设置了包装器和桥接标头等。当我运行应用程序时,我收到一条EXC_BAD_ACCESS消息并崩溃。我在迅速工作。这是包装文件中的代码。

我做错了什么?

#include "OpenCVWrapper.h"
#import "UIImage+OpenCV.h"

#include <opencv2/opencv.hpp>

using namespace cv;
using namespace std;

@implementation OpenCVWrapper : NSObject



+ (UIImage *)processImageWithOpenCV:(UIImage*)inputImage{
Mat mat = [inputImage CVMat];
cv::CascadeClassifier* faceDetector;
// do your processing here
//...

// Load cascade classifier from the XML file
NSString* cascadePath = [[NSBundle mainBundle] pathForResource:@"haarcascade_frontalface_alt2" ofType:@"xml"];

faceDetector->load([cascadePath UTF8String]);

// Load image with face
//UIImage* image = [UIImage imageNamed:@"lena.png"];
Mat faceImage;
faceImage = mat;
Mat grayImage;
cvtColor(faceImage, grayImage, CV_BGR2GRAY);

//Detect faces
std::vector<cv::Rect> faces;
faceDetector->detectMultiScale(grayImage, faces, 1.1, 2,0|CV_HAAR_SCALE_IMAGE, cv::Size(30,30));
//Draw all detected faces
for(unsigned int i = 0; i < faces.size(); i++)
{
    const cv::Rect& face = faces[i];
    //Get top-left and bottom-right corner points
    cv::Point tl(face.x , face.y);
    cv::Point br = tl + cv::Point(face.width,face.height);

    //draw rectangle around the face
    cv::Scalar magenta = cv::Scalar(255,0,255);
    cv::rectangle(faceImage, tl, br, magenta, 4, 8, 0);


    }

    //show resulting image

    return [UIImage imageWithCVMat:faceImage];

}

@end

1 个答案:

答案 0 :(得分:2)

我相信你做得很好。试试我写的这个包装器。这与你创建一个指向CascadeClassifier的指针有关,我不是c ++ boffin但只是一个快速谷歌建议' - &gt;'从指针和'。'访问成员变量和方法从实例访问成员变量和方法。因此,我想认为这是您访问错误的原因。我的包装在我的项目中适合我。希望它适合你!

#import <Foundation/Foundation.h>
using namespace cv;
using namespace std;

@interface VJImageProcessingModule : NSObject

+ (void)startCascadeClassifier;
+ (vector<cv::Rect>)faceDetect:(Mat &)image;
+ (void)drawRectanglesOnFacesForImage:(Mat &)image andFacesRect:(vector<cv::Rect>)faces;

@end

#import "VJImageProcessingModule.h"
@implementation VJImageProcessingModule

static NSString *cascade_name = @"haarcascade_frontalface_default";
static NSString *cascade_name2 = @"haarcascade_frontalface_alt";
static NSString *cascade_name3 = @"haarcascade_frontalface_alt2";
static NSString *cascade_name4 = @"haarcascade_frontalface_alt_tree";

static CascadeClassifier cascade_model;

+ (void)startCascadeClassifier
{
NSString *cascade_fp = [[NSBundle mainBundle] pathForResource:cascade_name3 ofType:@"xml"];
cascade_model.load([cascade_fp UTF8String]);
}

+ (vector<cv::Rect>)faceDetect:(Mat &)image
{
if (cascade_model.empty()){[VJImageProcessingModule startCascadeClassifier];};

vector<cv::Rect> faces;
cascade_model.detectMultiScale(image, faces, 1.1, 2, 0|CV_HAAR_SCALE_IMAGE, cv::Size(30, 30));

return faces;
}

+ (void)drawRectanglesOnFacesForImage:(Mat &)image andFacesRect:(vector<cv::Rect>)faces
{
    for( int i = 0; i < faces.size(); i++ )
    {
     cv::Point pt1(faces[i].x, faces[i].y);
     cv::Point pt2((faces[i].x + faces[i].height), (faces[i].y + faces[i].width));
     cv::rectangle(image,pt1,pt2, Scalar(255,0,0),3,8,0);
    }
}

@end