-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwound
106 lines (86 loc) · 3.25 KB
/
wound
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
import cv2
import numpy as np
# Global variables
drawing = False
current_mode = cv2.GC_FGD # Initial drawing mode (Foreground)
radius = 5 # Drawing brush radius
last_point = (0, 0)
img = None # Original image
img_markup = None # Image with user drawings
mask = None # Segmentation mask
bgd_model = np.zeros((1, 65), np.float64)
fgd_model = np.zeros((1, 65), np.float64)
# Colors for drawing visualization (BGR format)
colors = {
cv2.GC_FGD: (0, 0, 255), # Red for foreground
cv2.GC_BGD: (255, 0, 0) # Blue for background
}
def mouse_callback(event, x, y, flags, param):
global drawing, last_point, img_markup, mask
if event == cv2.EVENT_LBUTTONDOWN:
drawing = True
cv2.circle(img_markup, (x, y), radius, colors[current_mode], -1)
cv2.circle(mask, (x, y), radius, current_mode, -1)
last_point = (x, y)
elif event == cv2.EVENT_MOUSEMOVE:
if drawing:
# Draw line between previous and current point
cv2.line(img_markup, last_point, (x, y), colors[current_mode], radius*2)
cv2.line(mask, last_point, (x, y), current_mode, radius*2)
last_point = (x, y)
elif event == cv2.EVENT_LBUTTONUP:
drawing = False
# Load image
img = cv2.imread('/Users/srini/JCR/Srini/Thozhil/Code copy/toes-0020.jpg')
if img is None:
print("Error: Image not found")
exit()
# Initialize working images
img_markup = img.copy()
h, w = img.shape[:2]
mask = np.full((h, w), cv2.GC_PR_BGD, dtype=np.uint8) # Initialize as probable background
# Create window and set mouse callback
cv2.namedWindow('Wound Segmentation')
cv2.setMouseCallback('Wound Segmentation', mouse_callback)
print("Usage:")
print("- Draw with left mouse button")
print("- 'f': Mark foreground (wound)")
print("- 'b': Mark background")
print("- 's': Perform segmentation")
print("- 'r': Reset")
print("- 'q': Quit")
while True:
cv2.imshow('Wound Segmentation', img_markup)
key = cv2.waitKey(1) & 0xFF
# Change drawing mode
if key == ord('f'):
current_mode = cv2.GC_FGD
print("Foreground marking mode")
elif key == ord('b'):
current_mode = cv2.GC_BGD
print("Background marking mode")
# Perform segmentation
elif key == ord('s'):
print("Performing segmentation...")
temp_mask = mask.copy()
# Apply GrabCut
cv2.grabCut(img, temp_mask, None, bgd_model, fgd_model,
iterCount=5, mode=cv2.GC_INIT_WITH_MASK)
# Create result mask
result_mask = np.where((temp_mask == cv2.GC_FGD) |
(temp_mask == cv2.GC_PR_FGD), 255, 0).astype('uint8')
# Apply mask to original image
segmented = cv2.bitwise_and(img, img, mask=result_mask)
# Show segmentation result
cv2.imshow('Segmentation Result', segmented)
# Reset
elif key == ord('r'):
img_markup = img.copy()
mask = np.full((h, w), cv2.GC_PR_BGD, dtype=np.uint8)
bgd_model = np.zeros((1, 65), np.float64)
fgd_model = np.zeros((1, 65), np.float64)
print("Reset complete")
# Exit
elif key == ord('q'):
break
cv2.destroyAllWindows()