I'm trying to compare two histograms which I stored as an array. I'm new with the c++ interface (cv::Mat) and calculating histograms in OpenCV.
My code:
int testArr1[4] = {12, 10, 11, 11};
int testArr2[4] = {12, 0, 11, 0};
cv::Mat M1 = cv::Mat(1,4,CV_8UC1, testArr1);
cv::Mat M2 = cv::Mat(1,4,CV_8UC1, testArr2);
int histSize = 4;
float range[] = {0, 20};
const float* histRange = {range};
bool uniform = true;
bool accumulate = false;
cv::Mat a1_hist, a2_hist;
cv::calcHist(&M1, 1, 0, cv::Mat(), a1_hist, 1, &histSize, &histRange, uniform, accumulate );
cv::calcHist(&M2, 1, 0, cv::Mat(), a2_hist, 1, &histSize, &histRange, uniform, accumulate );
double compar_c = cv::compareHist(a1_hist, a2_hist, CV_COMP_CORREL);
double compar_chi = cv::compareHist(a1_hist, a2_hist, CV_COMP_CHISQR);
double compar_bh = cv::compareHist(a1_hist, a2_hist, CV_COMP_BHATTACHARYYA);
double compar_i = cv::compareHist(a1_hist, a2_hist, CV_COMP_INTERSECT);
cout << "compare(CV_COMP_CORREL): " << compar_c << "\n";
cout << "compare(CV_COMP_CHISQR): " << compar_chi << "\n";
cout << "compare(CV_COMP_BHATTACHARYYA): " << compar_bh << "\n";
cout << "compare(CV_COMP_INTERSECT): " << compar_i << "\n";
The results are a bit unexpected:
compare(CV_COMP_CORREL): 1
compare(CV_COMP_CHISQR): 0
compare(CV_COMP_BHATTACHARYYA): 0
compare(CV_COMP_INTERSECT): 4
For intersection, for example, I expected something like 0.5. What am I doing wrong? Can I not put arrays in a cv::mat? Or did I choose the wrong histogram "settings"?
The problem are your first 4 lines where you are converting the c array of integers to a matrix of chars. The constructor assumes a char array and therefore can't read the values properly. Your matrices M1 and M2 don't contain the correct values.
But if you change the following lines, so that the type of the array matches the type of the matrix:
char testArr1[4] = {12, 10, 11, 11};
char testArr2[4] = {12, 0, 11, 0};
I get the following output from your program:
compare(CV_COMP_CORREL): 0.57735
compare(CV_COMP_CHISQR): 2.66667
compare(CV_COMP_BHATTACHARYYA): 0.541196
compare(CV_COMP_INTERSECT): 2
Related
I think this should be a really simple thing but I don't get it solved. I'm trying to do a double contraction of two senond order Eigen tensors. Everything works well, but the result of the double contraction is an Eigen type:
Eigen::TensorContractionOp<const std::array<Eigen::IndexPair<int>, 2ul>, const Eigen::TensorFixedSize<double, Eigen::Sizes<3l, 3l> >, const Eigen::TensorFixedSize<double, Eigen::Sizes<3l, 3l> > >
but I need a double. I can print it but its not possible for me to work with it.
The code is the following
#include <iostream>
#include <unsupported/Eigen/CXX11/Tensor>
int main()
{
auto tensor1 = Eigen::TensorFixedSize<double, Eigen::Sizes<3,3>>();
tensor1.setValues({ {1, 0, 0},
{0, 1, 0},
{0, 0, 1} });
std::cout << "tensor1:\n" << tensor1 << "\n";
auto tensor2 = Eigen::TensorFixedSize<double, Eigen::Sizes<3,3>>();
tensor2.setValues({ {2, 0, 0},
{0, 2, 0},
{0, 0, 2} });
std::cout << "tensor2:\n" << tensor2 << "\n";
Eigen::array<Eigen::IndexPair<int>, 2> contraction_pair0011
= { Eigen::IndexPair<int>(0, 0), Eigen::IndexPair<int>(1, 1)};
auto tensor1_tensor2 = tensor1.contract(tensor2, contraction_pair0011);
std::cout << "tensor1 : tensor2:\n" << tensor1_tensor2 << "\n";
// double value = tensor1_tensor2; // won't compile
}
I need a function or call to get the value of the result, hope someone could help me.
Cheers Jonas
I solved the problem but think it will help you too if you are working with Eigen::Tensor module.
As written here in section Tensor Operations and C++ "auto":
Because Tensor operations create tensor operators, the C++ auto keyword does not have its intuitive meaning. When you use auto you do not get a Tensor as a result but instead a non-evaluated expression...
So the result of a tensor contraction is a
Eigen::TensorContractionOp<...>
and not a tensor from which we can get its elements. So we need to know the size of the resulting tensor. The Problem was that the result has to be a scalar tensor which is done with empty Eigen::Sizes<>
Eigen::TensorFixedSize<double, Eigen::Sizes<>>
Here the running code. I hope it helps somebody...
#include <iostream>
#include <unsupported/Eigen/CXX11/Tensor>
int main()
{
auto tensor1 = Eigen::TensorFixedSize<double, Eigen::Sizes<3,3>>();
tensor1.setValues({ {1, 0, 0},
{0, 1, 0},
{0, 0, 1} });
std::cout << "tensor1:\n" << tensor1 << "\n";
auto tensor2 = Eigen::TensorFixedSize<double, Eigen::Sizes<3,3>>();
tensor2.setValues({ {2, 0, 0},
{0, 2, 0},
{0, 0, 2} });
std::cout << "tensor2:\n" << tensor2 << "\n";
Eigen::array<Eigen::IndexPair<int>, 2> contraction_pair0011
= { Eigen::IndexPair<int>(0, 0), Eigen::IndexPair<int>(1, 1)};
Eigen::TensorFixedSize<double, Eigen::Sizes<>> tensor1_tensor2 = tensor1.contract(tensor2, contraction_pair0011);
std::cout << "tensor1 : tensor1:\n" << tensor1_tensor2 << "\n";
double t1_t2 = tensor1_tensor2(0);
std::cout << "result in double:\n" << t1_t2 << "\n";
}
This is how it should be done, and if i try simple code it works:
Mat a= Mat(4,3, CV_32FC1);
float elem_a= a.at<float>(i,j);
But after doing some math, this code gives worng results
Mat intrinsics(3, 3, CV_32FC1 );
Mat distortion( 5, 1, CV_32FC1 );
fs["camera_matrix"] >> intrinsics; //3*3
fs["distortion_coefficients"] >> distortion; //5*1
Mat rvec( 1, 3, CV_32FC1 );
Mat tvec( 1, 3, CV_32FC1 );
Mat R( 3, 3, CV_32FC1 );
Mat A( 3, 3, CV_32FC1 );
solvePnP( Mat(objectPoints), Mat(imagePoints), intrinsics, distortion, rvec, tvec, false );
Rodrigues( rvec, R );
A = intrinsics * R;
cout << "A = " << A << endl;
cout << "A[0] = " << A.at<float>(0,0) << "A[1] = " << A.at<float>(0,1) << endl;
Output:
A =
[-123.6820813196553, 792.0751394843999, -359.9404307669494;
668.8426426360758, -15.08087511838299, -513.8498143647524;
-0.3389607187919322, -0.03644067597638417, -0.9400945209128925]
A[0] = 4.12987e+09 A[1] = -3.48313
What Am I doing wrong?
Ty
Please check the data type of A matrix. I think it was silently converted to CV_64F.
I am trying to get max value from a 3-d Mat, but minmaxIdx and mixmaxloc both failed to do this.
int sz[] = {BIN, BIN, BIN};
Mat accumarray(3, sz, CV_8U, Scalar::all(0)) ;
double testMaxval = 0;
int minIdx = accumarray.dims ;
minMaxIdx(accumarray, NULL, &testMaxval,NULL,minIdx ,NULL) ;
cout<<testMaxval<<endl ;
This code wouldn't work, so Can I use max(), minmaxidx(), or minmaxloc() to get the max value efficiently without manually process the entire n-dimensional array?
Following code works for me with OpenCV 2.3.1:
int sz[] = {3, 3, 3};
Mat accumarray(3, sz, CV_8U, Scalar::all(0));
accumarray.at<uchar>(0, 1, 2) = 20;
double testMaxval;
int maxIdx[3];
minMaxIdx(accumarray, 0, &testMaxval, 0, maxIdx);
cout << testMaxval << endl ;
cout << maxIdx[0] << ", " << maxIdx[1] << ", " << maxIdx[2] << endl;
Use Mat() instead of NULL for Mask or you will vioulate an assertion Mask.empty()
Mat m;
double min, max;
int minInd, maxInd;
cv::minMaxIdx(m, &min, &max, &minInd, &maxInd, Mat());
With Boost's accumulators I can easily calculate statistical quantities for
weighted or unweighted input sets. I wonder if it is possible to mix weighted
and unweighted quantities inside the same accumulator. Looking at the
docs it doesn't seem that way.
This compiles fine but produces another result than I would have liked:
using namespace boost::accumulators;
const double a[] = {1, 1, 1, 1, 1, 2, 2, 2, 2};
const double w[] = {1, 2, 3, 4, 5, 6, 7, 8, 9};
accumulator_set<double, features<tag::sum, tag::weighted_sum>, double> stats;
for (size_t i=0; i<9; ++i)
stats(a[i], weight = w[i]);
std::cout << sum(stats) <<" "<< weighted_sum(stats) << std::endl;
// outputs "75 75" instead of "13 75"
Also, with a third template parameter to accumulator_set I always seems to
get weighted quantities, even when using an "unweighted" feature and extractor:
accumulator_set<double, features<tag::sum>, double> stats;
for (size_t i=0; i<9; ++i)
stats(a[i], weight = w[i]);
std::cout << sum(stats) << std::endl;
// outputs "75" instead of 13
Do I always have to use two different accumulators if I want to calculate both
weighted and unweighted quantities?
EDIT
I just use sum as an example, in reality I am interested in multiple, more complicated quantities.
It does say in the documentation that
When you specify a weight, all the
accumulators in the set are replaced
with their weighted equivalents.
There are probably better ways to do it but you can try something like this (basically swapping the meaning of the value with that of the weight):
accumulator_set< double, stats< tag::sum, tag::sum_of_weights >, double > acc;
const double a[] = {1, 1, 1, 1, 1, 2, 2, 2, 2};
const double w[] = {1, 2, 3, 4, 5, 6, 7, 8, 9};
for( int i = 0; i < sizeof( a ) / sizeof( a[ 0 ] ); i++ )
acc( w[ i ], weight = a[ i ] );
std::cout << extract_result< tag::sum >( acc ) << std::endl; // weighted sum, prints 75
std::cout << extract_result< tag::sum_of_weights >( acc ) << std::endl; // sum, prints 13
How to access elements by row, col in OpenCV 2.0's new "Mat" class? The documentation is linked below, but I have not been able to make any sense of it.
http://opencv.willowgarage.com/documentation/cpp/basic_structures.html#mat
On the documentation:
http://docs.opencv.org/2.4/modules/core/doc/basic_structures.html#mat
It says:
(...) if you know the matrix element
type, e.g. it is float, then you can
use at<>() method
That is, you can use:
Mat M(100, 100, CV_64F);
cout << M.at<double>(0,0);
Maybe it is easier to use the Mat_ class. It is a template wrapper for Mat.
Mat_ has the operator() overloaded in order to access the elements.
The ideas provided above are good. For fast access (in case you would like to make a real time application) you could try the following:
//suppose you read an image from a file that is gray scale
Mat image = imread("Your path", CV_8UC1);
//...do some processing
uint8_t *myData = image.data;
int width = image.cols;
int height = image.rows;
int _stride = image.step;//in case cols != strides
for(int i = 0; i < height; i++)
{
for(int j = 0; j < width; j++)
{
uint8_t val = myData[ i * _stride + j];
//do whatever you want with your value
}
}
Pointer access is much faster than the Mat.at<> accessing. Hope it helps!
Based on what #J. Calleja said, you have two choices
Method 1 - Random access
If you want to random access the element of Mat, just simply use
Mat.at<data_Type>(row_num, col_num) = value;
Method 2 - Continuous access
If you want to continuous access, OpenCV provides Mat iterator compatible with STL iterator and it's more C++ style
MatIterator_<double> it, end;
for( it = I.begin<double>(), end = I.end<double>(); it != end; ++it)
{
//do something here
}
or
for(int row = 0; row < mat.rows; ++row) {
float* p = mat.ptr(row); //pointer p points to the first place of each row
for(int col = 0; col < mat.cols; ++col) {
*p++; // operation here
}
}
If you have any difficulty to understand how Method 2 works, I borrow the picture from a blog post in the article Dynamic Two-dimensioned Arrays in C, which is much more intuitive and comprehensible.
See the picture below.
OCV goes out of its way to make sure you can't do this without knowing the element type, but if you want an easily codable but not-very-efficient way to read it type-agnostically, you can use something like
double val=mean(someMat(Rect(x,y,1,1)))[channel];
To do it well, you do have to know the type though. The at<> method is the safe way, but direct access to the data pointer is generally faster if you do it correctly.
For cv::Mat_<T> mat just use mat(row, col)
Accessing elements of a matrix with specified type cv::Mat_< _Tp > is more comfortable, as you can skip the template specification. This is pointed out in the documentation as well.
code:
cv::Mat1d mat0 = cv::Mat1d::zeros(3, 4);
std::cout << "mat0:\n" << mat0 << std::endl;
std::cout << "element: " << mat0(2, 0) << std::endl;
std::cout << std::endl;
cv::Mat1d mat1 = (cv::Mat1d(3, 4) <<
1, NAN, 10.5, NAN,
NAN, -99, .5, NAN,
-70, NAN, -2, NAN);
std::cout << "mat1:\n" << mat1 << std::endl;
std::cout << "element: " << mat1(0, 2) << std::endl;
std::cout << std::endl;
cv::Mat mat2 = cv::Mat(3, 4, CV_32F, 0.0);
std::cout << "mat2:\n" << mat2 << std::endl;
std::cout << "element: " << mat2.at<float>(2, 0) << std::endl;
std::cout << std::endl;
output:
mat0:
[0, 0, 0, 0;
0, 0, 0, 0;
0, 0, 0, 0]
element: 0
mat1:
[1, nan, 10.5, nan;
nan, -99, 0.5, nan;
-70, nan, -2, nan]
element: 10.5
mat2:
[0, 0, 0, 0;
0, 0, 0, 0;
0, 0, 0, 0]
element: 0