2

Hi I have written the following lines of code in python:

# convert the image to HSV color-space
image = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)

# compute the color histogram
hist  = cv2.calcHist([image], [0, 1, 2], None, [bins, bins, bins], [5, 240, 5, 240, 5, 240])

# normalize the histogram
cv2.normalize(hist, hist)

# return the histogram
return hist.flatten()

I am now trying to re-write it in c++. I found a excellent example at http://www.swarthmore.edu/NatSci/mzucker1/opencv-2.4.10-docs/doc/tutorials/imgproc/histograms/histogram_calculation/histogram_calculation.html

The problem which I face now is to flatten the hist in c++ such as in the python code.This is the shape of the flatten hist output in python (512,). Any ideas as how to get the same results in c++?

(Edit) c++ code up to this point.

Size size(500,500); image = imread("C:\johan.jpg",IMREAD_COLOR);

 resize(image,image,size);//resize image

 cvtColor(image, image, CV_BGR2HSV);
// Separate the image in 3 places ( H, S and V )
 vector<Mat> bgr_planes;
 split(image, bgr_planes );

 vector<Mat> hist_flat;

 // Establish the number of bins
 int histSize = 256;

 // Set the ranges ( for H,S,V) )
 float range[] = {5, 240} ;
 const float* histRange = { range };

 bool uniform = true; bool accumulate = false;

 Mat b_hist, g_hist, r_hist;

 cout << " Working fine Johan...";

 // Compute the histograms:
 calcHist( &bgr_planes[0], 1, 0, Mat(), b_hist, 1, &histSize, &histRange, uniform, accumulate );
 calcHist( &bgr_planes[1], 1, 0, Mat(), g_hist, 1, &histSize, &histRange, uniform, accumulate );
 calcHist( &bgr_planes[2], 1, 0, Mat(), r_hist, 1, &histSize, &histRange, uniform, accumulate );
 //calcHist( &image,3, 0, Mat(), hist_flat, 1, &histSize, &histRange, uniform, accumulate );



 // Draw the histograms for B, G and R
 int hist_w = 512; int hist_h = 400;
 int bin_w = cvRound( (double) hist_w/histSize );



 Mat histImage(hist_h,hist_w, CV_8UC3, Scalar(0,0,0));

 // Normalize the result to [ 0, histImage.rows ]
 normalize(b_hist, b_hist, 0, histImage.rows, NORM_MINMAX, -1, Mat() );
 normalize(g_hist, g_hist, 0, histImage.rows, NORM_MINMAX, -1, Mat() );
 normalize(r_hist, r_hist, 0, histImage.rows, NORM_MINMAX, -1, Mat() );



 // Draw for each channel
 for( int i = 1; i < histSize; i++ )
 {
     line( histImage, Point( bin_w*(i-1), hist_h - cvRound(b_hist.at<float>(i-1)) ) ,
                      Point( bin_w*(i), hist_h - cvRound(b_hist.at<float>(i)) ),
                      Scalar( 255, 0, 0), 2, 8, 0  );
     line( histImage, Point( bin_w*(i-1), hist_h - cvRound(g_hist.at<float>(i-1)) ) ,
                      Point( bin_w*(i), hist_h - cvRound(g_hist.at<float>(i)) ),
                      Scalar( 0, 255, 0), 2, 8, 0  );
     line( histImage, Point( bin_w*(i-1), hist_h - cvRound(r_hist.at<float>(i-1)) ) ,
                      Point( bin_w*(i), hist_h - cvRound(r_hist.at<float>(i)) ),
                      Scalar( 0, 0, 255), 2, 8, 0  );
 }

 // Display

 imshow("calcHist Demo", histImage );
 imshow("The image resized",image);
Johan Fick
  • 375
  • 2
  • 14
  • Strictly speaking there are no histograms in c++ if you dont use some library that provides such functionality. However, a `std::map` provides already all you need for a histogram. That being said, Stackoverflow is not a codewriting service, if you want to get help you need to show what you tried and ask a specific question – 463035818_is_not_an_ai Aug 28 '18 at 20:25
  • Hi thanks for the comment. I am currently using opencv to provide the functionality to be able to create a histogram of a given image. Here is the c++ code which I have written up to this point (added to question).To be more specific how can I flatten the histogram generated for a given image using opencv 3.1.0 and c++. In the code I have written I break the image up into HSV parts and then determine the histograms and plot them in for visualization purposes. – Johan Fick Aug 28 '18 at 20:42

2 Answers2

2

Basically you want to flatten a 2D array ( hist = cv2.calcHist([image], [0, 1, 2], None, [bins, bins, bins], [5, 240, 5, 240, 5, 240]) is 2D array 235x3 )

Easiest code for this is in is in function in C++ similar to numpy flatten

The basic algorithm is ( cf http://www.ce.jhu.edu/dalrymple/classes/602/Class12.pdf )

for (q = 0; q < n; q++)
{
    for (t = 0; t < m; t++)
    {
        b[q * n + t] = a[q][t];  <-------
    }
}

source : C++ 2D array to 1D array

( for 3D array cf How to "flatten" or "index" 3D-array in 1D array? )

ralf htp
  • 9,149
  • 4
  • 22
  • 34
2

Just to add one more answer to this question. Since you are using OpenCV cv::Mat as your histogram holder, one way to flatten it is using reshape for example:

// create mat a with 512x512 size and float type
cv::Mat a(512,512,CV_32F);
// resize it to have only 1 row
a = a.reshape(0,1);

This O(1) function and does not copy the elements, just changes the cv::Mat header to have the correct size.

After it you will have a 1 row cv::mat with 262144 columns.

api55
  • 11,070
  • 4
  • 41
  • 57