如何在c++中展平颜色直方图?

2024-05-16 01:39:32 发布

您现在位置:Python中文网/ 问答频道 /正文

嗨,我用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++中(比如在python中)展平hist代码。这个是python(512,)中展平hist输出的形状。对于如何在c++中获得相同的结果有什么想法吗?在

(编辑) c++代码。在

尺寸(500500); 图像=imread(“C:\约翰.jpg“,图像颜色)

^{pr2}$

Tags: theto代码图像imageconvertreturncv2
2条回答

只是想再给这个问题加一个答案。由于您使用OpenCV cv::Mat作为直方图固定器,因此一种使其变平的方法是使用“重塑”例如:

// 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头的大小就可以了。在

之后,您将得到一个包含262144列的1行cv::mat。在

基本上,您需要展平一个二维数组(hist = cv2.calcHist([image], [0, 1, 2], None, [bins, bins, bins], [5, 240, 5, 240, 5, 240])是2D数组235x3)

最简单的代码在function in C++ similar to numpy flatten

基本算法是(cfhttp://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];  <   -
    }
}

来源:C++ 2D array to 1D array

(对于3D阵列cfHow to "flatten" or "index" 3D-array in 1D array?

相关问题 更多 >