-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRenk ile Nesne Tespiti.py
116 lines (63 loc) · 2.95 KB
/
Renk ile Nesne Tespiti.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
import cv2
import numpy as np
from collections import deque
# nesne merkezini depolayacak veri tipi
buffer_size = 16
pts = deque(maxlen = buffer_size)
# mavi renk aralığı HSV
blueLower = (84, 98, 0)
blueUpper = (179, 255, 255)
# capture
cap = cv2.VideoCapture(0)
cap.set(3,960)
cap.set(4,480)
while True:
success, imgOriginal = cap.read()
if success:
# blur
blurred = cv2.GaussianBlur(imgOriginal, (11,11), 0)
# hsv
hsv = cv2.cvtColor(blurred, cv2.COLOR_BGR2HSV)
cv2.imshow("HSV Image",hsv)
# mavi için maske oluştur
mask = cv2.inRange(hsv, blueLower, blueUpper)
cv2.imshow("mask Image",mask)
# maskenin etrafında kalan gürültüleri sil
mask = cv2.erode(mask, None, iterations = 2)
mask = cv2.dilate(mask, None, iterations = 2)
cv2.imshow("Mask + erozyon ve genisleme",mask)
# farklı sürüm için
# (_, contours,_) = cv2.findContours(mask.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
# kontur
(contours,_) = cv2.findContours(mask.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
center = None
# Mavi Renk Bulunca
if len(contours) > 0:
# en buyuk konturu al
c = max(contours, key = cv2.contourArea)
# dikdörtgene çevir
rect = cv2.minAreaRect(c)
((x,y), (width,height), rotation) = rect
s = "x: {}, y: {}, width: {}, height: {}, rotation: {}".format(np.round(x),np.round(y),np.round(width),np.round(height),np.round(rotation))
print(s)
# kutucuk
box = cv2.boxPoints(rect)
box = np.int64(box)
# moment
M = cv2.moments(c)
center = (int(M["m10"]/M["m00"]), int(M["m01"]/M["m00"]))
# konturu çizdir: sarı
cv2.drawContours(imgOriginal, [box], 0, (0,255,255),2)
# merkere bir tane nokta çizelim: pembe
cv2.circle(imgOriginal, center, 5, (255,0,255),-1)
# bilgileri ekrana yazdır
cv2.putText(imgOriginal, s, (25,50), cv2.FONT_HERSHEY_COMPLEX_SMALL, 1, (255,255,255), 2)
# deque
pts.appendleft(center)
for i in range(1, len(pts)):
if pts[i-1] is None or pts[i] is None: continue
cv2.line(imgOriginal, pts[i-1], pts[i],(0,255,0),3) #
cv2.imshow("Orijinal Tespit",imgOriginal)
if cv2.waitKey(1) & 0xFF == ord("q"):
cv2.destroyAllWindows()
break