how to find the end point coordinates of an object in binary image.?
23 views (last 30 days)
Show older comments
Dear ,
Please anyone can help me out to find the end points coordinates in the binary image using matlab..?
Red points marked are the end points of the objects in binary image...
is it possiblt to find the coodrinates of the end point using matlab..?
Thanks in advance
0 Comments
Accepted Answer
Walter Roberson
on 7 Apr 2021
The requirements are not really well defined.
im = imread('https://www.mathworks.com/matlabcentral/answers/uploaded_files/574367/image.png');
bw = im2bw(im);
info = regionprops(bw, 'Centroid', 'Extrema', 'Orientation');
image(im); colormap(gray(256));
hold on
for K = 1 : 3
ti = info(K);
ori = ti.Orientation;
isflat = abs(ori) <= 45;
if isflat
outliers(:,:,K) = ti.Extrema([4 8],:);
else
outliers(:,:,K) = ti.Extrema([1 5], :);
end
text(ti.Centroid(1), ti.Centroid(2), string(K), 'color', 'r');
scatter(outliers(:,1,K), outliers(:,2,K), 'b*');
end
outliers
More Answers (3)
Image Analyst
on 7 Apr 2021
There is an easy way. Just use bwferet():
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;
%--------------------------------------------------------------------------------------------------------
% READ IN IMAGE
folder = pwd;
baseFileName = 'image.png';
grayImage = imread(baseFileName);
% Get the dimensions of the image.
% numberOfColorChannels should be = 1 for a gray scale image, and 3 for an RGB color image.
[rows, columns, numberOfColorChannels] = size(grayImage)
if numberOfColorChannels > 1
% It's not really gray scale like we expected - it's color.
% Use weighted sum of ALL channels to create a gray scale image.
grayImage = min(grayImage, [], 3);
end
%--------------------------------------------------------------------------------------------------------
% Display the image.
imshow(grayImage, []);
axis('on', 'image');
title('Gray Scale Image', 'FontSize', fontSize, 'Interpreter', 'None');
impixelinfo;
hFig = gcf;
hFig.WindowState = 'maximized'; % May not work in earlier versions of MATLAB.
drawnow;
% Turn into a binary image
mask = grayImage > 128;
% Get the Feret diameter info.
feretProperties = bwferet(mask)
% Plot Feret Diameter over image.
hold on;
for k = 1 : height(feretProperties)
maxCoordinates = feretProperties.MaxCoordinates{k};
x = maxCoordinates(:, 1);
y = maxCoordinates(:, 2);
plot(x, y, 'r.-', 'LineWidth', 2, 'MarkerSize', 30);
end
7 Comments
Walter Roberson
on 8 Apr 2021
regionprops fits an ellipse to the points, and the Orientation it returns is the angle of the major axes of the ellipse.
KSSV
on 6 Apr 2021
I = imread('image.png') ;
I1 = rgb2gray(I) ;
[y,x] = find(I1) ;
[idx,C] = kmeans([x y],3) ;
imshow(I)
hold on
plot(x(idx==1),y(idx==1),'r.')
plot(x(idx==2),y(idx==2),'b.')
plot(x(idx==3),y(idx==3),'g.')
plot(C(:,1),C(:,2),'*k')
7 Comments
KSSV
on 6 Apr 2021
I = imread('image.png') ;
I1 = rgb2gray(I) ;
[y,x] = find(I1) ;
[idx,C] = kmeans([x y],3) ;
bd = cell(3,1) ;
for i = 1:3
xi = x(idx==i) ; yi = y(idx==i) ;
id = boundary(xi,yi) ;
bd{i} = [xi(id) yi(id)] ;
end
% imshow(I)
hold on
plot(bd{1}(:,1),bd{1}(:,2),'r')
plot(bd{2}(:,1),bd{2}(:,2),'b')
plot(bd{3}(:,1),bd{3}(:,2),'g')
plot(C(:,1),C(:,2),'*k')
Image Analyst
on 6 Apr 2021
What I would do is to use regionprops() to find the centroid and orientation. Then also call bwboundaries() to get the x,y coordinates of the boundaries. Then create a line through the centroid at that angle with polyfit(). Then compare the line coordinates to the boundary coordinates and find out which coordinates of the line are closest to the boundary. And make sure they are far apart, like farther than half the major axis length over two, otherwise you've found two points on the same end of the blob (which is possible). Here's a start
props = regionprops(mask, 'Orientation', 'MajorAxisLength', 'Centroid');
boundaries = bwboundaries(mask);
[rows, columns] = size(mask)
for k = 1 : numel(props)
xy = props(k).Centroid;
xc = xy(1);
yc = xy(2);
thisBoundary = boundaries{k};
xb = thisBoundary(:, 2);
yb = thisBoundary(:, 1);
slope = atan(props(k).Orientation);
% Orientation line through centroid y = slope * (x - xc) + yc with x =
% 1 : columns
end
Sorry, that's all I have time for now but I'm certain you can figure out the rest easily.
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!