MATLAB: Using image processing tool measure the angle

image analysisimage processing

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.

Best Answer

  • 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);