Skip to content

Instantly share code, notes, and snippets.

@hugs
Created August 13, 2013 22:39
Show Gist options
  • Save hugs/6226408 to your computer and use it in GitHub Desktop.
Save hugs/6226408 to your computer and use it in GitHub Desktop.
OpenCV to find the bird in Angry Birds
#!/opt/local/bin/python2.7
import cv
import subprocess
import time
# Template to find
template_file = cv.CaptureFromFile('redbird.png')
template = cv.QueryFrame(template_file)
window_name = "Skynet"
threshold = 5510000.0
cv.NamedWindow(window_name, cv.CV_WINDOW_AUTOSIZE)
while True:
# Get frame
result = subprocess.call(['./v2u', 'frame.bmp']
, stdout=subprocess.PIPE
, stderr=subprocess.PIPE)
# Convert frame to opencv format
frame_file = cv.CaptureFromFile('frame.bmp')
frame = cv.QueryFrame(frame_file)
cv.ShowImage(window_name, frame)
# Find bird
W,H=cv.GetSize(frame)
w,h=cv.GetSize(template)
width=W-w+1
height=H-h+1
result=cv.CreateImage((width,height),32,1)
cv.MatchTemplate(frame, template, result,cv.CV_TM_SQDIFF)
(minval,maxval,minloc,maxloc)=cv.MinMaxLoc(result,mask=None)
(x,y)=minloc
if minval < threshold:
#if 1:
print ("%s < %s :-)" % (minval, threshold))
cv.Rectangle(frame,
( int(x), int(y) ),
( int(x)+w,int(y)+h ),
(255,255,255),
3,
0)
else:
print ("%s > %s" % (minval, threshold))
# Show image
cv.ShowImage(window_name, frame)
if cv.WaitKey(10) == 27:
break
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment