File size: 3,826 Bytes
f6b1b5d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import cv2
import tkinter as tk
from tkinter import filedialog

def save_image():
    filename = filedialog.asksaveasfilename(defaultextension='.jpg')
    if filename:
        cv2.imwrite(filename, frame)

def update_params():
    detector = cv2.SimpleBlobDetector_create(params)

def update_sliders():
    min_size_slider.set(params.minArea)
    max_size_slider.set(params.maxArea)
    threshold_slider.set(params.thresholdStep)

def on_value_changed(value):
    if value == "min_size":
        params.minArea = min_size_slider.get()
    elif value == "max_size":
        params.maxArea = max_size_slider.get()
    elif value == "threshold":
        params.thresholdStep = threshold_slider.get()

    update_params()

cap = cv2.VideoCapture(0)
cap.set(cv2.CAP_PROP_FRAME_WIDTH, 640)
cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 480)

params = cv2.SimpleBlobDetector_Params()
params.filterByArea = True
params.minArea = 1
params.maxArea = 5000
params.thresholdStep = 10
detector = cv2.SimpleBlobDetector_create(params)

root = tk.Tk()
root.title("Detector de microparticulas")

min_size_label = tk.Label(root, text="Tamaño minimo")
min_size_slider = tk.Scale(root, from_=0, to=1000, length=200, orient=tk.HORIZONTAL, label="Tamaño mínimo", command=lambda value: on_value_changed("min_size"))

max_size_label = tk.Label(root, text="Tamaño maximo")
max_size_slider = tk.Scale(root, from_=0, to=10000, length=200, orient=tk.HORIZONTAL, label="Tamaño máximo", command=lambda value: on_value_changed("max_size"))

threshold_label = tk.Label(root, text="Sensibilidad")
threshold_slider = tk.Scale(root, from_=0, to=255, length=200, orient=tk.HORIZONTAL, label="Sensibilidad", command=lambda value: on_value_changed("threshold"))

min_dist_label = tk.Label(root, text="Distancia minima entre blobs")
min_dist_slider = tk.Scale(root, from_=0, to=100, length=200, orient=tk.HORIZONTAL, label="Distancia minima", command=lambda value: on_value_changed("min_dist"))

circularity_label = tk.Label(root, text="Circulatidad")
circularity_slider = tk.Scale(root, from_=0, to=1, resolution=0.1, length=200, orient=tk.HORIZONTAL, label="Circulatidad", command=lambda value: on_value_changed("circularity"))

convexity_label = tk.Label(root, text="Convexidad")
convexity_slider = tk.Scale(root, from_=0, to=1, resolution=0.1, length=200, orient=tk.HORIZONTAL, label="Convexidad", command=lambda value: on_value_changed("convexity"))

inertia_label = tk.Label(root, text="Inercia")
inertia_slider = tk.Scale(root, from_=0, to=1, resolution=0.1, length=200, orient=tk.HORIZONTAL, label="Inercia", command=lambda value: on_value_changed("inertia"))

save_button = tk.Button(root, text="Guardar imagen", command=save_image)

min_size_label.pack()
min_size_slider.pack()
max_size_label.pack()
max_size_slider.pack()
threshold_label.pack()
threshold_slider.pack()
min_dist_label.pack()
min_dist_slider.pack()
circularity_label.pack()
circularity_slider.pack()

update_params()
update_sliders()

while True:
    ret, frame = cap.read()

    if not ret:
        continue

    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
    keypoints = detector.detect(gray)

    if keypoints:
        for kp in keypoints:
            x, y = kp.pt
            size = kp.size
            cv2.rectangle(frame, (int(x - size / 2), int(y - size / 2)), (int(x + size / 2), int(y + size / 2)), (0, 255, 0), 2)

        count = len(keypoints)
        cv2.putText(frame, "Contador: " + str(count), (10, 30), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 2)

    cv2.imshow("Detector de micropartículas", frame)

    key = cv2.waitKey(1)
    if key == ord('q'):
        break
    elif key == ord('+'):
        params.maxArea += 5000
        update_sliders()
    elif key == ord('-'):
        params.maxArea -= 5000
        update_sliders()

cap.release()
cv2.destroyAllWindows()