-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbasic.py
89 lines (64 loc) · 1.85 KB
/
basic.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import socket #for sockets
import sys #for exit
import cv2
import numpy as np
import matplotlib.pyplot as plt
# if not cv2.useOptimized(): cv2.setUseOptimized(True)
# cap=cv2.VideoCapture(0)
# if not cap.isOpened(): cap.open()
# print cap.get(5),cap.get(3),cap.get(4)
green0 = np.uint8([[[135,170,105 ]]])
green1 = np.uint8([[[145,175,115 ]]])
hsv_green0 = cv2.cvtColor(green0,cv2.COLOR_BGR2HSV)
hsv_green1 = cv2.cvtColor(green1,cv2.COLOR_BGR2HSV)
print hsv_green0,hsv_green1
while(True):
ret,frame=cap.read()
img=cv2.imread(frame,1)
cv2.imwrite('test.jpg',img)
img=frame[10,630]
hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
print hsv
img=frame[120:360,170:510,0]
ret,thresh = cv2.threshold(img,50,255,0)
cv2.imshow('frame2',thresh)
#cv2.imshow('frame3',thresh)
contours,hierarchy = cv2.findContours(thresh, 1, 2)
cnt0 = contours[4]
cv2.drawContours(thresh, contours, 0, (0,255,0), 3)
cnt = contours[0]
M = cv2.moments(cnt)
print M
cx = int(M['m10']/M['m00'])
cy = int(M['m01']/M['m00'])
print cx,cy
fgmask = fgbg.apply(frame)
img=cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY)
ret,th1 = cv2.threshold(img,80,255,cv2.THRESH_BINARY_INV)
r,co=th1.shape
r1,c1=img.shape
x=0
y=0
c=0
th1 = cv2.adaptiveThreshold(img,255,cv2.ADAPTIVE_THRESH_GAUSSIAN_C,cv2.THRESH_BINARY,11,2)
for a in range(r): #iteration over all the pixel values
for b in range(co):
val=th1[r-1][co-1]
c=c+val
x=x+ ((b+1)*val)
y=y+ ((a+1)*val)
# ======================================================================================
x=x/c
y=y/c
X=700+(x) #transformation from the image space to the world space
Y=700+(y)
print X,Y
gray = cv2.flip(gray,1)
plt.imshow(frame)
plt.show()
all the image processing comes here...it acts on a specific frame
cv2.imshow('frame',thresh)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()