-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbubbleDetection.py
More file actions
82 lines (63 loc) · 1.71 KB
/
Copy pathbubbleDetection.py
File metadata and controls
82 lines (63 loc) · 1.71 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
import cv2
import numpy as np
import csv
# Load video
cap = cv2.VideoCapture("video.mp4")
if not cap.isOpened():
print("Error: Could not open video.")
exit()
# Video properties
width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
fps = cap.get(cv2.CAP_PROP_FPS)
if fps == 0:
fps = 30
# Video writer
fourcc = cv2.VideoWriter_fourcc(*"mp4v")
out = cv2.VideoWriter("output.mp4", fourcc, fps, (width, height))
# CSV file
csv_file = open("bubbles.csv", mode="w", newline="")
csv_writer = csv.writer(csv_file)
# CSV header
csv_writer.writerow(["frame", "bubble_id", "x", "y", "radius"])
frame_idx = 0
while True:
ret, frame = cap.read()
if not ret:
break
# Grayscale + blur
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
blurred = cv2.GaussianBlur(gray, (9, 9), 1.5)
# Detect bubbles
circles = cv2.HoughCircles(
blurred,
cv2.HOUGH_GRADIENT,
dp=1.2,
minDist=30,
param1=100,
param2=30,
minRadius=5,
maxRadius=20
)
bubble_id = 0
if circles is not None:
circles = np.uint16(np.around(circles))
for (x, y, r) in circles[0, :]:
# Draw bubble
cv2.circle(frame, (x, y), 50, (0, 255, 0), 2)
cv2.circle(frame, (x, y), 2, (0, 0, 255), 3)
# Write to CSV
csv_writer.writerow([frame_idx, bubble_id, x, y, r])
bubble_id += 1
# Write video frame
out.write(frame)
# Optional preview
cv2.imshow("Bubble Detection", frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
frame_idx += 1
# Cleanup
cap.release()
out.release()
csv_file.close()
cv2.destroyAllWindows()