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

3 vues (au cours des 30 derniers jours)
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
Andrew Newell
Andrew Newell le 22 Fév 2014
What information do you have? A set of points or an equation?
Naishil shah
Naishil shah le 23 Fév 2014
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

Image Analyst
Image Analyst le 23 Fév 2014
Modifié(e) : Image Analyst le 23 Fév 2014
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

Connectez-vous pour commenter.

Plus de réponses (3)

Dishant Arora
Dishant Arora le 22 Fév 2014
Modifié(e) : Dishant Arora le 22 Fév 2014
doc line
I guess you have information about centroid and boundary points.
  2 commentaires
Naishil shah
Naishil shah le 22 Fév 2014
Yes,i have information about centroid and boundary points.
Image Analyst
Image Analyst le 22 Fév 2014
Modifié(e) : Image Analyst le 22 Fév 2014
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.


Star Strider
Star Strider le 22 Fév 2014
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
Naishil shah
Naishil shah le 23 Fév 2014
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 le 23 Fév 2014
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.


Dalila FEKRACHE
Dalila FEKRACHE le 22 Jan 2016
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
Image Analyst
Image Analyst le 22 Jan 2016
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 Image Processing and Computer Vision dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by