【问题标题】:OpenCV calculating square of image Assertion failed errorOpenCV计算图像的平方断言失败错误
【发布时间】:2012-12-25 05:28:42
【问题描述】:

我正在尝试使用 Sum of squared Difference 作为错误度量 (Image registration) 将面部图像与参考面部图像对齐。基本上,尝试实现 cvMatchTemplate 函数的类似功能(但是我没有模板图像,而是常见的面部表情)。尝试对差异图像求平方时,我收到断言失败错误:-215。我的问题正是:我是否必须使用矩阵乘法运算符 A*A 或每元素乘法 A.mul(A) 来获得差异图像的平方? (目前我使用 A*A)

//Start search
Mat result;
for(int i= 0; i<15; i++){
    for(int j= 0; j<15; j++){
        xTrans = i; //Translation on x-Axis
        yTrans = j; //Translation on y-Axis

        //Initialize translation matrix
        double m[2][3] = {{1,0,xTrans}, {0,1,yTrans}};
        Mat map = Mat(2,3,CV_64F, m);

        //Get the transformed image
        warpAffine(displaced, aligned, map, aligned.size());
        //Calculate the sum of squared differences
        absdiff(reference,aligned,result);
        try{
            squared = result*result; //Error line
        } catch (Exception const & e){
            cerr<<"OpenCV exception: "<<e.what()<<std::endl;
        }
        SSD = sum(squared)[0]; //Sum of squared difference
        cout <<xTrans << "," << yTrans << ","<<SSD<<endl;
    }
}

这是错误:

OpenCV Error: Assertion failed (type == B.type() && (type == CV_32FC1 || type ==
CV_64FC1 || type == CV_32FC2 || type == CV_64FC2)) in unknown function, file ..
\..\..\src\opencv\modules\core\src\matmul.cpp, line 711
OpenCV exception: ..\..\..\src\opencv\modules\core\src\matmul.cpp:711: error: (-
215) type == B.type() && (type == CV_32FC1 || type == CV_64FC1 || type == CV_32F
C2 || type == CV_64FC2)

两个图像的大小和类型相同,因为 warpAffine 工作正常。任何关于为什么会发生此错误或我的实施正确性的建议将不胜感激!

【问题讨论】:

  • 如果它是方阵(行数 = 列数),您只能将矩阵与自身相乘。您的矩阵“结果”的大小(行数和列数)是多少?
  • result.cols=302 和 result.rows=302

标签: algorithm image-processing opencv face-recognition


【解决方案1】:

我猜这行的问题

Mat map = Mat(2,3,CV_64F, m);

试试这个链接 error on Gaussian blur with convolution method in openCV2.3

【讨论】:

    【解决方案2】:

    您想使用逐元素乘法。

    squared = result.mul(result);
    

    由于您使用的是浮点图像,因此无需担心饱和度。

    逐元素乘法将两个矩阵的单个元素相乘(因此得名),并且要求矩阵具有相同的大小。 result 矩阵中的每个元素都代表像素值的差异。每个像素值都独立于图像中的任何其他像素值,因此逐元素乘法将产生正确的结果。

    Matrix multiplication 与元素乘法有很大不同。一个常见的应用是在计算机图形和计算机视觉中应用变换。矩阵乘法在应用于数学构造时是有意义的,但在应用于图像时则不然。不要以为一定要用矩阵乘法,因为cv::Mat也是用来存储图像数据的。

    【讨论】:

    • 您能解释一下这两种操作的区别吗?好像没完全看懂。
    • 嗯,有道理,我对 OpenCV 很陌生,所以感谢您的解释!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-07-14
    • 1970-01-01
    • 1970-01-01
    • 2020-03-29
    • 2018-05-20
    • 1970-01-01
    • 2017-10-26
    相关资源
    最近更新 更多