Using image processing tool measure the angle

26 views (last 30 days)
UsingMATLAB image processingtool, measure the angle between the connections for the following figures.
Teacher said that after some image processing we will need the polyfit function to determine the angle. And it will be good if our program calculates average angle rather than for only one incline.
  3 Comments
Akbar
Akbar on 25 May 2014
Below i've provided a code that i wrote for the image below. Now when you type:
imshow(D)
you will see two "lines" almost parallel to each other. I need help in finding their slopes (angles).
% Rope.m
% measure the angle between the connections
clc; clear; figure
A = imread('Rope.jpg');
B = rgb2gray(A);
C = imcrop(B, [220 295 70 90]);
D = C > 60;
%E=B>60;
%F = imcrop(E, [10 10 480 480]);
subplot(2,2,1); imshow(A);
subplot(2,2,2); imshow(B);
subplot(2,2,3); imshow(C);
subplot(2,2,4); imshow(D);
Akbar
Akbar on 25 May 2014
I think i should transform black pixels in image "D" to data points in x-y plane, then "somehow" using curve fitting find the slope or angle???

Sign in to comment.

Accepted Answer

Image Analyst
Image Analyst on 20 Jan 2018
Try this:
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 specified Toolbox installed and licensed.
hasLicenseForToolbox = license('test', 'image_toolbox'); % license('test','Statistics_toolbox'), license('test','Signal_toolbox')
if ~hasLicenseForToolbox
% User does not have the toolbox installed, or if it is, there is no available license for it.
% For example, there is a pool of 10 licenses and all 10 have been checked out by other people already.
ver % List what toolboxes the user has licenses available for.
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; % Determine where demo folder is (works with all versions).
baseFileName = 'rope.jpg';
% Get the full filename, with path prepended.
fullFileName = fullfile(folder, baseFileName);
% Check if file exists.
if ~exist(fullFileName, 'file')
% The file doesn't exist -- didn't find it there in that folder.
% Check the entire search path (other folders) for the file by stripping off the folder.
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
rgbImage = imread(fullFileName);
% Display the image.
subplot(2, 2, 1);
imshow(rgbImage, []);
title('Original Image', 'FontSize', fontSize, 'Interpreter', 'None');
axis on;
hp = impixelinfo();
% Get the dimensions of the image.
% numberOfColorChannels should be = 1 for a gray scale image, and 3 for an RGB color image.
[rows, columns, numberOfColorChannels] = size(rgbImage);
if numberOfColorChannels > 1
% It's not really gray scale like we expected - it's color.
% Use weighted sum of ALL channels to create a gray scale image.
% grayImage = rgb2gray(rgbImage);
% ALTERNATE METHOD: Convert it to gray scale by taking only the green channel,
% which in a typical snapshot will be the least noisy channel.
grayImage = rgbImage(:, :, 3); % Take bluechannel.
else
grayImage = rgbImage; % It's already gray scale.
end
% Display the image.
subplot(2, 2, 1);
imshow(grayImage, []);
title('Gray Scale Image', 'FontSize', fontSize, 'Interpreter', 'None');
axis on;
%------------------------------------------------------------------------------
% Set up figure properties:
% Enlarge figure to full screen.
set(gcf, 'Units', 'Normalized', 'OuterPosition', [0, 0.04, 1, 0.96]);
% 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')
subplot(2, 2, 2);
histogram(grayImage);
grid on;
title('Histogram', 'FontSize', fontSize, 'Interpreter', 'None');
% Create an edge image.
edgeImage = imgradient(grayImage);
% Get rid of upper rope.
edgeImage(1:300, :) = 0;
% Display the image.
subplot(2, 2, 3);
imshow(edgeImage, []);
title('Edge Image', 'FontSize', fontSize, 'Interpreter', 'None');
axis on;
hp = impixelinfo();
% Display the histogram.
subplot(2, 2, 4);
histogram(edgeImage);
grid on;
title('Histogram of Edge Image', 'FontSize', fontSize, 'Interpreter', 'None');
% Get rid of bright outer edges.
binaryImage = edgeImage > 1;
figure;
% Display the image.
subplot(2, 2, 1);
imshow(binaryImage, []);
title('Binary Image', 'FontSize', fontSize, 'Interpreter', 'None');
axis on;
% Fill holes
binaryImage = imfill(binaryImage, 'holes');
% Do an opening to separate the blobs.
se = strel('disk', 15, 0);
binaryImage = imerode(binaryImage, se);
% Display the image.
subplot(2, 2, 2);
imshow(binaryImage, []);
title('Binary Image', 'FontSize', fontSize, 'Interpreter', 'None');
axis on;
% Mask the original edge image
edgeImage(~binaryImage) = 0;
% Display the image.
subplot(2, 2, 3);
imshow(edgeImage, []);
title('Edge Image', 'FontSize', fontSize, 'Interpreter', 'None');
axis on;
% Display the histogram.
subplot(2, 2, 4);
histogram(edgeImage);
grid on;
title('Histogram of Edge Image', 'FontSize', fontSize, 'Interpreter', 'None');
% [lowThreshold, highThreshold, lastThresholdedBand] = threshold(20, 255, edgeImage);
lowThreshold = 100;
% Take the 24 largest blobs.
binaryImage = edgeImage > lowThreshold;
% Get rid of blobs smaller than 50 pixels.
binaryImage = bwareafilt(binaryImage, [50, inf]);
figure;
% Display the image.
subplot(2, 2, 1);
imshow(binaryImage, []);
title('Binary Image', 'FontSize', fontSize, 'Interpreter', 'None');
axis on;
% Get the orientations (angles
props = regionprops(binaryImage, 'Orientation', 'Area');
allAreas = [props.Area]
allAngles = [props.Orientation]
% Throw out unreasonable angles, like those less than 10 degrees.
allAngles(allAngles < 10) = [];
% Display the histogram.
subplot(2, 2, 2);
histogram(allAngles);
grid on;
title('Histogram of Edge Angles', 'FontSize', fontSize, 'Interpreter', 'None');
% Compute the mean angle
meanAngle = mean(allAngles)
message = sprintf('The mean angle = %f', meanAngle);
helpdlg(message);
  4 Comments
Darya Yakovleva
Darya Yakovleva on 21 Apr 2021
Hello! Could you tell me please, how to measure the avarege width of the rope?

Sign in to comment.

More Answers (3)

Van S
Van S on 20 Jan 2018
Dear Akbar,
I am interesting in angle measurement of the rope in matlab, could you please share to me about that now.
I really appreciate of your help. Best regards; Sry

VIJI S
VIJI S on 8 Aug 2019
I want to know the angle measurement in face image one is template(that is detected face)and other one is set of database which have different pose in face please help me please

VIJI S
VIJI S on 8 Aug 2019
I want to find the mean angle of face
  1 Comment
Image Analyst
Image Analyst on 9 Aug 2019
Why?
Anyway, use polyfit() to fit a line across the top of the rope and bottom of the rope, then along each twist. Then average the sets of lines and take the difference in angles. Sorry, I don't have code for that but polyfit() is super easy to use - I'm sure you can do it.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!