-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproc.py
More file actions
executable file
·99 lines (85 loc) · 2.85 KB
/
proc.py
File metadata and controls
executable file
·99 lines (85 loc) · 2.85 KB
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
#!/usr/bin/env python3
import cv2
import numpy as np
def detect_targets(cap):
#Bir kare al
ret, capture = cap.read()
# Gürültü kaldırma için gerekli kerneller
kernel = np.ones((9,9),np.uint8)
kernel2 = np.ones((13,13),np.uint8)
if ret:
# Beyazlık filtresi
gray = cv2.cvtColor(capture, cv2.COLOR_BGR2GRAY)
_, filter1 = cv2.threshold(gray,240,255,cv2.THRESH_BINARY)
# Arkaplandaki beyaz noktaları ve cisim üzerindeki siyah noktaları yok et
filter2 = cv2.morphologyEx(filter1, cv2.MORPH_OPEN, kernel)
filter3 = cv2.morphologyEx(filter2, cv2.MORPH_CLOSE, kernel2)
#Kontur bul
contours, _ = cv2.findContours(filter3,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
return capture, contours
else:
return None, None
def rectangle(img, contours):
try:
cnt1 = contours[0]
cnt2 = contours[1]
except IndexError:
return img
#Targetları dikdörtgen içine al
rect1 = cv2.minAreaRect(cnt1)
box_p1 = cv2.boxPoints(rect1)
box1 = np.int0(box_p1)
rect2 = cv2.minAreaRect(cnt2)
box_p2 = cv2.boxPoints(rect2)
box2 = np.int0(box_p2)
cv2.drawContours(img,[box1],0,(0,0,255),2)
cv2.drawContours(img,[box2],0,(0,0,255),2)
return img
def cnt_test(cnt):
rect = cv2.minAreaRect(cnt)
width = min(rect[1][0], rect[1][1])
height = max(rect[1][0], rect[1][1])
ratio = width/height
if cv2.contourArea(cnt) > 200 and ratio > 0.35 and ratio < 0.6:
return True
else:
return False
def maap(x, in_min, in_max, out_min, out_max):
return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min
def calculate_errors(contours):
try:
cnt1 = contours[0]
cnt2 = contours[1]
except IndexError:
return False, 0, 0
#Target etrafında dikdörtgensel bölge oluştur
rect1 = cv2.minAreaRect(cnt1)
box_p1 = cv2.boxPoints(rect1)
box1 = np.int0(box_p1)
rect2 = cv2.minAreaRect(cnt2)
box_p2 = cv2.boxPoints(rect2)
box2 = np.int0(box_p2)
#Targetların ağırlık merkezini bul
M1 = cv2.moments(box1)
M2 = cv2.moments(box2)
center1 = int(M1['m10']/M1['m00'])
center2 = int(M2['m10']/M2['m00'])
#TODO z ekseninde hata hesabı
z_error = 0
#Targetların ekran merkezine olan uzaklığı arasındaki fark -> Y eksenindeki hata
y_error = (240-c1) + (240-c2)
return True, z_error, y_error
if __name__ == '__main__':
cam = cv2.VideoCapture(0)
while cam.isOpened():
capture, contours= detect_targets(cam)
result = rectangle(capture, contours)
if capture is None:
print('Görüntü yok!')
else:
cv2.imshow('Image', capture)
cv2.imshow('Filter', result)
k = cv2.waitKey(5) & 0xFF
if k == 27:
break
cv2.destroyAllWindows()