TypeError: only size -1 arrays can be converted to python scalar

asked 2020-08-01 22:50:39 -0500

i am facing this error and am new to python. not sure how am not supposed to enter a matrix as the input ( image) this is the code :

   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)


   cv2.imshow('img', x)
edit retag flag offensive close merge delete


indentation looks broken, can you check again, please ?

please also give us the complete stacktrace, so we don't have to guess, where the error is coming from

berak gravatar imageberak ( 2020-08-02 01:19:12 -0500 )edit

show_rgb_hist does not return anything, so x will be always invalid

also, take another look at the tutorials

berak gravatar imageberak ( 2020-08-02 02:20:09 -0500 )edit