![](/img/trans.png)
[英]Python TypeError : only integer scalar arrays can be converted to a scalar index
[英]TypeError: only size -1 arrays can be converted to python scalar
我正面临这个错误,并且是 python 的新手。 不确定如何不应该输入矩阵作为输入(图像)这是代码:
import cv2
import matplotlib.pyplot as plt
image = cv2.imread('sample.jpg')
def show_rgb_hist(image):
colours = ('r','g','b')
for i, c in enumerate(colours):
plt.figure(figsize=(20, 4))
histr = cv2.calcHist([image], [i], None, [256], [0, 256])
if c == 'r': colours = [((i/256, 0, 0)) for i in range(0, 256)]
if c == 'g': colours = [((0, i/256, 0)) for i in range(0, 256)]
if c == 'b': colours = [((0, 0, i/256)) for i in range(0, 256)]
plt.bar(range(0, 256), histr, color=colours, edgecolor=colours, width=1)
plt.show()
x=show_rgb_hist(image)
cv2.imshow('img', x)
整个问题在于histr
255x1 的尺寸,使用 ravel ravel()
解决了这个问题。
import cv2
import matplotlib.pyplot as plt
image = cv2.imread('Crous.jpg')
def show_rgb_hist(image):
colours = ('r','g','b')
for i, c in enumerate(colours):
plt.figure(figsize=(20, 4))
histr = cv2.calcHist([image], [i], None, [256], [0, 256])
print(histr.shape) # shape here 255 x 1
histr = histr.ravel() # this line will solve the problem
if c == 'r': colours = [((i/256, 0, 0)) for i in range(0, 256)]
if c == 'g': colours = [((0, i/256, 0)) for i in range(0, 256)]
if c == 'b': colours = [((0, 0, i/256)) for i in range(0, 256)]
plt.bar(range(0, 256), histr, color=colours, edgecolor=colours, width=1)
plt.show()
x = show_rgb_hist(image)
# cv2.imshow('img', x) # no need for this
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.