- Input Parameters:
- img: Input image (RGB or grayscale).
- k: Desired number of superpixels.
- m: Compactness factor (controls the balance between color similarity and spatial proximity).
- max_iter: Maximum number of iterations.
- Steps:
- Convert the image to LAB color space for better segmentation.
- Initialize cluster centers on a grid.
- Iteratively assign pixels to the nearest cluster center based on color and spatial distance.
- Update cluster centers after each iteration.
- Enforce connectivity of superpixels in the post-processing step.
- Output:
- labels: A matrix where each pixel is assigned a superpixel label.
- num_labels: The number of superpixels generated.