Segmenting boxes in an image
Mostrar comentarios más antiguos
I'm here to get an advice from all on how to segment boxes in an image.
I have tried using threshold, contour, and watershed algorithm but the result only segments the whole image.
Here is the image that I want to segment. The first box represents a symbol, second box represents a digit, and the third box represents a letter. Why do I need segmentation? I need this segmentation in order to split the boxes according to their representative.
Can anyone advise me?

This is my code
img_o = "/MATLAB Drive/2.png";
% Image loading
img = imread(img_o);
% Create a figure with subplots
figure('units', 'normalized', 'outerposition', [0 0 1 1]);
% Show original image
subplot(2, 3, 1);
imshow(img);
axis off;
title('Original Image');
% Image grayscale conversion
gray = rgb2gray(img);
% Adaptive thresholding
threshold = adaptthresh(gray, 'NeighborhoodSize', 11, 'Statistic', 'Gaussian', 'ForegroundPolarity', 'dark');
binary_img = imbinarize(gray, threshold);
% Show thresholded image
subplot(2, 3, 2);
imshow(binary_img);
axis off;
title('Thresholded Image');
% Perform morphological operations to remove noise
se = strel('square', 3);
opening = imopen(binary_img, se);
% Show opened image
subplot(2, 3, 3);
imshow(opening);
axis off;
title('Opened Image');
% Perform distance transform
dist_transform = bwdist(~opening);
sure_fg = imregionalmax(dist_transform);
% Show sure foreground image
subplot(2, 3, 4);
imshow(sure_fg);
axis off;
title('Sure Foreground Image');
% Perform watershed algorithm
markers = watershed(-dist_transform);
img_copy = img;
img_copy(markers == 0) = 255;
% Show the segmented image
subplot(2, 3, 5);
imshow(img_copy);
axis off;
title('Segmented Image');
% Print the number of segmented objects
num_objects = max(markers(:)) - 1;
subplot(2, 3, 6);
text(0.5, 0.5, ['Number of Segmented Objects: ', num2str(num_objects)], 'HorizontalAlignment', 'center', 'VerticalAlignment', 'middle', 'FontSize', 12);
axis off;
sgtitle('Image Segmentation');
Respuesta aceptada
Más respuestas (1)
Image Analyst
el 20 de Jun. de 2023
Editada: Image Analyst
el 20 de Jun. de 2023
You can get all three square masks doing this:
mask = grayImage > 128; % Or whatever works.
% Get rid of white surround touching the border.
mask = imclearborder(mask);
% Fill holes.
mask = imfill(mask, 'holes');
% Take the 3 largest blobs.
mask = bwareafilt(mask, 3);
%--------------------------------------------------------------------------------------------------------
% Get the 3 individual masks and display them.
labeledImage = bwlabel(mask); % Give an ID label to each square.
for k = 1 : 3
% Get mask 1
separateMasks{k} = ismember(labeledImage, k);
subplot(2, 3, 3+k);
imshow(separateMasks{k});
impixelinfo;
axis('on', 'image');
caption = sprintf('Mask #%d', k);
title(caption, 'FontSize', fontSize, 'Interpreter', 'None');
end
Full code is in the attached m-file,

1 comentario
Muhammad Syukri
el 4 de Jul. de 2023
Categorías
Más información sobre Image Segmentation en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
