Skip to content

Instantly share code, notes, and snippets.

@hoyeunglee
Created September 17, 2016 06:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hoyeunglee/9c97fdef1cc32fefde0a454a24a2c2c1 to your computer and use it in GitHub Desktop.
Save hoyeunglee/9c97fdef1cc32fefde0a454a24a2c2c1 to your computer and use it in GitHub Desktop.
import cv2
import numpy as np
import time
img_rgb = cv2.imread(r'C:\Users\tester\Documents\scree2.png')
img_gray = cv2.cvtColor(img_rgb, cv2.COLOR_BGR2GRAY)
template = cv2.imread(r'C:\Users\tester\Documents\dragob.png',0)
w, h = template.shape[::-1]
res = cv2.matchTemplate(img_gray,template,cv2.TM_CCOEFF_NORMED)
threshold = 0.64
loc = np.where( res >= threshold)
pt = [(0,0)]
while not zip(*loc[::-1]):
threshold = threshold - 0.02
loc = np.where( res >= threshold)
counter = 1
print("threshold="+str(threshold))
for pt2 in zip(*loc[::-1]):
cv2.rectangle(img_rgb, pt2, (pt2[0] + w, pt2[1] + h), (0,0,255), 2)
pt = pt2
crop_img = img_rgb[pt[1]:(pt[1]+h), pt[0]:(pt[0]+w)]
counter = counter + 1
cv2.imwrite("C:\\Users\\tester\\Documents\\res.png",crop_img)
img = cv2.imread(r'C:\Users\tester\Documents\res.png',1)
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
height, width = gray.shape
edges = cv2.Canny(gray,height,width,apertureSize = 3)
ret,thresh = cv2.threshold(edges,250,150,0)
contours,hierarchy = cv2.findContours(thresh, 1, 2)
im = img.copy()
cntcounter = 0
for cnt in contours:
cntcounter = cntcounter + 1
print("here1")
hull = cv2.convexHull(cnt,returnPoints = True)
print("here2")
while im is None:
time.sleep(1)
if im is not None:
print("here3")
previousx = 0
previousy = 0
for c in hull:
if previousx != 0 and previousy != 0 and c[0][0] != 0 and c[0][1] != 0 and abs(previousy - c[0][1]) > 10 and abs(c[0][0] - previousx) > 1:
while im is None:
time.sleep(1)
cv2.line(im,(previousx,previousy),(c[0][0],c[0][1]),(0,255,0),2)
print("")
previousx = c[0][0]
previousy = c[0][1]
cv2.imwrite(r'C:\Users\tester\Documents\masda.png',im)
img = cv2.imread(r'C:\Users\tester\Documents\masda.png',1)
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
height, width = gray.shape
edges = cv2.Canny(gray,height,width,apertureSize = 3)
ret,thresh = cv2.threshold(edges,250,150,0)
contours,hierarchy = cv2.findContours(thresh, 1, 2)
im = img.copy()
cntcounter = 0
for cnt in contours:
cntcounter = cntcounter + 1
print("here1")
hull = cv2.convexHull(cnt,returnPoints = True)
print("here2")
while im is None:
time.sleep(1)
if im is not None:
print("here3")
previousx = 0
previousy = 0
for c in hull:
if previousx != 0 and previousy != 0 and c[0][0] != 0 and c[0][1] != 0 and abs(previousy - c[0][1]) > 10 and abs(c[0][0] - previousx) > 1:
while im is None:
time.sleep(1)
cv2.line(im,(previousx,previousy),(c[0][0],c[0][1]),(0,255,0),2)
print("")
previousx = c[0][0]
previousy = c[0][1]
cv2.imwrite(r'C:\Users\tester\Documents\masda2.png',im)
img = cv2.imread(r'C:\Users\tester\Documents\masda2.png',1)
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
height, width = gray.shape
edges = cv2.Canny(gray,height,width,apertureSize = 3)
ret,thresh = cv2.threshold(edges,250,150,0)
contours,hierarchy = cv2.findContours(thresh, 1, 2)
im = img.copy()
cntcounter = 0
for cnt in contours:
cntcounter = cntcounter + 1
print("here1")
hull = cv2.convexHull(cnt,returnPoints = True)
print("here2")
while im is None:
time.sleep(1)
if im is not None:
print("here3")
previousx = 0
previousy = 0
for c in hull:
if previousx != 0 and previousy != 0 and c[0][0] != 0 and c[0][1] != 0 and abs(previousy - c[0][1]) > 10 and abs(c[0][0] - previousx) > 1:
while im is None:
time.sleep(1)
cv2.line(im,(previousx,previousy),(c[0][0],c[0][1]),(0,255,0),2)
print("")
previousx = c[0][0]
previousy = c[0][1]
cv2.imwrite(r'C:\Users\tester\Documents\masda3.png',im)
img = cv2.imread(r'C:\Users\tester\Documents\masda3.png',1)
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
height, width = gray.shape
edges = cv2.Canny(gray,height,width,apertureSize = 3)
ret,thresh = cv2.threshold(edges,250,150,0)
contours,hierarchy = cv2.findContours(thresh, 1, 2)
im = img.copy()
cntcounter = 0
for cnt in contours:
cntcounter = cntcounter + 1
print("here1")
hull = cv2.convexHull(cnt,returnPoints = True)
print("here2")
while im is None:
time.sleep(1)
if im is not None:
print("here3")
previousx = 0
previousy = 0
for c in hull:
if previousx != 0 and previousy != 0 and c[0][0] != 0 and c[0][1] != 0 and abs(previousy - c[0][1]) > 10 and abs(c[0][0] - previousx) > 1:
while im is None:
time.sleep(1)
cv2.line(im,(previousx,previousy),(c[0][0],c[0][1]),(0,255,0),2)
print("")
previousx = c[0][0]
previousy = c[0][1]
cv2.imwrite(r'C:\Users\tester\Documents\masda4.png',im)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment