How to draw vertical line and a horizontal line passing from the centroid of a region?

How to draw vertical line and a horizontal line passing from the centroid of a region?The horizontal and vertical line should be inside the region only.I also want to find the length of the lines.Please help me.

2 件のコメント

Andrew Newell
Andrew Newell 2014 年 2 月 22 日
What information do you have? A set of points or an equation?
Naishil shah
Naishil shah 2014 年 2 月 23 日
Actually, I have a region obtained using regionprops method.I get the centroid of this region using the property 'Centroid' of regionprops method.Can you please help me using this method to draw vertical line and a horizontal line passing from the centroid of a region? Thanks in advance.

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

 採用された回答

Image Analyst
Image Analyst 2014 年 2 月 23 日
編集済み: Image Analyst 2014 年 2 月 23 日
Naishil: Try this. Replace peaks() with imread() of your own image so that you are not using the peaks demo image and are using your own image.
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
imtool close all; % Close all imtool figures if you have the Image Processing Toolbox.
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 17;
% Check that user has the Image Processing Toolbox installed.
hasIPT = license('test', 'image_toolbox');
if ~hasIPT
% User does not have the toolbox installed.
message = sprintf('Sorry, but you do not seem to have the Image Processing Toolbox.\nDo you want to try to continue anyway?');
reply = questdlg(message, 'Toolbox missing', 'Yes', 'No', 'Yes');
if strcmpi(reply, 'No')
% User said No, so exit.
return;
end
end
grayImage = peaks(400);
% 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, 2, 1);
imshow(grayImage, []);
axis on;
title('Original Grayscale Image', 'FontSize', fontSize);
% Enlarge figure to full screen.
set(gcf, 'Units', 'Normalized', 'OuterPosition', [0 0 1 1]);
% Give a name to the title bar.
set(gcf, 'Name', 'Demo by ImageAnalyst', 'NumberTitle', 'Off')
% Let's compute and display the histogram.
[pixelCount, grayLevels] = imhist(grayImage);
subplot(2, 2, 2);
bar(grayLevels, pixelCount);
grid on;
title('Histogram of original image', 'FontSize', fontSize);
xlim([0 grayLevels(end)]); % Scale x axis manually.
% Binarize the image
binaryImage = grayImage > 0.9;
% Display the original gray scale image.
subplot(2, 2, 3);
imshow(binaryImage, []);
axis on;
title('Binary Image', 'FontSize', fontSize);
% Label the image
[labeledImage, numberOfObjects] = bwlabel(binaryImage);
measurements = regionprops(labeledImage, 'Centroid');
t = struct2table(measurements) % New with 2013 releases
xCentroids = t.Centroid(:,1)
yCentroids = t.Centroid(:,2)
% Find the column and row number nearest to the centroid
xCentroidColumns = int32(xCentroids)
yCentroidColumns = int32(yCentroids)
hold on;
% Plot centroids
for k = 1 : numberOfObjects
plot(xCentroids(k), yCentroids(k), 'ro', 'Markersize', 10, 'linewidth', 2);
end
% Find vertical and horizontal lines
for k = 1 : numberOfObjects
thisBlob = ismember(labeledImage, k);
% Look at column yCentroidColumns(k) and find out
% the top and bottom line of the blob.
topRow = find(thisBlob(:,xCentroidColumns(k)), 1, 'first');
bottomRow = find(thisBlob(:,xCentroidColumns(k)), 1, 'last');
plot([xCentroidColumns(k), xCentroidColumns(k)], [topRow, bottomRow], 'b-', 'LineWidth', 2);
% Horizontal lines
leftColumn = find(thisBlob(yCentroidColumns(k), :), 1, 'first');
rightColumn = find(thisBlob(yCentroidColumns(k), :), 1, 'last');
plot([leftColumn, rightColumn], [yCentroidColumns(k), yCentroidColumns(k)], 'b-', 'LineWidth', 2);
end

14 件のコメント

