如何使用开放的 CV 在 python 中计算 3D 直方图

Khu*_*boo 4 python opencv histogram

我想在 python 中计算我的 Cielab 图像的 3D 直方图。我正在使用 openCV 来计算我的直方图。我想使用compareHistopenCV 的函数比较图像,这就是为什么我使用 openCV 来计算图像的 3D 直方图。

我尝试使用以下变量:

i_lab = image.copy()
i_lab = i_lab.astype(np.uint8)
Range_hist = [[0, 100], [-100, 100], [-100, 100]]    
hist_1 = cv2.calcHist([i_lab], [[0], [1], [2]], None, [[20], [20], [20]], Range_hist)
Run Code Online (Sandbox Code Playgroud)

但它给出了错误SystemError: error return without exception set 请告诉我我做错了什么,是否可以在 python 中使用 openCV 计算 3D 直方图

小智 5

我在尝试制作 HSV 图像的 3D 直方图时遇到了这个问题,并遇到了同样的错误。事实证明,OpenCV 文档让我们误入歧途。这些文档是为 C++ API 编写的,因此只能用作 Python cv2API的模糊指南(尽管我发现这些文档有时也会误导 C++)。

函数签名如下:

cv2.calcHist(images, channels, mask, histSize, ranges[, hist[, accumulate]]) -> hist
Run Code Online (Sandbox Code Playgroud)

关键是channels,histSizeranges参数应该是平面列表,而不是您的示例中的嵌套列表。尝试以下操作,假设i_lab是三通道图像:

range_hist = [0, 100, -100, 100, -100, 100]
hist_1 = cv2.calcHist([i_lab], [0, 1, 2], None, [20, 20, 20], range_hist)
Run Code Online (Sandbox Code Playgroud)

对于一个更完整的例子,尝试该代码清单opencvpython博客