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

5 visualizaciones (últimos 30 días)
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 comentarios
Andrew Newell
Andrew Newell el 22 de Feb. de 2014
What information do you have? A set of points or an equation?
Naishil shah
Naishil shah el 23 de Feb. de 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.

Iniciar sesión para comentar.

Respuesta aceptada

Image Analyst
Image Analyst el 23 de Feb. de 2014
Editada: Image Analyst el 23 de Feb. de 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 comentarios

Iniciar sesión para comentar.

Más respuestas (3)

Dishant Arora
Dishant Arora el 22 de Feb. de 2014
Editada: Dishant Arora el 22 de Feb. de 2014
doc line
I guess you have information about centroid and boundary points.
  2 comentarios
Naishil shah
Naishil shah el 22 de Feb. de 2014
Yes,i have information about centroid and boundary points.
Image Analyst
Image Analyst el 22 de Feb. de 2014
Editada: Image Analyst el 22 de Feb. de 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.

Iniciar sesión para comentar.


Star Strider
Star Strider el 22 de Feb. de 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 comentarios
Naishil shah
Naishil shah el 23 de Feb. de 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 el 23 de Feb. de 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.

Iniciar sesión para comentar.


Dalila FEKRACHE
Dalila FEKRACHE el 22 de En. de 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 comentario
Image Analyst
Image Analyst el 22 de En. de 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.

Iniciar sesión para comentar.

Categorías

Más información sobre Convert Image Type en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by