Star Strider
Star Strider 2014 年 2 月 23 日
編集済み: Star Strider 2014 年 2 月 23 日
Commenting on this so I can find it easily.
+1
Naishil shah
Naishil shah 2014 年 2 月 23 日
Thank you very much for this.Can you please help me to classify the detected object as regular or irregular?????
Image Analyst
Image Analyst 2014 年 2 月 23 日
First tell me what those terms mean to you.
Naishil shah
Naishil shah 2014 年 2 月 23 日
編集済み: Naishil shah 2014 年 2 月 23 日
Actually i want to find the shape of the object.I also want to find the irregular shape.But i don`t have an idea to find the regular and irregular object and i also want the size of the horizontal and vertical lines.
Image Analyst
Image Analyst 2014 年 2 月 23 日
編集済み: Image Analyst 2014 年 2 月 23 日
Well you know the length of the lines. It's just (rightColumn-leftColumn) and (bottomRow - topRow). But that's so obvious I wonder if I am missing something? I'm sure you know that already so what is wrong with those measures?
Secondly, I still don't know what regular or irregular mean, despite me asking and you answering. How about if I just say that they're all regular . Is anything wrong with that? I have no evidence to the contrary that they're not. I have not heard from you any explanation of why they're not.
Naishil shah
Naishil shah 2014 年 2 月 23 日
please check this image.Regular shapes are like Circle,Square and ellipse.And i want a size of the blue lines only.Thank you.
Image Analyst
Image Analyst 2014 年 2 月 23 日
編集済み: Image Analyst 2014 年 2 月 23 日
Then they're all irregular. An image is spatially quantized/digitized/sampled so you'll never get a mathematically perfect shape. They're all irregular.
Naishil shah
Naishil shah 2014 年 2 月 23 日
Yes,but i want to find the shape which is nearer to circle or ellipse.I don`t want to find the perfect shape.I want to classify the shape whether it is circle or ellipse.
Dishant Arora
Dishant Arora 2014 年 2 月 24 日
This might help you Identify Round Objects
Naishil shah
Naishil shah 2014 年 2 月 24 日
Thank you Dishant for your help.
Image Analyst
Image Analyst 2014 年 2 月 25 日
編集済み: Image Analyst 2014 年 2 月 25 日
You can find out which is closest to a circle or ellipse by looking at the ratio perimeter^2 / (4*pi*area). It's 1 for a circle and gets bigger the more tortuous the boundary is. You can also look at the solidity which is the ratio of the area to the convex hull. The problem before was that you wanted to distinguish "perfect" geometric shapes (triangles, pentagons, hexagons, etc.) from other irregular shapes like some arbitrary convex shape. To do that you'd have to test for every shape you're interested in individually. But if you just want some metric of how round something is, you can look at the circularity using the formula I gave you. You can also run the two attached demos.
Adam Drake
Adam Drake 2015 年 1 月 9 日
This answer is wonderful and works brilliantly. Only one issue - I have multiple image files that I need to process and display. Can you tell me how I can make this example properly display ucessive images (almost like a movie)? Currently it just displays the last image. Many thanks.
Image Analyst
Image Analyst 2015 年 1 月 9 日
Adam, see the FAQ: http://matlab.wikia.com/wiki/FAQ#How_can_I_process_a_sequence_of_files.3F as well as my attached demo to turn a figure into a movie.
Abdullah Alkurdi
Abdullah Alkurdi 2018 年 1 月 11 日
@Image Analyst you are the best

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

その他の回答 (3 件)

Dishant Arora
Dishant Arora 2014 年 2 月 22 日
編集済み: Dishant Arora 2014 年 2 月 22 日
doc line
I guess you have information about centroid and boundary points.

2 件のコメント

Naishil shah
Naishil shah 2014 年 2 月 22 日
Yes,i have information about centroid and boundary points.
Image Analyst
Image Analyst 2014 年 2 月 22 日
編集済み: Image Analyst 2014 年 2 月 22 日
So, is this solved or not? If so, mark it Accepted. If not, look at my Image Segmentation Tutorial to learn how to find centroids and boundary points. http://www.mathworks.com/matlabcentral/fileexchange/?term=authorid%3A31862 If you still need help after that, let me know. It's a lot easier to find vertical and horizontal bisectors than bisectors that go along the major and minor axes (if the blob is not aligned with the edges of the image), so you're lucky you want the easy way.

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

Star Strider
Star Strider 2014 年 2 月 22 日
My suggestion:
elps = @(a,b,c,d,t) [c+a.*cos(t); d+b.*sin(t)];
a = 0.13; b = 0.77; c = 3.12; d = 5.17;
t = 0:0.1:2*pi;
pts = elps(a,b,c,d,t); % Generate ellipse
endpts = [min(pts,[],2) max(pts,[],2)] % Limits of x, y
mxy = mean(pts,2); % Determine centroid
figure(1)
plot(pts(1,:),pts(2,:), 'b*')
hold on
plot(endpts(1,:), [1 1]*mxy(2), '-r') % Plot first axis in red
plot([1 1]*mxy(1), endpts(2,:), '-g') % plot second axis in green
hold off
grid

2 件のコメント

Naishil shah
Naishil shah 2014 年 2 月 23 日
Actually, I have a region obtained using regionprops method.I get the centroid of this region using the property 'Centroid' of regionprops method.Can you please help me using this method to draw vertical line and a horizontal line passing from the centroid of a region? Thanks in advance.
Image Analyst
Image Analyst 2014 年 2 月 23 日
You still couldn't figure it out even after running my tutorial? OK, I'll try to see if I can adapt it for you if I get time later this afternoon.

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

Dalila FEKRACHE
Dalila FEKRACHE 2016 年 1 月 22 日

0 投票

Good morning, After drawing vertical line and a horizontal line passing from the centroid of a region, How can i calculate the areas in each side of the two axis ?? Please help me

1 件のコメント

Image Analyst
Image Analyst 2016 年 1 月 22 日
Is the line really vertical and horizontal, or is it at an angle long the major axis? Maybe you should start your own question and attach your image.

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

Community Treasure Hunt

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

Start Hunting!

Translated by