尝试在xcode中创建一个视差图,但在运行stereosgbm.compute时一直获得exc不良访问,无法找出原因。该函数的输出数组是一个mat,但我不知道如何初始化它以使其工作。
下面是代码:#import <Foundation/Foundation.h>
#import "DisparityGenerator.h"
#import "AppDelegate.h"
@implementation DisparityGenerator
AppDelegate *appDelDG;
cv::Mat matImageR;
cv::Mat matImageL;
+ (UIImage*) dispGen:(int)image{
appDelDG = (AppDelegate *)[[UIApplication sharedApplication] delegate];
UIImage* result = nil;
// convert the photo the camera took to MAT
if(image == 0){
UIImage *R = [UIImage imageNamed:@"im6"];
UIImageToMat(R, matImageR);
UIImage *L = [UIImage imageNamed:@"im2"];
UIImageToMat(L, matImageL);
}else{
UIImage *R = [UIImage imageNamed:@"im6a"];
UIImageToMat(R, matImageR);
UIImage *L = [UIImage imageNamed:@"im2a"];
UIImageToMat(L, matImageL);
}
cv::Mat dispArray;;
// dispArray.create(<#int _rows#>, <#int _cols#>, int _type)
cv::cvtColor(matImageL, matImageL, cv::COLOR_BGR2GRAY);
cv::cvtColor(matImageR, matImageR, cv::COLOR_BGR2GRAY);
cv::StereoSGBM *imbm = cv::StereoSGBM::create(6, 6, 6);
//cv::StereoBM::create(16, 2);
imbm->compute(matImageL,matImageR,dispArray);
result = MatToUIImage(dispArray);
return result;
}
@end
明白了!我初始化StereoBM错误。正确的方法是:
cv::Ptr <cv::StereoBM> imbm = cv::StereoBM::create(0);