试图找到贴图的幅值图像

Trying to find Magnitude of Mat image

本文关键字:图像      更新时间:2023-10-16

我正在尝试学习OpenCV(使用3.0.0版本)。现在我正在尝试观察点运算对各种图像的作用,一切都很好,直到我尝试进行幅度运算,这需要形式的输入

magnitude(InputArray x, InputArray y, OutputArray magnitude)

它还描述了x和y应该是向量的x/y坐标的浮点数组,并且大小相同。

我试着制作一个Mat的向量,将输入图像分割成这些向量,然后对它们进行幅度运算,但这没有奏效。所以我认为我需要将参数作为列和行传递,但现在我得到了错误

    OpenCV Error: Assertion failed (src1.size() == src2.size() && type == src2.type() && (depth == CV_32F || depth == CV_64F)) in magnitude, file /home/<user>/opencv-3.0.0-beta/modules/core/src/mathfuncs.cpp, line 521
    terminate called after throwing an instance of 'cv::Exception'
      what():  /home/<user>/opencv-3.0.0-beta/modules/core/src/mathfuncs.cpp:521: error: (-215) src1.size() == src2.size() && type == src2.type() && (depth == CV_32F || depth == CV_64F) in function magnitude
Aborted (core dumped)

我不知道为什么,因为我清楚地将输入Mats转换为CV_64F类型。我是不是用错了量值函数?或者只是传递了错误的数据?

void Magnitude(Mat img, Mat out)
{
    img.convertTo(img, CV_64F);
    out.convertTo(out, CV_64F); 
    for(int i = 0 ; i < img.rows ; i ++)
        for(int j = 0 ; j < img.cols ; j++)
            cv::magnitude(img.row(i), img.col(j), out.at<cv::Vec2f>(i,j));
    cv::normalize(out,out,0,255,cv::NORM_MINMAX);
    cv::convertScaleAbs(out,out);
    cv::imshow("Magnitude", out);
    waitKey();
}
void magnitude(InputArray x, InputArray y, OutputArray magnitude)

其中xymagnitude必须具有相同的大小。在你的情况下,这意味着你的形象必须是二次的。对吗?

示例用法:

cv::Sobel(img, gx, img.depth(), 1, 0, 3);     
cv::Sobel(img, gy, img.depth(), 0, 1, 3); 
cv::Mat mag(gx.size(), gx.type());
cv::magnitude(gx, gy, mag);