Fahimeh Orvati Nia commited on
Commit
8633967
·
1 Parent(s): a913fdb
sorghum_pipeline/data/mask_handler.py CHANGED
@@ -16,18 +16,20 @@ class MaskHandler:
16
  if mask is None:
17
  return image
18
 
19
- # Ensure mask is uint8
 
 
 
 
 
20
  mask = mask.astype(np.uint8)
21
 
22
- # Resize mask to match image if needed
23
- if mask.shape[:2] != image.shape[:2]:
24
- mask = cv2.resize(mask, (image.shape[1], image.shape[0]), interpolation=cv2.INTER_NEAREST)
25
 
26
- # Create binary mask
27
  binary = (mask > 0).astype(np.uint8) * 255
28
 
29
- # Ensure binary mask is single channel and matches image dimensions
30
- if binary.shape[:2] != image.shape[:2]:
31
- binary = cv2.resize(binary, (image.shape[1], image.shape[0]), interpolation=cv2.INTER_NEAREST)
32
-
33
  return cv2.bitwise_and(image, image, mask=binary)
 
16
  if mask is None:
17
  return image
18
 
19
+ # Get image dimensions
20
+ img_h, img_w = image.shape[:2]
21
+
22
+ # Ensure mask is 2D uint8
23
+ if mask.ndim > 2:
24
+ mask = mask[:, :, 0] # Take first channel if multi-channel
25
  mask = mask.astype(np.uint8)
26
 
27
+ # Resize mask to exactly match image dimensions
28
+ if mask.shape != (img_h, img_w):
29
+ mask = cv2.resize(mask, (img_w, img_h), interpolation=cv2.INTER_NEAREST)
30
 
31
+ # Create binary mask (must be exactly same H x W as image)
32
  binary = (mask > 0).astype(np.uint8) * 255
33
 
34
+ # Apply mask
 
 
 
35
  return cv2.bitwise_and(image, image, mask=binary)