【问题标题】:Substracting two matrices with different sizes, OpenCV error两个大小不同的矩阵相减,OpenCV报错
【发布时间】:2015-12-11 13:41:44
【问题描述】:

我正在尝试制作一个程序,该程序从两张相同的图片中找到齐次坐标,但是对象已被旋转或平移,然后拍摄第一张图片并将其覆盖在第二张图片上。

问题是,我得到一个矩阵错误,因为我有不同的矩阵大小。

我找到了解决方案,我需要将其应用于旋转和平移以在第二张图片上获取第一张图片(值,我必须在第二个矩阵上应用)

这是我的代码:

    Size size(image2.cols, image2.rows);

    Mat dest = cv::Mat::zeros(image2.rows, image2.cols, CV_32FC2);

    warpAffine(image2, dest, t, size);

    namedWindow("manipulated img", CV_WINDOW_NORMAL);
    imshow("manipulated img", dest);

    Mat output;

    cout << image1.size() << endl;
    cout << dest.size() << endl;

    bitwise_and(dest, image1, output);

    //subtract(dest, image1, output);
    namedWindow("output img", CV_WINDOW_NORMAL);
    imshow("output img", output);

我收到以下错误:

> [3722 x 2937] 
> [3722 x 2932] 
> OpenCV Error: Sizes of input arguments do
> not match (The operation is neither 'a rray op array' (where arrays
> have the same size and type), nor 'array op scalar' , nor 'scalar op
> array') in cv::binary_op, file C:\builds\master_PackSlave-win32
> -vc12-shared\opencv\modules\core\src\arithm.cpp, line 1573

我想我知道问题是什么,但我不知道解决方案是什么。

矩阵有不同的大小

image1.size() = [3722 x 2937] 
dest.size() = [3722 x 2932]

我能做些什么来解决这个问题?

【问题讨论】:

  • 你想如何在不同大小的矩阵之间进行按位计算?!理论上是不可能的
  • 哦,我忘了说我也尝试只减去每个图像。有没有办法“添加”像素?

标签: c++ opencv matrix homogenous-transformation


【解决方案1】:

我怀疑你只需要将dest 设为image1 的大小,而不是image2warpAffine 很有可能会处理得很好。

【讨论】:

  • 所以只要改变 Mat dest = cv::Mat::zeros(image1.rows, image1.cols, CV_32FC2); ?:-)
【解决方案2】:
  1. 创建一个与 image1 大小相同的新矩阵 (newDest)
  2. 将 newDest 设置为零
  3. 将 dest 矩阵复制到 newDest 到您想要的感兴趣区域(如果您希望新矩阵位于顶角,您可以将 left、top 设置为 0,0)

像这样:

src.copyTo(dst(Rect(left, top, src.cols, src.rows))); // left and top are the x,y position of where this Mat is going

类似链接: Copy an cv::Mat inside a ROI of another one

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-08-17
    • 1970-01-01
    • 2015-07-20
    • 2019-09-23
    • 1970-01-01
    • 1970-01-01
    • 2021-10-26
    相关资源
    最近更新 更多