Pedro
Pedro

Reputation: 293

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. The cout of comp is not matching the first element of the array C. I'm new to OpenCV so any help is appreciated.

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
}

Upvotes: 1

Views: 1183

Answers (2)

cxyzs7
cxyzs7

Reputation: 1227

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_<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().

Upvotes: 0

juanchopanza
juanchopanza

Reputation: 227418

Your Mat_<double> instances internally store doubles. 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. 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). So, call C.at<double> instead.

Upvotes: 1

Related Questions