Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I am trying to draw contours where I see motion in an image using python and cv2. I followed the tutorial here: http://www.steinm.com/blog/motion-detection-webcam-python-opencv-differential-images/ to get the motion part working, but I keep running into different array errors when I try different approaches to getting the contours drawn.

Here is my approach. I find the differential image and then find the contours on that image. Then I draw the contours on the differential image and then show it.

Here is the code:

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)

Here is the error I am getting:

 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

Line 28 is where final_contour is declared in the while loop. I don't understand why I am getting this since it seems like I am essentially just swapping images between functions.

Thanks for any advice.

share|improve this question
1  
Did you trace your code!? What was the type of final_contour? –  Constantine Apr 15 at 11:17
1  
Yeah. I can print out the sizes. I believe contours is coming out as a list, but it fails before I see what final_contour is. –  LearnLanguages96 Apr 15 at 15:34

1 Answer 1

up vote 1 down vote accepted

Okay so, figured it out with props to @Constantine for reminding me to trace my code. When I printed out what diffImg(t_minus,t,t_plus)and contour and hierarchy, I found that they were an array of zeros, [] and None respectively. So, there was (atleast the way I see it) no image to draw contours on. Thus the error. I changed the code to draw contours on a copy of a color image being read in directly from the camera. So, basically, if I find contours on the diffImg(t_minus,t,t_plus) and then I draw the contours on the image feed from the camera and show it on a new screen. Here is a section of the code to clarify:

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)
    ...
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.