Python TypeError:src不是numpy数组,也不是标量。无流
我试图找到轮廓时,从我的无人机流检测轮廓的排水沟,但我得到这个错误。我不知道如何修理它。任何帮助都将不胜感激。您正在使用Python TypeError:src不是numpy数组,也不是标量。无流,python,python-2.7,opencv,numpy,image-processing,Python,Python 2.7,Opencv,Numpy,Image Processing,我试图找到轮廓时,从我的无人机流检测轮廓的排水沟,但我得到这个错误。我不知道如何修理它。任何帮助都将不胜感激。您正在使用cam对象作为图像cv2。视频捕获对象用于读取帧 import cv2 import numpy as np cam = cv2.VideoCapture('tcp://192.168.1.1:5555') img_gray = cv2.cvtColor(cam,cv2.COLOR_BGR2GRAY) ret, thresh = cv2.threshold(img_gray,
cam
对象作为图像<代码>cv2。视频捕获对象用于读取帧
import cv2
import numpy as np
cam = cv2.VideoCapture('tcp://192.168.1.1:5555')
img_gray = cv2.cvtColor(cam,cv2.COLOR_BGR2GRAY)
ret, thresh = cv2.threshold(img_gray, 127, 255,0)
contours,hierarchy = cv2.findContours(thresh,2,1)
cnt = contours[0]
hull = cv2.convexHull(cnt,returnPoints = False)
defects = cv2.convexityDefects(cnt,hull)
for i in range(defects.shape[0]):
s,e,f,d = defects[i,0]
start = tuple(cnt[s][0])
end = tuple(cnt[e][0])
far = tuple(cnt[f][0])
cv2.line(img,start,end,[0,255,0],2)
cv2.circle(img,far,5,[0,0,255],-1)
cv2.imshow('img',cam)
cv2.waitKey(0)
cv2.destroyAllWindows()
我认为您可以这样做来解决代码中的问题。您缺少用于从源摄像机捕获帧的read()函数。让我知道这是否是问题所在。我需要添加什么代码?谢谢。现在我正试着把这个代码输入到我的无人机上,这样它就可以跟着我了。我有什么办法可以做到吗?我不太清楚该怎么做对不起。我对此并不熟悉。提出另一个问题并给出合理的解释,有人会帮助你。如果它解决了问题,请接受答案。实际上,我收到了一个错误,其值太多,无法解包。轮廓,层次=cv2。查找轮廓(thresh,2,1)2和1表示什么?
im2,轮廓,层次=cv2。查找轮廓(thresh,cv2.RETR\u TREE,cv2.CHAIN\u About\u SIMPLE)
这是从图像中获取轮廓的有效代码。其中,thresh计算为ret,thresh=cv2。阈值(imgray,127255,0)
# create cv2.VideoCapture object with your parameters
cam = cv2.VideoCapture('tcp://192.168.1.1:5555')
# check if the object created successfully
if not cam.isOpened():
print("VideoCapture failed to open")
# start reading frames until user interrupts or end of file is encountered
while True:
ret, frame = cam.read()
if ret == False:
print("frame empty")
break
# process frame here
# display frame
cv2.imshow("frame",frame)
key = cv2.waitKey(1)
# if 'q' is pressed, then quit loop
if k == ord('q')
break
import cv2
import numpy as np
cam = cv2.VideoCapture('tcp://192.168.1.1:5555')
if not cam.isOpened():
print("VideoCapture failed to open")
while True:
ret, frame = cam.read()
if ret == False:
print("frame empty")
break
img_gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
ret, thresh = cv2.threshold(img_gray, 127, 255,0)
contours,hierarchy = cv2.findContours(thresh,2,1)
cnt = contours[0]
hull = cv2.convexHull(cnt,returnPoints = False)
defects = cv2.convexityDefects(cnt,hull)
for i in range(defects.shape[0]):
s,e,f,d = defects[i,0]
start = tuple(cnt[s][0])
end = tuple(cnt[e][0])
far = tuple(cnt[f][0])
cv2.line(img,start,end,[0,255,0],2)
cv2.circle(img,far,5,[0,0,255],-1)
cv2.imshow('img',frame)
cv2.waitKey(0)
cv2.destroyAllWindows()