Ask Your Question

rajesh's profile - activity

2015-01-09 01:32:16 -0600 commented question colorHist python and cpp

Even a simple looping code produces different result - Am I printing the MatND correctly ?

void printMatrix(Mat mnd) {

    for(int i=0;i<4;i++) {
        for(int j=0;j<4;j++) {
            for(int k=0;k<4;k++) {
                cout << setprecision(11)<<mnd.at<double>(i,j,k)<<"\t";
            }
            cout<<endl;
        }
        cout<<endl<<endl;
    }
}
2015-01-09 01:29:35 -0600 received badge  Student (source)
2015-01-09 01:23:42 -0600 asked a question colorHist python and cpp

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