gpt4 book ai didi

ios - OpenCV 加载级联分类器 EXC_BAD_ACCESS 崩溃

转载 作者:太空宇宙 更新时间:2023-11-03 22:26:57 25 4
gpt4 key购买 nike

我正在使用“官方”opencv2.framework 并且已经设置了包装器和桥接 header 等。我收到 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

最佳答案

我相信你做得很好。试试我写的这个包装器。这与你创建一个指向 CascadeClassifier 的指针有关,我不是 C++ 专家,但只是一个快速的谷歌建议 '->' 从指针和 '.' 访问成员变量和方法。从实例访问成员变量和方法。因此,我想这就是您访问错误的原因。我的包装器在我的项目中为我工作。希望它对你有用!

#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

关于ios - OpenCV 加载级联分类器 EXC_BAD_ACCESS 崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36046031/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com