本文介绍了在OpenCV 2.1中:如何将一个矩阵分配给另一个矩阵的子矩阵?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
假设我有一个矩阵
A = cv::Mat(3,3,CV_32F)
和矩阵
B = cv::Mat(2,2,CV_32F).
比方说,A的全为零,B的全为零.我想将B的值分配给A的左上角.我该怎么做?
Let's say A has all zeros and B has all ones.I want to assign the values of B to the upper left corner of A. How can I do this?
我尝试了以下操作:
A(cv::Rect_<int>(0,0,2,2)) = B
但这似乎不起作用.但是,通过这种方式将标量值分配给A的subrect确实可行:
But this doesn't seem to work. However assigning a scalar value to the subrect of A this way does work:
A(cv::Rect_<int>(0,0,2,2)) = 1.0
第一种方法有什么问题?
What is wrong with the first approach?
推荐答案
我更喜欢单线,但这可以解决问题:
I'd prefer a one-liner, but this does the trick:
cv::Mat tmp = A(cv::Rect(0,0,2,2));
B.copyTo(tmp);
这篇关于在OpenCV 2.1中:如何将一个矩阵分配给另一个矩阵的子矩阵?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!