1

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
}
1
  • 1
    Does it work if you do double comp = C.at<double>(0,0);? Commented Jun 8, 2013 at 21:47

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.