Reputation: 1329
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.
Upvotes: 0
Views: 988
Reputation: 1329
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)
...
Upvotes: 1