将 opencv Mat 传递给函数时,对非常量的引用的初始值必须是左值
Initial value of reference to non-const must be an lvalue when passing opencv Mat to a function
我有垫子。我想根据矩形定义的某些区域更新垫子。当我像下面显示的代码一样传递它时,我得到了提到的错误。
void temp2(Mat &A){
//this function updates the value of A in the region defined by the rect.
for(i = 0 to A.rows){
Vec2f *p = reinterpret_cast<Vec2f * >(A.ptr(i));
for(j = 0 to A.cols){
// p[j] = Vec2f(5,5);
}
}
}
void temp1(Mat &A){
Point a(0,0), b(10,10);
Rect t(a,b);
temp2(A(t));
}
void temp(Mat &A){
A = Mat(500, 500, CV_32FC2, Scalar(-1.0, -1.0));
temp1(A);
}
int main(){
Mat A;
temp(A);
}
我查找了解决方案,它说要在 temp2 中使 mat A const function.I 不能在 temp2 函数中使 mat A const,因为我必须更新由定义的 mat 的特定区域temp2 函数中的 rect。如何以这种方式更新特定区域?
这不适合你吗?
/* Renamed arg to reflect what's happening.
Let it be A if you so wish but it's not
the original A from temp().
*/
void temp2(Mat &extractedFromA){
// Do stuff
}
void temp1(Mat &A){
Point a(0,0), b(10,10);
Rect t(a,b);
Mat extracted = A(t);
temp2(extracted);
}
您正在使用 this API
Mat cv::Mat::operator() (const Rect & roi)const
这意味着在调用 A(t)
时,A
未被修改(因为上面 API 是一个 const 方法)并产生一个 new Mat
object而这个是temp2()
里面需要操作的,不是原来的objectA
通过的进入 temp1()
。此外,对 extracted
Mat
所做的更改应反映回原始 Mat A
,因为您只在它们之间共享 headers。
此外,您可能遇到的错误是,由于 A(t)
正在生成一个临时 object,并且在将其传递给 temp2()
时,您试图绑定它到 non-const 左值引用。将其作为 const 左值引用可以修复它,但这显然对您没有帮助。
我有垫子。我想根据矩形定义的某些区域更新垫子。当我像下面显示的代码一样传递它时,我得到了提到的错误。
void temp2(Mat &A){
//this function updates the value of A in the region defined by the rect.
for(i = 0 to A.rows){
Vec2f *p = reinterpret_cast<Vec2f * >(A.ptr(i));
for(j = 0 to A.cols){
// p[j] = Vec2f(5,5);
}
}
}
void temp1(Mat &A){
Point a(0,0), b(10,10);
Rect t(a,b);
temp2(A(t));
}
void temp(Mat &A){
A = Mat(500, 500, CV_32FC2, Scalar(-1.0, -1.0));
temp1(A);
}
int main(){
Mat A;
temp(A);
}
我查找了解决方案,它说要在 temp2 中使 mat A const function.I 不能在 temp2 函数中使 mat A const,因为我必须更新由定义的 mat 的特定区域temp2 函数中的 rect。如何以这种方式更新特定区域?
这不适合你吗?
/* Renamed arg to reflect what's happening.
Let it be A if you so wish but it's not
the original A from temp().
*/
void temp2(Mat &extractedFromA){
// Do stuff
}
void temp1(Mat &A){
Point a(0,0), b(10,10);
Rect t(a,b);
Mat extracted = A(t);
temp2(extracted);
}
您正在使用 this API
Mat cv::Mat::operator() (const Rect & roi)const
这意味着在调用 A(t)
时,A
未被修改(因为上面 API 是一个 const 方法)并产生一个 new Mat
object而这个是temp2()
里面需要操作的,不是原来的objectA
通过的进入 temp1()
。此外,对 extracted
Mat
所做的更改应反映回原始 Mat A
,因为您只在它们之间共享 headers。
此外,您可能遇到的错误是,由于 A(t)
正在生成一个临时 object,并且在将其传递给 temp2()
时,您试图绑定它到 non-const 左值引用。将其作为 const 左值引用可以修复它,但这显然对您没有帮助。