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 commentaires

What information do you have? A set of points or an equation?
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.

Connectez-vous pour commenter.

 Réponse acceptée

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 commentaires

Commenting on this so I can find it easily.
+1
Thank you very much for this.Can you please help me to classify the detected object as regular or irregular?????
First tell me what those terms mean to you.
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.
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.
please check this image.Regular shapes are like Circle,Square and ellipse.And i want a size of the blue lines only.Thank you.
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.
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.
Thank you Dishant for your help.
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.
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 you are the best

Connectez-vous pour commenter.

Plus de réponses (3)

doc line
I guess you have information about centroid and boundary points.

2 commentaires

Yes,i have information about centroid and boundary points.
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.

Connectez-vous pour commenter.

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 commentaires

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.
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.

Connectez-vous pour commenter.

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 commentaire

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.

Connectez-vous pour commenter.

Catégories

En savoir plus sur Convert Image Type dans Centre d'aide et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by