how to find the end point coordinates of an object in binary image.?

25 ビュー (過去 30 日間)
Emma Murphy
Emma Murphy 2021 年 4 月 6 日
編集済み: Emma Murphy 2021 年 4 月 8 日
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

採用された回答

Walter Roberson
Walter Roberson 2021 年 4 月 7 日
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
outliers =
outliers(:,:,1) = 116.5000 240.5000 168.5000 390.5000 outliers(:,:,2) = 185.5000 410.5000 254.5000 580.5000 outliers(:,:,3) = 438.5000 337.5000 243.5000 352.5000
  1 件のコメント
Emma Murphy
Emma Murphy 2021 年 4 月 7 日
Thank you much sir ...its means me a lot ...
thanks for the reply and the solution....
appreciate you...!

サインインしてコメントする。

その他の回答 (3 件)

Image Analyst
Image Analyst 2021 年 4 月 7 日
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 件のコメント
Walter Roberson
Walter Roberson 2021 年 4 月 8 日
regionprops fits an ellipse to the points, and the Orientation it returns is the angle of the major axes of the ellipse.
Emma Murphy
Emma Murphy 2021 年 4 月 8 日
編集済み: Emma Murphy 2021 年 4 月 8 日
Thanks for the information..!!!

サインインしてコメントする。


KSSV
KSSV 2021 年 4 月 6 日
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 件のコメント
KSSV
KSSV 2021 年 4 月 6 日
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')
Emma Murphy
Emma Murphy 2021 年 4 月 6 日
Thanks for the solution but it give the end point of all the boundary i only want the end points of the marked red points coordinates...?
i want the coodrinate of only the red marked end points not of all the boundary points...
thanks for reply

サインインしてコメントする。


Image Analyst
Image Analyst 2021 年 4 月 6 日
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.
  1 件のコメント
Emma Murphy
Emma Murphy 2021 年 4 月 6 日
Thanks for reply ....getting the boundary coordinates but i want the end points of the object,....is it possible to get...?
but not getting exact end points can you tell if any minor changes can me done..?
i tried this....
labeledImage = bwlabel(binaryImage);
% Get the orientation
measurements = regionprops(labeledImage, 'Orientation', 'MajorAxisLength', 'Centroid');
allAngles = -[measurements.Orientation]
hold on;
for k = 1 : length(measurements)
xCenter = measurements(k).Centroid(1);
yCenter = measurements(k).Centroid(2);
% Determine endpoints
axisRadius = measurements(k).MajorAxisLength / 2;
x1 = xCenter + axisRadius * cosd(allAngles(k));
x2 = xCenter - axisRadius * cosd(allAngles(k));
y1 = yCenter + axisRadius * sind(allAngles(k));
y2 = yCenter - axisRadius * sind(allAngles(k));
fprintf('x1 = %.2f, y1 = %.2f, x2 = %.2f, y2 = %.2f\n\n', x1, y1, x2, y2);
plot([x1, x2], [y1, y2], 'r-', 'LineWidth', 2);
end

サインインしてコメントする。

Community Treasure Hunt

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

Start Hunting!

Translated by