mathworks / protractor

Measure angles in images or in non-image axes, using MATLAB.
Other
0 stars 2 forks source link

? #1

Closed ghost closed 3 years ago

shoelson commented 3 years ago

My protractor function allows for manual manipulation of an angle (and distance) measuring tool. It’s very different from regionprops(I, ‘orientation’). Download it, give it a try. Brett

From: mike_lars @.> Sent: Sunday, April 4, 2021 11:30 PM To: mathworks/protractor @.> Cc: Brett Shoelson @.>; Mention @.> Subject: [mathworks/protractor] find the angles of the objects in the binary image .? (#1)

Respected @shoelsonhttps://github.com/shoelson, I have a binary image in which there are 12 white objects. I want to find the angle of each object using Matlab. I was researching and found the protractor library. I am very excited to work with this library. Based on this, I tried to find the angles but I am getting the wrong angles. I would like to know how to work with a protractor to get such a kind of angle. Any leads will be appreciated. Thanks in advance binary img https://drive.google.com/file/d/13A-P069YXOGw6AUz6MwjgHhjU81EnkTm/view?usp=sharing

this below is my matlab code

clc; % Clear the command window.

close all; % Close all figures (except those of imtool.)

clear; % Erase all existing variables. Or clearvars if you want.

workspace; % Make sure the workspace panel is showing.

format long g;

format compact;

fontSize = 20;

% Check that user has the Image Processing Toolbox installed.

hasIPT = license('test', 'image_toolbox'); % license('test','Statistics_toolbox'), license('test','Signal_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

%===============================================================================

% Read in gray scale demo image.

folder = pwd

baseFileName = 'bill_mold_check.png';

% Get the full filename, with path prepended.

fullFileName = fullfile(folder, baseFileName);

% Check if file exists.

if ~exist(fullFileName, 'file')

% File doesn't exist -- didn't find it there. Check the search path for it.

fullFileNameOnSearchPath = baseFileName; % No path this time.

if ~exist(fullFileNameOnSearchPath, 'file')

% Still didn't find it.  Alert user.

errorMessage = sprintf('Error: %s does not exist in the search path folders.', fullFileName);

uiwait(warndlg(errorMessage));

return;

end

end

grayImage = imread(fullFileName);

% Get the dimensions of the image.

% numberOfColorBands should be = 1.

[rows, columns, numberOfColorChannels] = size(grayImage);

if numberOfColorChannels > 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 image.

subplot(2, 2, 1);

imshow(grayImage, []);

title('Original Grayscale Image', 'FontSize', fontSize, 'Interpreter', 'None');

% Set up figure properties:

% Enlarge figure to full screen.

set(gcf, 'Units', 'Normalized', 'OuterPosition', [0 0 1 1]);

% Get rid of tool bar and pulldown menus that are along top of figure.

set(gcf, 'Toolbar', 'none', 'Menu', 'none');

% Give a name to the title bar.

set(gcf, 'Name', 'Demo by ImageAnalyst', 'NumberTitle', 'Off')

% Let's compute and display the histogram.

subplot(2, 2, 2);

histogram(grayImage, 0:256);

grid on;

title('Histogram of original image', 'FontSize', fontSize, 'Interpreter', 'None');

xlabel('Gray Level', 'FontSize', fontSize);

ylabel('Pixel Count', 'FontSize', fontSize);

xlim([0 255]); % Scale x axis manually.

% Threshold and binarize the image

binaryImage = grayImage > 128;

% Display the image.

subplot(2, 2, 3);

imshow(binaryImage, []);

axis on;

title('Binary Image', 'FontSize', fontSize, 'Interpreter', 'None');

% Label the image

labeledImage = bwlabel(binaryImage);

% Get the orientation

%measurements = regionprops(labeledImage, 'Orientation', 'MajorAxisLength', 'Centroid');

%allAngles = -[measurements.Orientation]

%hold on;

measurements = regionprops(labeledImage, 'Orientation', 'Centroid');

allAngles = -[measurements.Orientation]

xy = vertcat(measurements.Centroid)

hold on;

for k = 1 : length(measurements)

fprintf('For blob #%d, the angle = %.4f\n', k, allAngles(k));

x = xy(k, 1);

y = xy(k, 2);

text(x, y, num2str(k));

end

for k = 1 : length(measurements)

fprintf('For blob #%d, the angle = %.4f\n', k, allAngles(k));

xCenter = measurements(k).Centroid(1);

yCenter = measurements(k).Centroid(2);

Plot centroids.

plot(xCenter, yCenter, 'r*', 'MarkerSize', 15, 'LineWidth', 2);

Determine endpoints

axisRadius = measurements(k).MajorAxisLength / 2;

x1 = xCenter + axisRadius * cosd(allAngles(k));

x2 = xCenter - axisRadius * cosd(allAngles(k));

y1 = yCenter + axisRadius * sind(allAngles(k));

y2 = yCenter - axisRadius * sind(allAngles(k));

fprintf('x1 = %.2f, y1 = %.2f, x2 = %.2f, y2 = %.2f\n\n', x1, y1, x2, y2);

plot([x1, x2], [y1, y2], 'r-', 'LineWidth', 2);

end

output from this code [Image removed by sender. Screenshot from 2021-04-02 11-44-10]https://user-images.githubusercontent.com/55427143/113534081-4c033b00-95ed-11eb-831c-b17ce8e67302.png

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHubhttps://github.com/mathworks/protractor/issues/1, or unsubscribehttps://github.com/notifications/unsubscribe-auth/ADZEN6FDEKSDFJN3AUU4EADTHEVEZANCNFSM42MDPYOA.