我尝试将haar级联代码与直方图代码相结合
我尝试以下代码:
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]
我加上这个直方图代码
^{pr2}$在我运行了所有这些之后,我得到了一个错误:
Traceback (most recent call last):
File "/home/arizal/Documents/cascade jeruk/histogram/project1.py", line 52, in
cv2.rectangle(h,(xbin_width,y),(xbin_width + bin_width-1,hist_height), (255), -1)
TypeError: img is not a numpy array, neither a scalar
第52行的错误:
cv2.rectangle(h,(x*bin_width,y),(x*bin_width + bin_width-1,hist_height), (255), -1)
但是如果我删除级联代码,柱状图代码可以用相同的直方图代码正常运行吗?如何修复?在
你有:
它确实是一个有效的数组,但是它应该被指定为dtype,以确保它在以后的imshow中是可见的,因为它的目的是:
^{pr2}$对于普通灰度图像。但是,出现错误的原因是:
这将在
h
中放入一个数字来替换您的数组。在解决方案:
更改
h
的名称,同时尽量避免使用一个字母的名称,这是一种错误的做法,而且容易出错,特别是在python中,没有为变量设置类型。在顺便说一句,如果你按照评论的建议来写,这本可以更快更容易地发现:
在台前
您将得到类似于
int
的内容,并得到一个错误消息,h
没有dtype属性。在相关问题 更多 >
编程相关推荐