Image Segmentation Using Split and Merge

14 views (last 30 days)
Victor Fletcher
Victor Fletcher on 9 Jan 2019
Commented: Akshat on 30 Jan 2025 at 5:18
I have a grayscale image that I would like to segment.
I would like to write a script and predicate that uses the split and merge method for this segmentation.
I would also like to be able to control the minimum block size.

Answers (1)

Akshat
Akshat on 29 Jan 2025 at 17:38
Edited: Akshat on 30 Jan 2025 at 5:17
As per the query, I think you just want to implement the split and merge method for the segmentation.
Please refer to the following code to get a rough idea of how I am thinking of implementing the recursive split.
function segmentedImg = splitAndMerge(img, minBlockSize, threshold)
img = im2double(img);
% Initialize the segmented image
segmentedImg = zeros(size(img));
% Start the recursive split and merge process
segmentedImg = recursiveSplit(img, segmentedImg, 1, 1, size(img, 1), size(img, 2), minBlockSize, threshold);
end
function segmentedImg = recursiveSplit(img, segmentedImg, x, y, width, height, minBlockSize, threshold)
% Extract the current block
block = img(x:x+width-1, y:y+height-1);
% Calculate the block's mean and standard deviation
blockMean = mean(block(:));
blockStd = std(block(:));
% If the block is uniform enough or smaller than the minimum size, fill it
if blockStd < threshold || width <= minBlockSize || height <= minBlockSize
segmentedImg(x:x+width-1, y:y+height-1) = blockMean;
return;
end
% Otherwise, split the block into four quadrants and process each recursively
halfWidth = floor(width / 2);
halfHeight = floor(height / 2);
% Top-left quadrant
segmentedImg = recursiveSplit(img, segmentedImg, x, y, halfWidth, halfHeight, minBlockSize, threshold);
% Top-right quadrant
segmentedImg = recursiveSplit(img, segmentedImg, x, y + halfHeight, halfWidth, height - halfHeight, minBlockSize, threshold);
% Bottom-left quadrant
segmentedImg = recursiveSplit(img, segmentedImg, x + halfWidth, y, width - halfWidth, halfHeight, minBlockSize, threshold);
% Bottom-right quadrant
segmentedImg = recursiveSplit(img, segmentedImg, x + halfWidth, y + halfHeight, width - halfWidth, height - halfHeight, minBlockSize, threshold);
end
An example driver code for the above functions can be:
img = imread('your_image.png');
minBlockSize = 8; % Define the minimum block size
threshold = 0.05;
segmentedImg = splitAndMerge(img, minBlockSize, threshold);
imshow(segmentedImg, []);
title('Segmented Image');
I hope the above written functions will be able to help you with the split and merge method of segmentation.
  2 Comments
Image Analyst
Image Analyst on 30 Jan 2025 at 0:34
image is a built-in function name, so you should not use it as the name of a variable.
Akshat
Akshat on 30 Jan 2025 at 5:18
Thanks for pointing that out @Image Analyst! I have updated the answer.

Sign in to comment.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!