-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlaser.py
125 lines (94 loc) · 3.19 KB
/
laser.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
import cv2
import numpy as np
import subprocess
subprocess.check_call("v4l2-ctl -d /dev/video0 -c auto_exposure=1",shell=True)
subprocess.check_call("v4l2-ctl -d /dev/video0 -c exposure_time_absolute=355",shell=True)
def nothing(x):
pass
cap = cv2.VideoCapture(0)
# Create a black image, a window
img = np.zeros((240,320,3), np.uint8)
res = np.zeros((240,320,3), np.uint8)
cv2.namedWindow('image')
# create trackbars for color change
cv2.createTrackbar('Hmin','image',0,255,nothing)
cv2.createTrackbar('Hmax','image',255,255,nothing)
cv2.createTrackbar('Smin','image',0,255,nothing)
cv2.createTrackbar('Smax','image',255,255,nothing)
cv2.createTrackbar('Vmin','image',30,255,nothing)
cv2.createTrackbar('Vmax','image',255,255,nothing)
# create switch for ON/OFF functionality
switch = '0 : OFF \n1 : ON'
cv2.createTrackbar(switch, 'image',0,1,nothing)
cap.set(cv2.CAP_PROP_FRAME_WIDTH,320)
cap.set(cv2.CAP_PROP_FRAME_HEIGHT,240)
cap.set(cv2.CAP_PROP_FPS,60)
while(1):
# get current positions of four trackbars
hmin = cv2.getTrackbarPos('Hmin','image')
hmax = cv2.getTrackbarPos('Hmax','image')
smin = cv2.getTrackbarPos('Smin','image')
smax = cv2.getTrackbarPos('Smax','image')
vmin = cv2.getTrackbarPos('Vmin','image')
vmax = cv2.getTrackbarPos('Vmax','image')
s = cv2.getTrackbarPos(switch,'image')
# Take each frame
_, frame = cap.read()
# Convert BGR to HSV
hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
# define range of blue color in HSV
lower_blue = np.array([hmin, smin, vmin])
upper_blue = np.array([hmax, smax, vmax])
# Threshold the HSV image to get only blue colors
mask = cv2.inRange(hsv, lower_blue, upper_blue)
# morph
if s == 1:
kernel = np.ones((5,5),np.uint8)
dilation = cv2.dilate(mask,kernel,iterations = 2)
kernel = np.ones((15,15),np.uint8)
opening = cv2.morphologyEx(dilation, cv2.MORPH_OPEN, kernel)
mask = cv2.morphologyEx(opening, cv2.MORPH_CLOSE, kernel)
# Bitwise-AND mask and original image
#res = cv2.bitwise_and(frame,frame, mask= mask)
#Contour -----------------
_, contours, _ = cv2.findContours(mask, cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)
if len(contours) > 0:
cnt = max(contours, key = cv2.contourArea)
#cnt = contours[0]
M = cv2.moments(cnt)
if M['m00'] ==0:
M['m00']=0.001
cx = int(M['m10']/M['m00'])
cy = int(M['m01']/M['m00'])
z= 28.99 - 0.1222*cx + 0.00199* (cx**2)
x,y,w,h = cv2.boundingRect(cnt)
h11 = 245.75 - 6.09*z + 0.05* z**2
objH = h / h11 * 11
print(cx,z, h , objH)
res = cv2.rectangle(frame,(x,y),(x+w,y+h),(0,255,0),2)
cv2.imshow('mask',mask)
cv2.imshow('res',res)
cv2.imshow('image',frame)
k = cv2.waitKey(1) & 0xFF
if k == 27:
break
cap.release()
cv2.destroyAllWindows()
'''
calib measurements:
height(cm) pixel(x direction)
30 69
35 93
40 112
45 125
50 138
height = 28.99 - 0.1222*x + 0.00199* x**2
-------
h,z calib for 11cm obj:
108 30
94 35
82 40
73 45
h = 245.75 - 6.09*z + 0.05* z**2
'''