【问题标题】:iOS & OpenCV Error: Assertion Failed in PhaseCorrelateResiOS 和 OpenCV 错误:PhaseCorrelateRes 中的断言失败
【发布时间】:2014-01-04 17:27:57
【问题描述】:

我正在尝试在 iOS 中使用 OpenCV 检测 2 个图像之间的转换。我使用的函数是 phaseCorrelate,它应该返回给定 2 个cv::Mat 图像的 Point2d。我按照示例代码here 将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 中的应用程序会由于内存过大而立即关闭。

这是发生错误的代码的 sn-p(在 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

【问题讨论】:

    标签: c++ ios image opencv camera


    【解决方案1】:

    没关系,这实际上是由于 UIImage 首先有 3 个通道。当转换为 Mat 和 CV_32F 类型时,生成的 Mat 实际上是 CV_32FC3 类型(3 个通道);因此,由于参数类型不匹配而发生错误。

    我的解决方案是将原始 Mat 拆分为通道数组,然后仅将一个通道传递给 phaseCorrelate 函数:

    vector<Mat> refChannels;
    split(refMatImage, refChannels);
    phaseCorrelate(refChannels[0],...);
    

    【讨论】:

    • 太好了,我刚刚遇到了同样的问题并找到了您的答案。解决此问题的另一种方法是使用mat1.convertTo(mat1, CV_32FC1); 直接转换为 1 通道垫
    猜你喜欢
    • 2018-01-14
    • 1970-01-01
    • 2017-07-14
    • 1970-01-01
    • 1970-01-01
    • 2015-05-21
    • 1970-01-01
    • 2018-05-20
    • 1970-01-01
    相关资源
    最近更新 更多