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()