简体   繁体   English

在OpenCV中访问数组的元素

[英]Accessing an element of an Array in OpenCV

I am trying to get the value of an element in an array in order to use it in an if statement but unfortunately the following code is not working for me. 我正在尝试获取数组中元素的值,以便在if语句中使用它,但不幸的是,以下代码对我不起作用。 The cout of comp is not matching the first element of the array C. I'm new to OpenCV so any help is appreciated. compcout与数组C的第一个元素不匹配。我是OpenCV的新手,因此感谢您的帮助。

Mat A = (Mat_<double>(2,1) << u, v); 
Mat B = (Mat_<double>(2,6) << -1/Z,  0 ,  x/Z , x*y , -(x*x+1),y,                                                       
                               0 ,-1/Z,  y/Z ,y*y+1,   -x*y  ,-x);
Mat pinvB = B.inv(DECOMP_SVD);
Mat C=pinvB*A; // 6x1 Array

float comp = C.at<float>(0,0);
cout << "comp " << comp << endl; //This value does not match C[0,0]
cout << "C " << C << endl;

if (comp < 0.0001){
   //process
}

Your Mat_<double> instances internally store double s. Mat_<double>实例在内部存储double When you do this: 执行此操作时:

float comp = C.at<float>(0,0);

you are trying to use some of the bits that form a double, and interpret them as a float . 您正在尝试使用一些构成double的位,并将它们解释为float Floating point representation means that half of the bits of a double don't translate into a meaningful float (assuming a platform where float has half the size of a double, which is quite common). 浮点表示意味着double一半位不会转换为有意义的float (假定平台上float的大小是double的一半,这是很常见的)。 So, call C.at<double> instead. 因此,请改为调用C.at<double>

Actually, if you use the template version of cv::Mat_<_Tp> , you can access pixel value by Mat_<_Tp>::operator ()(int y, int x) 实际上,如果使用cv::Mat_<_Tp>的模板版本,则可以通过Mat_<_Tp>::operator ()(int y, int x)访问像素值

cv::Mat_<double> M(3, 3);
for (int i = 0;i < 3; ++i) {
  for (int j = 0;j < 3; ++j) {
    std::cout<<M(i, j)<<std::endl;
  }
}

so that later if you change the template argument from double to float, you don't need to modify each at() . 这样一来,如果稍后将template参数从double更改为float,则无需修改每个at()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM