-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathborder_detection_v2.py
More file actions
56 lines (42 loc) · 1.68 KB
/
border_detection_v2.py
File metadata and controls
56 lines (42 loc) · 1.68 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
import cv2
import numpy as np
# use
def detect_mask_border(mask, border_thickness=1):
"""
Detect borders in a binary segmentation mask.
Parameters:
mask: numpy.ndarray
Binary segmentation mask (2D array with 0s and 1s or 0s and 255s)
border_thickness: int
Thickness of the border to detect (default: 1)
Returns:
numpy.ndarray: Binary image with only the borders
"""
# Ensure mask is binary
if mask.max() > 1:
mask = mask / 255.0
# Convert to uint8
mask = (mask * 255).astype(np.uint8)
# Method 1: Using morphological operations
kernel = np.ones((3, 3), np.uint8)
erosion = cv2.erode(mask, kernel, iterations=border_thickness)
border = cv2.subtract(mask, erosion)
# Alternative Method 2: Using contours
# contours, _ = cv2.findContours(mask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
# border = np.zeros_like(mask)
# cv2.drawContours(border, contours, -1, (255,255,255), border_thickness)
return border
# Example usage
if __name__ == "__main__":
# Read the mask image
mask = cv2.imread('segmentation_v2_masks/ISIC_0000042_segmented.png', cv2.IMREAD_GRAYSCALE)
# Detect borders
border = detect_mask_border(mask, border_thickness=2)
# Save or display results
cv2.imwrite('mask_border.png', border)
# Optional: Overlay border on original image in color
colored_border = np.zeros((mask.shape[0], mask.shape[1], 3), dtype=np.uint8)
colored_border[border > 0] = [0, 0, 255] # Red border
# Blend with original image if needed
# original = cv2.imread('original_image.png')
# result = cv2.addWeighted(original, 1.0, colored_border, 0.5, 0)