Also, I wrote in an AbsDiff function, but if I run it (without the attempted template matching code, it turns the image black and reflects the text onto the bottom half of the screen). If I try to run the code without the template matching code I get UnboundLocalError: local variable 'contour' referenced before assignment.
Are there cv (2.4) functions that can make this process easier? Could someone help me pinpoint where these errors are coming from? I looked at open cv 2.4 docs and played around with other image manipulation code but I started getting confused with all of the image bit parameter requirements.
import cv
import cv2
class Capture:
#similar to Java constructor
def __init__(self):
self.capture = cv.CaptureFromCAM(0)
cv.NamedWindow('Capture',1)
def run(self):
"""
#"Grab" your first frame
# cpic = the current frame that you are working with
"""
cpic = cv.QueryFrame(self.capture)
cpic_size = cv.GetSize(cpic)
track = True
#current color image
curr_image = cv.CreateImage(cv.GetSize(cpic), 8, 1)
motion_image = cv.CreateImage(cv.GetSize(cpic), cv.IPL_DEPTH_32F, 3)
grey_image = cv.CreateImage(cv.GetSize(cpic), cv.IPL_DEPTH_8U, 1)
storage = cv.CreateMemStorage(0)
while True:
curr_image = cv.QueryFrame(self.capture)
if track:
frame_diff = cv.CloneImage(curr_image)
temp = cv.CloneImage(curr_image)
cv.ConvertScale(curr_image, motion_image, 1.0, 0.0)
first = False
else:
cv.RunningAvg(curr_image, motion_image, 0.020, None)
# Convert the scale of the moving average.
cv.ConvertScale(frame_diff, temp, 1.0, 0.0)
# Minus the current frame from the moving average.
#cv.AbsDiff(curr_image, temp, frame_diff)
cv.CvtColor(frame_diff,grey_image,cv.CV_BGR2GRAY)
threshold = 100
cv.Threshold(grey_image,grey_image,threshold-5,110,cv.CV_THRESH_BINARY)
cv.Dilate(grey_image,grey_image,None,5)
cv.Erode(grey_image,grey_image,None,5)
cv.Smooth(grey_image,grey_image,cv.CV_GAUSSIAN,15,0)
image = LoadImage('C:\Users\___.jpeg')
template = grey_image
W,H = GetSize(image)
w,h = GetSize(grey_image)
width = W-w+1
height = H-h+1
result = CreateImage ((width,height),32,1)
MatchTemplate (image,template,result,CV_TM_CCORR)
(min_x,min_y,minloc,maxloc) = MinMaxLoc(result)
(x,y) = minloc
THRESHOLD = 10;
if minloc < THRESHOLD:
contour = cv.FindContours(grey_image, storage, cv.CV_RETR_CCOMP, cv.CV_CHAIN_APPROX_SIMPLE)
#cv.contourArea(contour)
cv.DrawContours(curr_image,contour,(0,255,0),(0,255,0),10,6,8,(0,0))
print result
#points = []
while contour:
cv.MeanShift(grey_image,curr_image,10)
#bound_rect = cv.BoundingRect(list(contour))
contour = contour.h_next()
scalarMult = 10
x = 100
y = 100
w = 100
h = 100
point1 = (x,y)
point2 = (x+w,x+y)
#Print the font on the screen
font = cv.InitFont(cv.CV_FONT_HERSHEY_SIMPLEX, 1, 1, 0, 3, 8)
cv.PutText(curr_image,"Motion:",(10,50),font,255)
#Show your resultant images
threshold = 100
cv.Threshold(motion_image,motion_image,threshold-5,110,cv.CV_THRESH_BINARY_INV)
cv.ShowImage ('CaptureLive',curr_image)
cv.ShowImage ('Capture Light',grey_image)
cv.ShowImage ('Capture Motion',motion_image)
c = cv.WaitKey(7)%0x100
if c == 27:
break
if __name__=="__main__":
t = Capture()
t.run()

New Topic/Question
Reply



MultiQuote



|