Image segmentation of connected shapes problem

2 次查看(过去 30 天)
Hello, Below is the code I used to segment the image however when there are are connected shapes (shown in the img) the code take it as one object. I want it to take it as separate object and get the centroid of each. Your help will be very much appreciated. Thank you.
A = imread('image.png');
figure, imshow(A);
title('Original Image');
B = im2bw(A);
B = ~B;
B = 1-B;
B = (B == 0);
figure, imshow(B);
title('Image Without Holes');
C=imfill(B,'holes');
figure,imshow(C);
title('Image With Holes');
label=bwlabel(B);
max(max(label))
im1=(label==1);
for j=1:max(max(label))
[row, col] = find(label==j);
len=max(row)-min(row)+2;
breadth=max(col)-min(col)+2;
target=uint8(zeros([len breadth]));
sy=min(col)-1;
sx=min(row)-1;
for i=1:size(row,1)
x=row(i,1)-sx;
y=col(i,1)-sy;
target(x,y)=A(row(i,1),col(i,1));
end
mytitle=strcat('Object Number:',num2str(j));
figure,imshow(target);title(mytitle);
end
Image
Result: 2 Objects only should be 5 objects

采纳的回答

Image Analyst
Image Analyst 2015-8-30
Wrong approach. Simply use imclearborder():
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 20;
grayImage = imread('c2.png');
% Get the dimensions of the image.
% numberOfColorBands should be = 1.
[rows, columns, numberOfColorBands] = size(grayImage)
if numberOfColorBands > 1
% It's not really gray scale like we expected - it's color.
% Convert it to gray scale by taking only the green channel.
grayImage = grayImage(:, :, 2); % Take green channel.
end
% Display the original gray scale image.
subplot(2, 1, 1);
imshow(grayImage, []);
title('Original Grayscale Image', 'FontSize', fontSize, 'Interpreter', 'None');
% Set up figure properties:
% Enlarge figure to full screen.
set(gcf, 'Units', 'Normalized', 'OuterPosition', [0 0 1 1]);
% Get rid of tool bar and pulldown menus that are along top of figure.
set(gcf, 'Toolbar', 'none', 'Menu', 'none');
% Give a name to the title bar.
set(gcf, 'Name', 'Demo by ImageAnalyst', 'NumberTitle', 'Off')
% Binarize the image
binaryImage = grayImage > 128;
% Get rid of the background
binaryImage = imclearborder(binaryImage);
% Get rid of small triangle between the circles.
binaryImage = bwareaopen(binaryImage, 1000);
% Display the binary image.
subplot(2, 1, 2);
imshow(binaryImage, []);
title('Binary Image', 'FontSize', fontSize, 'Interpreter', 'None');
% Identify individual blobs by seeing which pixels are connected to each other.
% Each group of connected pixels will be given a label, a number, to identify it and distinguish it from the other blobs.
% Do connected components labeling with either bwlabel() or bwconncomp().
labeledImage = bwlabel(binaryImage, 8); % Label each blob so we can make measurements of it
% Get all the blob properties. Can only pass in originalImage in version R2008a and later.
blobMeasurements = regionprops(labeledImage, 'Centroid');
numberOfBlobs = size(blobMeasurements, 1);
hold on;
for k = 1 : length(blobMeasurements)
x = blobMeasurements(k).Centroid(1);
y = blobMeasurements(k).Centroid(2);
plot(x, y, 'r+', 'MarkerSize', 30, 'LineWidth', 3);
str = sprintf('The centroid of oval %d is at (%.2f, %.2f)', ...
k, x, y);
uiwait(helpdlg(str));
end
  3 个评论
Image Analyst
Image Analyst 2015-8-31
It's most likely because you don't have solid lines separating the states. You have breaks in them which makes two neighboring states connected and considered as one blob. It could also be that there's an 8-connected path from one to another. Try it using 4-connected labeling:
labeledImage = bwlabel(binaryImage, 4);
Phantom.i7
Phantom.i7 2015-9-5
hi again,
there's another problem which involves in irregular shape object like in image shown below. its centroid is out of the area.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Computer Vision with Simulink 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by