-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
92 lines (68 loc) · 2.47 KB
/
Copy pathmain.py
File metadata and controls
92 lines (68 loc) · 2.47 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
import threading
import cv2
import numpy as np
import time
start = time.perf_counter()
class ImageProcessing:
"""
Görüntü işlemede kullanılan morfolojik işlemler (erozyon,
genişleme ve tophat) uygulandı ve resim üzerindeki arabalar
tespit edildi.
"""
def __init__(self):
"""
Üzerinde işlem yapacağımız resim dosyası tanımlandı ve thread işlemi
start metoduyla başladı. Daha sonra join metoduyla threadlerin bitmesi beklendi.
"""
self.image_path = "resim.jpeg"
self.img = cv2.imread(self.image_path, 0)
thread1 = threading.Thread(target=self.apply_morphology, args=())
thread1.start()
thread1.join()
thread2 = threading.Thread(target=self.carTracking, args=())
thread2.start()
thread2.join()
def apply_morphology(self):
"""
Resim üzerinde erozyon, genişleme ve tophat işlemi
gerçekleştirildi.
"""
kernel = np.ones((10, 10), np.uint8)
self.dilation = cv2.dilate(self.img, kernel, iterations=1)
self.erosion = cv2.erode(self.img, kernel, iterations=2)
self.tophat = cv2.morphologyEx(self.img, cv2.MORPH_TOPHAT, kernel)
cv2.imshow("img_dilation", self.dilation)
cv2.waitKey(2)
time.sleep(2)
cv2.imshow("img_erosion", self.erosion)
cv2.waitKey(2)
time.sleep(2)
cv2.imshow("img_tophat", self.tophat)
cv2.waitKey(2)
time.sleep(2)
print("Process continues. . .")
cv2.waitKey(0)
cv2.destroyAllWindows()
def carTracking(self):
"""
Resim dosyasındaki arabalar tespit edildi.
"""
self.img = cv2.imread(self.image_path)
car_classifier = cv2.CascadeClassifier("cars.xml")
gray_image = cv2.cvtColor(self.img, cv2.COLOR_BGR2GRAY)
cars = car_classifier.detectMultiScale(gray_image, scaleFactor=1.1, minNeighbors=5)
while True:
for (x, y, w, h) in cars:
cv2.rectangle(self.img, (x, y), (x + w, y + h), (0, 255, 0), 2)
cv2.imshow("Car Tracking", self.img)
cv2.waitKey(2)
time.sleep(2)
if cv2.waitKey(0) & 0xFF == ord("q"):
break
cv2.destroyAllWindows()
print("Process completed for main!")
image_processor = ImageProcessing()
cv2.waitKey(0)
cv2.destroyAllWindows()
finish = time.perf_counter()
print(f"Finished in {round(finish - start, 2)} seconds.")