Skip to content

Conversation

@DAKMAK2006
Copy link
Contributor

Fixed the preprocessing functions which assumed that the size of images to be 3D and gave errors when it was (H, W) or (H, W, 1)

Changes Made

1. Added Helper Functions for Format Handling

Three utility functions were introduced to detect and handle different image formats:

  • _is_grayscale(img): Detects if an image is grayscale

  • _ensure_3d(img): Normalizes input to 3D format for processing

  • _match_input_format(img, original_shape): Restores original format i.e. if input was 2D (H, W) it returns 2D and if it was 3D (H, W, 1) or (H, W, 3), returns 3D output

2. **Updated median_filter(), Gaussian_blur() and for grayscale(): **

Before:

def grayscale(img):
    # ❌ Would crash if already grayscale
    weights = np.array([0.2989, 0.5870, 0.1140])
    gray_img = np.dot(img, weights)
    return gray_img.astype(img.dtype)

After:

def grayscale(img):
    # Check if already grayscale using helper
    if _is_grayscale(img):
        if img.ndim == 3:  # (H, W, 1) → (H, W)
            return np.squeeze(img, axis=2)
        return img  # Already (H, W)
    
    # Convert RGB to grayscale
    if img.ndim == 3 and img.shape[2] == 3:
        weights = np.array([0.2989, 0.5870, 0.1140])
        gray_img = np.dot(img, weights)
        return gray_img.astype(img.dtype)
    
    # Handle RGBA and other multi-channel formats
    if img.ndim == 3 and img.shape[2] >= 3:
        weights = np.array([0.2989, 0.5870, 0.1140])
        gray_img = np.dot(img[:, :, :3], weights)
        return gray_img.astype(img.dtype)

5. resize() Already Compatible

The resize() function already worked for both formats due to using img.shape[:2] (which works for both 2D and 3D arrays) and NumPy's advanced indexing. No changes were needed.

Testing

Comprehensive tests were added to verify:

  • ✅ RGB images (H, W, 3) work correctly
  • ✅ Pure 2D grayscale (H, W) work correctly
  • ✅ 3D grayscale (H, W, 1) work correctly
  • ✅ Output format matches input format exactly
  • ✅ Edge cases (small images, large kernels)

- Added helper functions (_is_grayscale, _ensure_3d, _match_input_format)
- Updated median_filter and Gaussian_blur to handle grayscale images
- Updated grayscale detection and conversion logic
- Tested changes with both grayscale and RGB images to ensure functionality
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant