python - Python,OpenCv阵列错误

标签 python opencv

我正在尝试使用python和cv2在图像中看到运动的地方绘制轮廓。
我在这里按照教程进行操作:http://www.steinm.com/blog/motion-detection-webcam-python-opencv-differential-images/使运动部件正常工作,但是当尝试使用不同方法绘制轮廓时,我不断遇到不同的数组错误。

这是我的方法。我找到了差分图像,然后在该图像上找到了轮廓。然后我在差分图像上绘制轮廓,然后显示出来。

这是代码:

import cv2
import numpy as np

#Find the differential image
def diffImg(t0, t1,t2):
    d1 = cv2.absdiff(t2, t1)
    d2 = cv2.absdiff(t1, t0)
    d_final = cv2.bitwise_and(d1,d2)
    d_binary = cv2.threshold(d_final, 35, 255,cv2.THRESH_BINARY)[1]
    d_blur =  cv2.blur(d_binary, (15,15))
    return d_blur

#Capture Video from camera
cam = cv2.VideoCapture(0)
s, img = cam.read()

window_name = "Movement Visual"
cv2.namedWindow(window_name, cv2.CV_WINDOW_AUTOSIZE)

#Read the first three images to find the differential image
t_minus = cv2.cvtColor(cam.read()[1], cv2.COLOR_RGB2GRAY)
t = cv2.cvtColor(cam.read()[1], cv2.COLOR_RGB2GRAY)
t_plus = cv2.cvtColor(cam.read()[1], cv2.COLOR_RGB2GRAY)

while s:
    #draw contours
    contours, hierarchy = cv2.findContours(diffImg(t_minus,t,t_plus),cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
    final_contour = cv2.drawContours(img,contours,-1,(250,250,250),2)
    cv2.imshow(window_name,final_contour)
    t_minus = t
    t = t_plus
    t_plus = cv2.cvtColor(cam.read()[1],cv2.COLOR_RGB2GRAY)

    key = cv2.waitKey(10)
    if key == 27: 
        cv2.destroyWindow(window_name)
        break
print "Bye"

#cv2.imwrite("image.png", diffImg(t_minus,t,t_plus)

这是我得到的错误:
 line 28, in <module>
    cv2.imshow(window_name,final_contour)
error: C:\slave\WinInstallerMegaPack\src\opencv\modules\core\src\array.cpp:2482: error: (-206) Unrecognized or unsupported array type

第28行是在while循环中声明final_contour的位置。我不明白为什么要得到这个,因为看来我实际上只是在函数之间交换图像。

感谢您的任何建议。

最佳答案

好的,可以通过@Constantine的 Prop 来解决这个问题,以提醒我跟踪代码。当我打印出什么diffImg(t_minus,t,t_plus)contourhierarchy时,我发现它们分别是一个零数组,[]和None。因此,(至少以我的观看方式)没有图像可以绘制轮廓。因此错误。我更改了代码,以在直接从相机读取的彩色图像副本上绘制轮廓。因此,基本上,如果我在diffImg(t_minus,t,t_plus)上找到轮廓,然后在从摄像机输入的图像上绘制轮廓,并将其显示在新屏幕上。这是一段要澄清的代码:

while s:
    #draw contours
    contours, hierarchy = cv2.findContours(diffImg(t_minus,t,t_plus),cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
    cv2.drawContours(img,contours,-1,(250,250,250),2)
    cv2.imshow('Contours',img)
    cv2.imshow(window_name,diffImg(t_minus,t,t_plus))
    t_minus = t
    t = t_plus
    t_plus = cv2.cvtColor(cam.read()[1],cv2.COLOR_RGB2GRAY)
    ...

关于python - Python,OpenCv阵列错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23072378/

相关文章:

python - 使用列表值作为后续键访问字典值

python - 从Python的图片中获取所需的部分,然后找到该部分中的缺陷像素

opencv - 如何使用USB从多个DSLR相机捕获图像?

安卓 OpenCV : AddWeighted function implementation in C++

c++ - 计算点之间的角度

python - Django 查询查找具有大于零的特定值的行数,按用户分组

Python:将字符串中的多个日期格式更改为日期时间格式

Python 表单处理替代方案

python - 使用 python 和 lxml 从表中提取文本

c++ - 用于 SVM OpenCV 训练的交通标志图像集