TypeError:只能将大小为1的数组转换为python标量
我面临着这个错误,而且对python还是新手。不确定如何输入矩阵作为输入(图像)这是代码:TypeError:只能将大小为1的数组转换为python标量,python,opencv,image-processing,Python,Opencv,Image Processing,我面临着这个错误,而且对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=(
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()
解决了问题
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
“我正面临这个错误”请将错误消息的完整回溯放在帖子的正文中,格式为代码。