Finding the radius at regular intervals of theta for an irregular shaped object

4 views (last 30 days)
For my research project, I need to find the radius at regular intervals of theta for an irregularly shaped object (for this particular case, a kidney bean shape). I can read in the image and convert it to black and white and then use the edge function. But I cannot figure out a way to find the radii like I need too. Please and thank you for your help.

Accepted Answer

Image Analyst
Image Analyst on 21 Mar 2013
You don't need the edge function. Simply threshold your image, call bwboundaries, call regionprops to get the centroid, then run around the boundary to find the distance from the centroid to each boundary pixel, and the angle. Then if you need evenly spaced angles, and don't have any more than 1 boundary pixel at each angle, then you can use interp1 to get the angles at the values you want. Understand? If not, here's a demo where I did most everything for you, except the final interpolation part (not sure why that is needed though - it's probably not needed).
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
imtool close all; % Close all imtool figures if you have the Image Processing Toolbox.
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 standard MATLAB gray scale demo image.
folder = fullfile(matlabroot, '\toolbox\images\imdemos');
baseFileName = 'moon.tif';
% 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.
fullFileName = baseFileName; % No path this time.
if ~exist(fullFileName, '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 numberOfColorBands] = size(grayImage);
% Display the original gray scale image.
subplot(2, 3, 1);
imshow(grayImage, []);
title('Original Grayscale Image', 'FontSize', fontSize);
% Enlarge figure to full screen.
set(gcf, 'units','normalized','outerposition',[0 0 1 1]);
% Give a name to the title bar.
set(gcf,'name','Demo by ImageAnalyst','numbertitle','off')
% Let's compute and display the histogram.
[pixelCount grayLevels] = imhist(grayImage);
subplot(2, 3, 2);
bar(pixelCount);
grid on;
title('Histogram of original image', 'FontSize', fontSize);
xlim([0 grayLevels(end)]); % Scale x axis manually.
binaryImage = grayImage > 15;
% Clean it up a bit.
binaryImage = bwareaopen(binaryImage, 1000); % Remove small objects
binaryImage = imfill(binaryImage, 'holes'); % Fill holes.
subplot(2, 3, 3);
imshow(binaryImage, []);
title('Binary Image', 'FontSize', fontSize);
% Measure/find the centroid
measurements = regionprops(binaryImage, 'Centroid');
centroid = measurements.Centroid; % [row, column], NOT [x,y]
% bwboundaries() returns a cell array, where each cell contains the row/column coordinates for an object in the image.
% Plot the borders of all the objects on the original grayscale image using the coordinates returned by bwboundaries.
subplot(2, 3, 1);
title('Original image with Outlines, from bwboundaries()', 'FontSize', fontSize);
hold on;
boundaries = bwboundaries(binaryImage); % in row, column order, not x,y
numberOfBoundaries = size(boundaries, 1)
for k = 1 : numberOfBoundaries
thisBoundary = boundaries{k};
x = thisBoundary(:,2);
y = thisBoundary(:,1);
plot(x, y, 'g', 'LineWidth', 2);
end
hold off;
% Calculate the angles in degrees
deltaY = thisBoundary(:,1) - centroid(1);
deltaX = thisBoundary(:,2) - centroid(2);
angles = atand(deltaY ./ deltaX);
% Calculate the distances.
distances = sqrt((thisBoundary(:,1) - centroid(1)).^2 + (thisBoundary(:,2) - centroid(2)).^2);
% Plot distance vs. angle.
subplot(2, 3, 4:6);
plot(angles, distances, 'b-');
grid on;
title('Distance as a function of angle', 'FontSize', fontSize);
xlabel('Angle', 'FontSize', fontSize);
ylabel('Distance', 'FontSize', fontSize);
  17 Comments
Image Analyst
Image Analyst on 28 Mar 2013
threshold to find the cutout, then call regionprops. Same concept, you're just getting a different foreground object. Do you understand that regionprops makes measurements on the "white" regions in the binary image? So do whatever you have to do to get the region(s) you want to be white/1/true. Then call regionprops().
Jacob Lundeen
Jacob Lundeen on 28 Mar 2013
I'm figuring it out, slowly. We don't get taught a majority of these image manipulations and such, so I'm learning on the go.

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!