I am currently struggling with the very weird behavior of OpenCV / C ++. This is what I do:
Updated 1: Here is more information:
I compute the gradient orientation for a set of pixels and store them in the double field of the declared self struct . Elements of the structure are saved in the vector.
myfunct1() { (...) c.grad_orientation = (sum_ori / highgrads.size()) + M_PI;
Later, I got several c stored in a vector, and I want to estimate the average value of each individual grad_orientation stored in each structural element of my vector, which looks like this:
myfunct0() { myfunct1(); //adds element to the my_struct_vector (...) int n = 0; double total_ori = 0.0; for (uint i = 0; i < my_struct_vector.size(); ++i) { total_ori += my_struct_vector[i].grad_orientation; n++; } azimuth = (total_ori / n); cout << "Average: " << azimuth * 180/M_PI << endl; // print out in degrees }
Now for the funny part: if I do this under certain circumstances, cout prints 215.963 . This is the result that I got most often (EXACTLY this result). In some cases, if I add the matrices mentioned above or (yes, really), if I move the double field while holding the grad_orientation in the structure one column higher in terms of code, I get 223.442 . Thus, the difference between the two results in the code is only as follows:
struct my_struct { std::vector<cv::Point> contour_shadow; std::vector<cv::Point> contour_light; cv::RotatedRect rect; cv::Point pf; cv::Point pc; double grad_orientation;
};
The printed results depend not only on the column position in the structure declaration, but also when changing different parts of my code.
This may have something to do with double precision, but why does it change if I move the columns of code?
source share