how to find the angle and the sector of the image it belongs in

4 views (last 30 days)
How can I compute the angle of an image and which of the 8 sectors it belongs in.? i want also for each of the 8 groups of distances I computed, get their standard deviation." I atach a matlab code and the image.
  2 Comments
JovanS
JovanS on 18 Sep 2022
Edited: JovanS on 18 Sep 2022
Actually i have two problems . The fist one is that I don't calculate the angle in the right way and and the other one is that I can't complete the process for sorting distances by angle. I don't know what I have to change in my matlab code in order to fix it.
To be clear , I have to follow this procedure to receive the right results. " compute the Euclidean distance between each boundary coordinate and the centroid. Also compute the angle and which of the 8 sectors it belongs in. For each of the 8 groups of distances you computed, get their standard deviation. If it's more than some predetermined amount, increment the "border" value by one. After all this you will have 8 "border" values. If a sector has a smooth circular shape, it's std dev will be zero, and the std dev will increase the more tortuous the border is." @Matt J

Sign in to comment.

Accepted Answer

Image Analyst
Image Analyst on 18 Sep 2022
@Ioanna St 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 = 15;
%===============================================================================
% Get the name of the image the user wants to use.
baseFileName = 'color_wh.jpg'; % Assign the one on the button that they clicked on.
% Get the full filename, with path prepended.
folder = []; % Determine where demo folder is (works with all versions).
fullFileName = fullfile(folder, baseFileName);
%===============================================================================
% Read in a demo image.
grayImage = imread(fullFileName);
% 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(grayImage);
if numberOfColorChannels > 1
grayImage = rgb2gray(grayImage);
end
% Display the image.
subplot(2, 2, 1);
imshow(grayImage, []);
axis on;
caption = sprintf('Original Gray Scale Image');
title(caption, 'FontSize', fontSize, 'Interpreter', 'None');
drawnow;
hp = impixelinfo();
% Maximize figure window:
g = gcf;
g.WindowState = 'maximized';
drawnow;
% Binarize the image by thresholding.
binaryImage = grayImage<100;
% Get rid of white surround.
binaryImage = imclearborder(binaryImage);
% Extract the largest blob only.
binaryImage = bwareafilt(binaryImage, 1);
% Fill holes.
binaryImage=imfill(binaryImage,'holes');
% Display the image.
subplot(2, 2, 2);
imshow(binaryImage);
impixelinfo
axis on;
title('Binary Image', 'fontSize', fontSize);
drawnow;
% Label the image
labeledImage = bwlabel(binaryImage);
% Make the measurements
props = regionprops(labeledImage, grayImage, 'Centroid', 'WeightedCentroid');
xCentroid = props.Centroid(1)
yCentroid = props.Centroid(2)
% Find the half way point of the image.
middlex = columns/2;
middley = rows/2;
% Plot a + at the center of the blob and center of the image.
hold on;
yline(middley, 'Color', 'm', 'LineWidth', 2);
xline(middlex, 'Color', 'm', 'LineWidth', 2);
plot(xCentroid, yCentroid, 'r+', 'LineWidth', 2, 'MarkerSize', 30);
title('Binary Image with centers found', 'fontSize', fontSize);
% Display the image.
subplot(2, 2, 3);
imshow(grayImage);
axis on;
title('Gray Image with centroid marked', 'fontSize', fontSize);
% Plot a + at the center of the blob and center of the image.
hold on;
yline(middley, 'Color', 'm', 'LineWidth', 2);
xline(middlex, 'Color', 'm', 'LineWidth', 2);
plot(xCentroid, yCentroid, 'r+', 'LineWidth', 2, 'MarkerSize', 30);
drawnow;
% Compute center of mass, though user doesn't actually use it.
% centerOfMass = props.WeightedCentroid
% Find boundaries.
[B, L] = bwboundaries(binaryImage, 'noholes');
subplot(2, 2, 4);
imshow(grayImage);
title('Boundaries of the Image', 'fontSize', fontSize);
% Draw boundaries over the image.
hold on
numberOfRegions = numel(B)
for k = 1 : numberOfRegions % Should be just one boundary because we used bwareafilt
thisBoundary = B{k};
xB = thisBoundary(:,2);
yB = thisBoundary(:,1);
plot(thisBoundary(:,2), thisBoundary(:,1), 'b', 'LineWidth', 2)
end
numberOfSectors = 8; % I want to divide the image in 8 sectors
sectorAngleLimits = linspace(-180, 180, numberOfSectors + 1)
%border=0;
caDistances = cell(numberOfSectors,1);
for k = 1:numberOfRegions % Should be just one boundary because we used bwareafilt
thisBoundary = B{k};
xB = thisBoundary(:,2);
yB = thisBoundary(:,1);
distances = sqrt((xCentroid-xB).^2+(yCentroid-yB).^2);
% Find the angle in radians from centroid of every point in the border.
[angles, rho] = cart2pol((xB - xCentroid), (yB - yCentroid));
% Note: rho should equal distances.
% Convert angles to degrees.
angles = ((angles/(pi*2)) * 360);
% Alternate way.
% slopes = (yB - yCentroid) ./ (xB - xCentroid);
% angles =atand(slopes)
% Get distances for each sector
for ks = 1 : numberOfSectors
angleMask = angles >= sectorAngleLimits(ks) & angles < sectorAngleLimits(ks + 1);
distancesWithinEachSector{ks} = distances(angleMask);
end
end
distancesWithinEachSector
figure;
% Determine if the distances vary widely.
borderValue = zeros(1, numberOfSectors);
for c = 1 : numberOfSectors
thisSectorsDistances = distancesWithinEachSector{c};
subplot(3, 3, c);
histogram(thisSectorsDistances);
grid on;
caption = sprintf('Distance Distribution for Sector #%d', c);
title(caption, 'FontSize',fontSize)
stddev = std(thisSectorsDistances);
% Determine if it's big.
if stddev > 30 % sto paper anaferei oti threshold is set empirically to 30
borderValue(c) = 1;
end
end
borderValue
% Maximize figure window:
g2 = gcf;
g2.WindowState = 'maximized';
  11 Comments
Image Analyst
Image Analyst on 4 Nov 2022
Simply plot it to see what sector is where:
for ks = 1 : numberOfSectors
angleMask = angles >= sectorAngleLimits(ks) & angles < sectorAngleLimits(ks + 1);
ph = plot(xB(angleMask), yB(angleMask), 'r-', 'LineWidth', 3)
message = sprintf('This is sector #%d', ks);
reply = questdlg(message, 'Continue', 'Yes', 'No', 'Yes');
delete(ph);
if strcmpi(reply, 'No')
return;
end
distancesWithinEachSector{ks} = distances(angleMask);
end
Full demo attached.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!