python - 如何在C++中展平颜色直方图?

标签 python c++ opencv opencv3.0

嗨,我用 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()

我现在正在尝试用 C++ 重写它。我在 http://www.swarthmore.edu/NatSci/mzucker1/opencv-2.4.10-docs/doc/tutorials/imgproc/histograms/histogram_calculation/histogram_calculation.html 找到了一个很好的例子

我现在面临的问题是在c++中展平hist,例如在python代码中。这是python中展平hist输出的形状(512,)。关于如何在 C++ 中获得相同结果的任何想法?

(编辑) 到目前为止的c++代码。

尺寸大小(500,500); 图像 = 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);

最佳答案

只是为这个问题添加一个答案。由于您使用 OpenCV cv::Mat 作为直方图持有者,因此展平它的一种方法是使用 reshape 例如:

// 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);

这个 O(1) 函数不会复制元素,只是更改 cv::Mat header 以获得正确的大小。

之后您将拥有 1 行 cv::mat 和 262144 列。

关于python - 如何在C++中展平颜色直方图?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52065142/

相关文章:

c++ - 通过对立方体应用变换来创建 3D 方形平截头体?

python - 如何找到对象(形状)的方向? - Python Opencv

python - BSD/OS X 上的网络掩码/IP 地址查找

python - 多行正则表达式匹配

c++ - 从 std::map 中删除 std::function lambda-wrapped 方法

c++ - 另一个类的友元类函数?

c++ - 如何在 QtCreator 中链接 opencv 并使用 Qt 库

c# - 关于OpenCV模板匹配

python - 如何在 Pandas 数据框中使用带有多个表达式的 str.contains()?

Python 3.6 安装 statsmodels 失败,退出状态为 127