how to draw polar histogram of the object of image?

hello friends,
I am working on handwritten math expression recognition. I want to use polar histogram shape descriptor for shape feature extraction. I have attach the image of expression. As shown in image, from the mid point between two centroids I have to draw polar bins of this objects. I would be thankful if someone helps me to draw polar histogram.

 Accepted Answer

Find the midpoint. Then scan the image with a nested for loop over all rows and columns. At each (row,column) compute the angle using simple trigonometry (atan2d). See if the image is white at that location, and if it is, increment the histogram array by one.
hist2D = zeros(1, 360);
for row = 1 : rows
for col = 1 : columns
angle = round(atan(......
if binaryImage(row, col) == 1
hist2D(angle) = hist2D(angle) + 1;
end
end
end
It's a pretty trivial brute force method but it should work pretty fast (less than a second or so). Let me know if you can't figure out the code after trying to.

7 Comments

hello Image Analyst, Thank you for your response. I am new in matlab. I would be thankful if you explain this code with above figure example.
It's all very basic stuff, like for loops and assignments. I'm headed out now and don't have time to explain it line by line. Is there one particular line you don't understand? I'll be back later this afternoon or evening. In the meantime, see this.
Binary image is the name of your image and has values of 0 or 1. If it's 0 and 255, then just do
binaryImage = grayImage > 1128;
Other than that look up for loop, atan2d(), and zeros() in the help to understand what they do.
You do know that the angle is atan2d(deltay/deltax) and that deltax = x - xCenter, right?
Rujal
Rujal on 7 Feb 2016
Edited: Rujal on 7 Feb 2016
Thank you Image Analyst. I have attach a pdf which I want to refer.
but here's some more code that you can use as a start. Good luck.
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;
%===============================================================================
% Read in a demo image.
folder = pwd;
baseFileName = 'capture1.jpg';
% 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
originalImage = imread(fullFileName);
% Get the dimensions of the image.
% numberOfColorBands should be = 1.
[rows, columns, numberOfColorChannels] = size(originalImage);
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 = originalImage(:, :, 3); % Take green channel.
else
grayImage = originalImage; % It's already gray level.
end
% Display the image.
subplot(2, 2, 1);
imshow(grayImage, []);
axis on;
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')
% Binarize the image
binaryImage = grayImage > 100;
binaryImage = imfill(binaryImage, 'holes'); % Get rid of asterisk artifacts.
binaryImage = imclearborder(binaryImage); % Get rid of white bars at top and bottom.
% Display the image.
subplot(2, 2, 2);
imshow(binaryImage, []);
axis on;
title('Initial Binary Image', 'FontSize', fontSize, 'Interpreter', 'None');
% Display the image.
subplot(2, 2, 3);
imshow(binaryImage, []);
axis on;
title('Final Binary Image', 'FontSize', fontSize, 'Interpreter', 'None');
% Label the image.
[labeledImage, numberOfBlobs] = bwlabel(binaryImage);
% Measure bounding box(es).
blobMeasurements = regionprops(labeledImage, 'Centroid', 'Area');
allAreas = [blobMeasurements.Area]
xCenter = [blobMeasurements(1).Centroid(1) + blobMeasurements(2).Centroid(1)]/2
yCenter = [blobMeasurements(1).Centroid(2) + blobMeasurements(2).Centroid(2)]/2
% Plot the centers.
hold on;
plot(blobMeasurements(1).Centroid(1), blobMeasurements(1).Centroid(2), 'r*', 'MarkerSize', 30, 'LineWidth', 2);
plot(blobMeasurements(2).Centroid(1), blobMeasurements(2).Centroid(2), 'r*', 'MarkerSize', 30, 'LineWidth', 2);
plot(xCenter, yCenter, 'r*', 'MarkerSize', 30, 'LineWidth', 2);
subplot(2, 2, 4);
hist2D = zeros(columns, 360);
for row = 1 : rows
for col = 1 : columns
thisX = col;
thisY = row;
deltaX = thisX - xCenter;
deltaY = thisY - yCenter;
angle = ceil(atan2d(deltaY, deltaX) + 180);
radius = round(sqrt(deltaX^2 + deltaY^2));
if binaryImage(row, col) == 1
hist2D(radius, angle) = hist2D(radius, angle) + 1;
end
end
end
imshow(hist2D, []);
axis on;
title('2D Histogram', 'FontSize', fontSize, 'Interpreter', 'None');
thank you so much.. its really helpful.
Hello Image Analyst, Do you have an idea how to draw polar plots on binary image that I have attached?
Not really sure. I know it can be done, but I've never done it so I don't have any demos all ready for you. I'd have to look it up just like you'd have to. Good luck. Thanks for Accepting the Answer.

Sign in to comment.

More Answers (0)

Categories

Find more on Convert Image Type in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!