math.pi -> np.pi
squares.py sample added
This commit is contained in:
parent
570041fed5
commit
ae5dd1d748
@ -1,5 +1,4 @@
|
||||
import numpy as np
|
||||
import math
|
||||
from numpy import random
|
||||
import cv2
|
||||
|
||||
@ -20,7 +19,7 @@ def make_gaussians(cluster_n, img_size):
|
||||
def draw_gaussain(img, mean, cov, color):
|
||||
x, y = np.int32(mean)
|
||||
w, u, vt = cv2.SVDecomp(cov)
|
||||
ang = np.arctan2(u[1, 0], u[0, 0])*(180/math.pi)
|
||||
ang = np.arctan2(u[1, 0], u[0, 0])*(180/np.pi)
|
||||
s1, s2 = np.sqrt(w)*3.0
|
||||
cv2.ellipse(img, (x, y), (s1, s2), ang, 0, 360, color, 1, cv2.CV_AA)
|
||||
|
||||
|
@ -13,9 +13,6 @@ Keys:
|
||||
SPACE - reset features
|
||||
'''
|
||||
|
||||
|
||||
|
||||
|
||||
lk_params = dict( winSize = (21, 21),
|
||||
maxLevel = 2,
|
||||
criteria = (cv2.TERM_CRITERIA_EPS | cv2.TERM_CRITERIA_COUNT, 10, 0.03),
|
||||
@ -35,7 +32,7 @@ def calc_flow_old(img0, img1, p0):
|
||||
np.asarray(img1_cv)[:] = img1
|
||||
t = clock()
|
||||
features, status, error = cv.CalcOpticalFlowPyrLK(img0_cv, img1_cv, None, None, p0,
|
||||
lk_params['winSize'], lk_params['maxLevel'], (cv.CV_TERMCRIT_EPS | cv.CV_TERMCRIT_ITER, 10, 0.03), 0, p0)
|
||||
lk_params['winSize'], lk_params['maxLevel'], lk_params['criteria'], 0, p0)
|
||||
return np.float32(features), status, error, clock()-t
|
||||
|
||||
def main():
|
||||
@ -60,7 +57,7 @@ def main():
|
||||
p1, st, err, dt = calc_flow_old(img0, img1, p0)
|
||||
else:
|
||||
t = clock()
|
||||
p1, st, err = cv2.calcOpticalFlowPyrLK(img0, img1, p0, **lk_params)
|
||||
p1, st, err = cv2.calcOpticalFlowPyrLK(img0, img1, p0, None, **lk_params)
|
||||
dt = clock()-t
|
||||
for tr, (x, y) in zip(tracks, p1.reshape(-1, 2)):
|
||||
tr.append((x, y))
|
||||
|
@ -1,5 +1,4 @@
|
||||
import numpy as np
|
||||
import math
|
||||
import cv2
|
||||
import cv2.cv as cv
|
||||
import video
|
||||
@ -31,7 +30,7 @@ def draw_hsv(flow):
|
||||
ang = np.arctan2(fy, fx) + np.pi
|
||||
v = np.sqrt(fx*fx+fy*fy)
|
||||
hsv = np.zeros((h, w, 3), np.uint8)
|
||||
hsv[...,0] = ang*(180/math.pi/2)
|
||||
hsv[...,0] = ang*(180/np.pi/2)
|
||||
hsv[...,1] = 255
|
||||
hsv[...,2] = np.minimum(v*4, 255)
|
||||
bgr = cv2.cvtColor(hsv, cv.CV_HSV2BGR)
|
||||
@ -61,7 +60,7 @@ if __name__ == '__main__':
|
||||
while True:
|
||||
ret, img = cam.read()
|
||||
gray = cv2.cvtColor(img, cv.CV_BGR2GRAY)
|
||||
flow = cv2.calcOpticalFlowFarneback(prevgray, gray, 0.5, 3, 15, 3, 5, 1.2, 0)
|
||||
flow = cv2.calcOpticalFlowFarneback(prevgray, gray, None, 0.5, 3, 15, 3, 5, 1.2, 0)
|
||||
prevgray = gray
|
||||
|
||||
cv2.imshow('flow', draw_flow(gray, flow))
|
||||
|
39
samples/python2/squares.py
Normal file
39
samples/python2/squares.py
Normal file
@ -0,0 +1,39 @@
|
||||
import numpy as np
|
||||
import cv2
|
||||
|
||||
|
||||
def angle_cos(p0, p1, p2):
|
||||
d1, d2 = p0-p1, p2-p1
|
||||
return abs( np.dot(d1, d2) / np.sqrt( np.dot(d1, d1)*np.dot(d2, d2) ) )
|
||||
|
||||
def find_squares(img):
|
||||
img = cv2.GaussianBlur(img, (5, 5), 0)
|
||||
squares = []
|
||||
for gray in cv2.split(img):
|
||||
for thrs in xrange(0, 255, 26):
|
||||
if thrs == 0:
|
||||
bin = cv2.Canny(gray, 0, 50, apertureSize=5)
|
||||
bin = cv2.dilate(bin, None)
|
||||
else:
|
||||
retval, bin = cv2.threshold(gray, thrs, 255, cv2.THRESH_BINARY)
|
||||
contours, hierarchy = cv2.findContours(bin, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)
|
||||
for cnt in contours:
|
||||
cnt_len = cv2.arcLength(cnt, True)
|
||||
cnt = cv2.approxPolyDP(cnt, 0.02*cnt_len, True)
|
||||
if len(cnt) == 4 and cv2.contourArea(cnt) > 1000 and cv2.isContourConvex(cnt):
|
||||
cnt = cnt.reshape(-1, 2)
|
||||
max_cos = np.max([angle_cos( cnt[i], cnt[(i+1) % 4], cnt[(i+2) % 4] ) for i in xrange(4)])
|
||||
if max_cos < 0.3:
|
||||
squares.append(cnt)
|
||||
return squares
|
||||
|
||||
if __name__ == '__main__':
|
||||
from glob import glob
|
||||
for fn in glob('../cpp/pic*.png'):
|
||||
img = cv2.imread(fn)
|
||||
squares = find_squares(img)
|
||||
cv2.drawContours( img, squares, -1, (0, 255, 0), 3 )
|
||||
cv2.imshow('squares', img)
|
||||
ch = cv2.waitKey()
|
||||
if ch == 27:
|
||||
break
|
@ -52,7 +52,7 @@ class App:
|
||||
print 'auto_update if', ['off', 'on'][self.auto_update]
|
||||
if ch in [ord('r'), ord('R')]:
|
||||
self.markers[:] = 0
|
||||
self.markers_vis[:] = self.img.copy()
|
||||
self.markers_vis[:] = self.img
|
||||
self.sketch.show()
|
||||
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user