-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
40 lines (30 loc) · 1.17 KB
/
utils.py
File metadata and controls
40 lines (30 loc) · 1.17 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
### utils.py
import cv2
import numpy as np
import os
import scipy.io
def load_image_and_mask(image_path, mask_path, target_size=(128, 128)):
# Load image
img = cv2.imread(image_path, cv2.IMREAD_GRAYSCALE)
img = cv2.resize(img, target_size)
img = img / 255.0
img = img[..., np.newaxis]
# Load .mat edge mask (BSDS format)
mat = scipy.io.loadmat(mask_path)
# Each 'groundTruth' entry is a dict with 'Boundaries' and 'Segmentation'
mask = mat['groundTruth'][0][0][0][0][1] # Get 'Boundaries' field from the first annotation
# Resize and normalize
mask = cv2.resize(mask.astype(np.float32), target_size)
mask = mask[..., np.newaxis]
print(type(mat['groundTruth'][0][0][0])) # Should be a numpy.void
print(mat['groundTruth'][0][0][0][0].dtype.names) # Should show ('Segmentation', 'Boundaries')
return img, mask
return img, mask
def preprocess_image(path, target_size=(128, 128)):
img = cv2.imread(path, cv2.IMREAD_GRAYSCALE)
if img is None:
raise FileNotFoundError(f"Could not read image at: {path}")
img = cv2.resize(img, target_size)
img = img / 255.0
img = img[..., np.newaxis]
return img