I am a newbie to opencv, so pls bear with if I missed something very basic.. I've been using the python interface of OpenCV so-far and now getting used to the CPP interface...
I notice that there is some difference in output when I use the colorHist() function in python and CPP..- Pls let me know if I have missed something in the CPP code to get the same output as that of the python code..
Is the below way of printing/formatting a MatND object correct ? - Also any pointer to the python wrapper for C++ interface that I could use as reference ? Thanks in advance
Python code :
#! /usr/bin/env python
import cv2;
import sys;
image = cv2.imread(sys.argv[1]);
dim = (128, 128);
resized = cv2.resize(image, dim, interpolation = cv2.INTER_AREA);
hist = cv2.calcHist([resized], [0, 1, 2],
None, [4, 4, 4], [0, 256, 0, 256, 0, 256]);
print "#####raw hist#####\n",hist;
CPP code :
#include <stdio.h>
#include <string>
#include <opencv2/opencv.hpp>
using namespace std;
using namespace cv;
void printMatrix(Mat mnd) {
int dims[] = {4,4,4};
std::vector<cv::Mat> matVec;
for (int p = 0; p < dims[2]; ++p) {
double *ind = (double*)mnd.data + p * dims[0] * dims[1];
matVec.push_back(cv::Mat(2, dims, CV_64F, ind).clone());
}
std::cout << "Size of matVec: " << matVec.size() << std::endl;
std::cout << "Size of first Mat: " << matVec[0].size() << std::endl;
for(int i=0;i<matVec.size();i++) {
std::cout << "\nmatVec["<<i<<"]:\n" << matVec[i] << std::endl;
}
}
int main(int argc, char** argv )
{
if ( argc != 2 )
{
printf("usage: opencv.out <Image_Path>\n");
return -1;
}
Mat image, resizedImage;
image = imread( argv[1], 1 );
Size size(128,128);
resize(image, resizedImage, size, INTER_AREA);
MatND hist;
int imgCount = 1;
int dims = 3;
const int sizes[] = {4,4,4};
const int channels[] = {0,1,2};
float rRange[] = {0,256};
float gRange[] = {0,256};
float bRange[] = {0,256};
const float *ranges[] = {rRange,gRange,bRange};
Mat mask = Mat();
calcHist(&resizedImage, imgCount, channels, mask, hist, dims, sizes, ranges);
cout<<"*******raw Hist***********"<<endl;
printMatrix(hist);
return 0;
}
Python output of hist:
python test.py ./lena.jpg
#####raw hist#####
[[[ 2.93700000e+03 2.76800000e+03 2.26000000e+02 0.00000000e+00]
[ 0.00000000e+00 1.68000000e+02 2.50300000e+03 1.00000000e+00]
[ 0.00000000e+00 0.00000000e+00 0.00000000e+00 0.00000000e+00]
[ 0.00000000e+00 0.00000000e+00 0.00000000e+00 0.00000000e+00]]
[[ 2.80000000e+01 2.69000000e+02 1.00000000e+00 0.00000000e+00]
[ 0.00000000e+00 4.61000000e+02 3.96500000e+03 8.84000000e+02]
[ 0.00000000e+00 0.00000000e+00 2.45000000e+02 6.63000000e+02]
[ 0.00000000e+00 0.00000000e+00 0.00000000e+00 0.00000000e+00]]
[[ 0.00000000e+00 0.00000000e+00 0.00000000e+00 0.00000000e+00]
[ 0.00000000e+00 4.00000000e+00 1.00000000e+00 0.00000000e+00]
[ 0.00000000e+00 0.00000000e+00 7.00000000e+01 1.16700000e+03]
[ 0.00000000e+00 0.00000000e+00 0.00000000e+00 1.80000000e+01]]
[[ 0.00000000e+00 0.00000000e+00 0.00000000e+00 0.00000000e+00]
[ 0.00000000e+00 0.00000000e+00 0.00000000e+00 0.00000000e+00]
[ 0.00000000e+00 0.00000000e+00 0.00000000e+00 2.00000000e+00]
[ 0.00000000e+00 0.00000000e+00 0.00000000e+00 3.00000000e+00]]]
CPP output of hist :
./opencvImage-basic ./lena.jpg
*******raw Hist***********
Size of matVec: 4
Size of first Mat: [4 x 4]
matVec[0]:
[1.666051144622405e+25, 5.588630272226044e-315, 2040693581152256, 512.0001317816786;
0, 0, 0, 0;
3.039930190974157e+16, 0, 9.799832789158199e+18, 2.471864316870538e+21;
0, 1.660207337268062e+20, 0, 0]
matVec[1]:
[0, 0, 262144, 5.377518630424689e-315;
0, 2.774390778232357e+22, 0, 4294967296;
0, 0, 0, 0;
0, 0, 0, 4194304]
matVec[2]:
[6.953268614625385e-310, 6.953268614625188e-310, 6.951141603555176e-310, 6.951141603555176e-310;
4.243991581930545e-314, 6.951141608611246e-310, 6.951141608611246e-310, 1.264808053353591e-321;
0, 0, 0, 0;
2.681561585988519e+154, 2.681561585988519e+154, 2.681561585988519e+154, 6.951141608629428e-310]
matVec[3]:
[3.162020133383978e-322, 7.905050333459945e-323, 1.976262583364986e-323, 8.487983165343286e-314;
8.487983165837352e-314, 4.232774800424432e+175, 2.681561585988519e+154, 6.95114160863259e-310;
1.666051144622405e+25, 5.588630272226044e-315, 2040693581152256, 512.0001317816786;
0, 0, 0, 0]