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

centroidhorizontalImage Processing Toolboxshape recognitionvertical

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.

Best Answer

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