iOS 和 OpenCV 错误:断言在 PhaseCorrelateRes 中失败
iOS & OpenCV Error: Assertion Failed in PhaseCorrelateRes
我正在尝试在iOS中使用OpenCV检测两个图像之间的偏移。我使用的函数是phaseCorrelate,它应该返回给定2个cv::Mat
图像的Point2d。我按照这里的示例代码将UIImage转换为Mat,然后将Mat
转换为CV_32F
类型。但我一直收到这个错误:
OpenCV Error: Assertion failed (src1.type() == CV_32FC1 || src1.type() == CV_64FC1) in phaseCorrelateRes, file /Users/alexandershishkov/dev/opencvIOS/opencv-2.4.7/modules/imgproc/src/phasecorr.cpp, line 498
libc++abi.dylib: terminating with uncaught exception of type cv::Exception: /Users/alexandershishkov/dev/opencvIOS/opencv-2.4.7/modules/imgproc/src/phasecorr.cpp:498: error: (-215) src1.type() == CV_32FC1 || src1.type() == CV_64FC1 in function phaseCorrelateRes
我不明白为什么会出现这个错误,因为我已经将Mat类型转换为CV_32F
。仅供参考:我之所以没有转换为CV_64F,是因为它占用了大量内存,而且iOS中的应用程序会因内存过大而立即关闭。
以下是发生错误的代码片段(在phaseCorrelate调用时):
#ifdef __cplusplus
-(void)alignImages:(NSMutableArray *)camImages
{
int i;
Mat matImages, refMatImage, hann;
Point2d pcPoint;
for (i = 0; i < [camImages count]; i++) {
if(i == 0){
UIImageToMat([camImages objectAtIndex:i], refMatImage);
refMatImage.convertTo(refMatImage, CV_32F);
createHanningWindow(hann, refMatImage.size(), CV_32F);
}
else{
UIImageToMat([camImages objectAtIndex:i], matImages);
matImages.convertTo(matImages, CV_32F);
pcPoint = phaseCorrelate(refMatImage, matImages, hann);
NSLog(@"phase correlation points: (%f,%f)",pcPoint.x, pcPoint.y);
}
}
NSLog(@"Done Converting!");
}
#endif
Nevermind,这实际上是由UIImage最初有3个通道这一事实引起的。当转换为Mat和CV_32F型时,得到的Mat实际上是CV_32FC3型(3个通道);因此,由于参数类型不匹配,出现错误。
我的解决方案是将原始Mat拆分为通道阵列,然后只将一个通道传递给phaseCorrelate
函数:
vector<Mat> refChannels;
split(refMatImage, refChannels);
phaseCorrelate(refChannels[0],...);
相关文章:
- 没有找到相关文章