Python img不是numpy数组,也不是标量数组

Python img不是numpy数组,也不是标量数组,python,numpy,opencv,histogram,cascade,Python,Numpy,Opencv,Histogram,Cascade,我尝试将haar级联代码与直方图代码结合起来 histcam = cv2.calcHist([cam], [0], None, [nbins], [0,256]) cv2.normalize(histcam,histcam, hist_height, cv2.NORM_MINMAX) hist=np.int32(np.around(histcam)) for x,y in enumerate(hist): cv2.rectangle(h,(x*b

我尝试将haar级联代码与直方图代码结合起来

    histcam = cv2.calcHist([cam], [0], None, [nbins], [0,256])
    cv2.normalize(histcam,histcam, hist_height, cv2.NORM_MINMAX)
    hist=np.int32(np.around(histcam))

    for x,y in enumerate(hist):
        cv2.rectangle(h,(x*bin_width,y),(x*bin_width + bin_width-1,hist_height), (255), -1)

    #Flip upside down
    h=np.flipud(h)

#Show the histogram
    cv2.imshow('Color Histogram',h)
    h = np.zeros((hist_height,hist_width))

    cv2.imshow('img',img)
    k = cv2.waitKey(30) & 0xff
    if k == 27:
        break

camera.release()
cv2.destroyAllWindows()
我尝试以下代码:

import cv2
import numpy as np 
from matplotlib import pyplot as plt

#Cascade jeruk
jeruk_cascade = cv2.CascadeClassifier('cascade.xml')

camera = cv2.VideoCapture(1)

base1 = cv2.imread('base1.png')
base2 = cv2.imread('base2.png')
base3 = cv2.imread('base3.png')

#Set hist parameters
hist_height = 64
hist_width = 256
nbins = 32
bin_width = hist_width/nbins
hrange = [0,180]
srange = [0,256]
ranges = hrange+srange                                  # ranges =   [0,180,0,256]

#Create an empty image for the histogram
h = np.zeros((hist_height,hist_width))

while 1:
    grabbed, img = camera.read()
    cam = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    if not grabbed:
      "Camera could not be started."
      break

    # add this
    # image, reject levels level weights.
    jeruks = jeruk_cascade.detectMultiScale(cam, 1.03, 5)

    # add this
    for (x,y,w,h) in jeruks:

    #    cv2.rectangle(img,(x,y),(x+w,y+h),(17,126,234),2)
    font = cv2.FONT_HERSHEY_SIMPLEX
        cv2.putText(img,'Jeruk',(x+w,y+h), font, 1, (17,126,234), 2, cv2.LINE_AA) #---write the text

        roi_gray = cam[y:y+h, x:x+w]
        roi_color = img[y:y+h, x:x+w]
我添加了这个直方图代码

    histcam = cv2.calcHist([cam], [0], None, [nbins], [0,256])
    cv2.normalize(histcam,histcam, hist_height, cv2.NORM_MINMAX)
    hist=np.int32(np.around(histcam))

    for x,y in enumerate(hist):
        cv2.rectangle(h,(x*bin_width,y),(x*bin_width + bin_width-1,hist_height), (255), -1)

    #Flip upside down
    h=np.flipud(h)

#Show the histogram
    cv2.imshow('Color Histogram',h)
    h = np.zeros((hist_height,hist_width))

    cv2.imshow('img',img)
    k = cv2.waitKey(30) & 0xff
    if k == 27:
        break

camera.release()
cv2.destroyAllWindows()
在我运行了所有这些之后,我得到了以下错误:

回溯(最近一次呼叫最后一次):

文件“/home/arizal/Documents/cascade-jeruk/histogram/project1.py”,第52行,在

cv2.矩形(h,(xbin_宽度,y),(xbin_宽度+bin_宽度-1,历史高度),(255),-1)

TypeError:img不是numpy数组,也不是标量

第52行中的错误:

cv2.rectangle(h,(x*bin_width,y),(x*bin_width + bin_width-1,hist_height), (255), -1)
但是如果我删除级联代码,直方图代码可以使用相同的直方图代码运行而不会出错?如何修复它?

您有:

h = np.zeros((hist_height,hist_width))
这确实是一个有效的数组,但应将其指定为dtype,以确保其在以后的imshow中可见,如所示:

h = np.zeros((hist_height,hist_width), dtype=np.uint8)
对于普通灰度图像。但是,出现错误是因为您编写了:

for (x,y,w,h) in jeruks:
这将在
h
中放置一个数字,替换您拥有的阵列

解决方案:

更改
h
的名称,同时尽量避免使用单字母名称,这是一种不好的做法,并且容易出错,特别是在python中,没有为变量设置类型

顺便说一句,如果你按照评论的建议写的话,这本可以更快更容易地被发现:

print(type(h))
print(h.dtype)
排队前

for x,y in enumerate(hist):
    cv2.rectangle(h,(x*bin_width,y),(x*bin_width + bin_width-1,hist_height), (255), -1)

您可能会得到类似于
int
的内容,并出现一个错误,说明
h
没有数据类型属性。

您可以打印该行之前的类型(h)和h.dtype吗?使用打印功能打印到终端@API55是的,我的意思是它不是一个解决方案,但可能会告诉你它是什么,也许它可以帮助确定问题。我在第24行打印:h=np.zeros((历史高度,历史宽度)),这是结果:('h:',数组([0,0,0,0,0,0,0.]),[0,0,0,0,0,0.],[0,0,0,0.],[0,0,0,0,0.]如果答案解决了你的问题,最好把它标为答案,帮助人们找到答案faster@ArizalZanuarRisqi如果答案对你有帮助,请接受。如果有更多的事情,那么请提出请求。