centroid distance calculation at 10 degree

2 次查看(过去 30 天)
I m having leaf image I need to calculate 36 centroid contour descriptor of it @ distance of 10 degree starting from maximum radius. I have used flavia dataset and have to calculate for its image but here I have attached one image. I got the centroid, centroid distance from centroid to every boundary pixel and max of it also. kindly guide me how can I proceed further. plz help
Thanx

采纳的回答

Image Analyst
Image Analyst 2016-11-6
编辑:Image Analyst 2016-11-6
Use max() on the distances from the centroid to the boundary pixels. Then use circshift to move that index to the beginning:
% Find out where the max distance is.
[maxDistance, indexOfMaxDistance] = max(distances);
% Shift that index to be at index #1.
newDistances = circshift(distances, -(indexOfMaxDistance-1));
Then you will have to subsample that to get indexes only at every 10 degrees. This means that for every boundary point, you have to compute its angle as well as its distances using atand2(). I hope you can figure out that part.
  2 个评论
Image Analyst
Image Analyst 2016-11-8
Your problem (below) was that you chose the wrong threshold. Try this:
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
imtool close all; % Close all imtool figures.
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 the color image.
folder = pwd;
baseFileName = '1001.jpg';
% Get the full filename, with path prepended.
fullFileName = fullfile(folder, baseFileName);
if ~exist(fullFileName, 'file')
% 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.', fullFileName);
uiwait(warndlg(errorMessage));
return;
end
end
rgbImage = imread(fullFileName);
% Get the dimensions of the image. numberOfColorBands should be = 3.
[rows columns numberOfColorBands] = size(rgbImage);
% Display the original color image.
subplot(4, 2, 1);
imshow(rgbImage, []);
axis on;
title('Original Color Image', 'FontSize', fontSize);
drawnow;
% Enlarge figure to full screen.
set(gcf, 'units','normalized','outerposition',[0 0 1 1]);
% Histogram the red channel, but there is a huge spike at 255 so let's not count those.
subplot(4, 2, 2);
redChannel = rgbImage(:,:,1);
imshow(redChannel, []);
title('Red Channel', 'FontSize', fontSize);
subplot(4, 2, 3);
histogram(redChannel(redChannel<255))
grid on;
title('Histogram of Red Channel', 'FontSize', fontSize);
% Get the binary image by thresholding the red channel.
binaryImage = rgbImage(:,:,1) < 128;
% Fill holes
binaryImage = imfill(binaryImage, 'holes');
% Take largest blob only.
binaryImage = bwareafilt(binaryImage, 1);
subplot(4, 2, 4);
imshow(binaryImage, []);
axis on;
title('Binary Image', 'FontSize', fontSize);
drawnow;
uiwait(helpdlg('See the red channel histogram and the binary image'));
[labeledImage numberOfBlobs] = bwlabel(binaryImage, 8); % Label each blob so we can make measurements of it
% Get the centroid.
blobMeasurements = regionprops(labeledImage, 'Centroid');
xCenter = blobMeasurements(1).Centroid(1);
yCenter = blobMeasurements(1).Centroid(2);
% Plot the centroid.
hold on;
plot(xCenter, yCenter, 'r+', 'MarkerSize', 20, 'LineWidth', 3);
% 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 coins on the original grayscale image using the coordinates returned by bwboundaries.
subplot(4, 2, 5);
imshow(rgbImage);
axis on;
title('Outlines, from bwboundaries()', 'FontSize', fontSize);
drawnow;
hold on;
boundaries = bwboundaries(binaryImage);
numberOfBoundaries = size(boundaries);
% numberOfBoundaries will be 1 since we called bwareafilt.
thisBoundary = boundaries{1};
plot(thisBoundary(:, 2), thisBoundary(:,1), 'r', 'LineWidth', 3);
% for each boundary, calculate the angle an distance as a function of boundary point.
numberOfBoundaryPoints = length(thisBoundary);
angles = zeros(1, numberOfBoundaryPoints);
distances = zeros(1, numberOfBoundaryPoints);
for p = 1 : numberOfBoundaryPoints
xb = thisBoundary(p,2);
yb = thisBoundary(p,1);
angles(p) = atand((yb-yCenter) / (xb-xCenter));
distances(p) = sqrt((xb-xCenter)^2+(yb-yCenter)^2);
end
% Plot them
subplot(4, 2, 6);
plot(angles, 'LineWidth', 3);
caption = sprintf('Angles for Blob');
title(caption, 'FontSize', fontSize);
grid on;
subplot(4, 2, 7);
plot(distances, 'LineWidth', 3);
grid on;
caption = sprintf('Distances for blob');
title(caption, 'FontSize', fontSize);
hold off;
Also, you still need to do the shifting stuff, because you just happened to luck out here with the longest distance being the first element of the boundary. That won't always be the case with other orientation of leaves.
parul mittal
parul mittal 2016-11-12
Sr plz tell me after segmenting image how to subsample inorder to get indexes only at every 10 degrees. Thnx plz help me sr

请先登录,再进行评论。

更多回答(2 个)

parul mittal
parul mittal 2016-11-7
编辑:parul mittal 2016-11-7
Thanx sr for your reply. Sr i have went through ur code mentioned below to find angle and distance. Issue is I m not getting idea how to sample at 10 degree. Kindly help me with this issue. Thanx
if true
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
imtool close all; % Close all imtool figures.
clear; % Erase all existing variables.
workspace; % Make sure the workspace panel is showing.
format longg;
format compact;
fontSize = 20;
% Read in the color image.
folder = 'C:\Users\Diah\Documents';
baseFileName = 'flower.jpg';
% Get the full filename, with path prepended.
fullFileName = fullfile(folder, baseFileName);
if ~exist(fullFileName, 'file')
% 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.', fullFileName);
uiwait(warndlg(errorMessage));
return;
end
end
rgbImage = imread(fullFileName);
% Get the dimensions of the image. numberOfColorBands should be = 3.
[rows columns numberOfColorBands] = size(rgbImage);
% Display the original color image.
subplot(2, 2, 1);
imshow(rgbImage, []);
axis on;
title('Original Color Image', 'FontSize', fontSize);
% Enlarge figure to full screen.
set(gcf, 'units','normalized','outerposition',[0 0 1 1]);
% Get the binary image by thresholding the red channel.
binaryImage = rgbImage(:,:,1) > 34;
subplot(2,2,2);
imshow(binaryImage, []);
axis on;
title('Binary Image', 'FontSize', fontSize);
[labeledImage numberOfBlobs] = bwlabel(binaryImage, 8); % Label each blob so we can make measurements of it
% Get the centroid.
blobMeasurements = regionprops(labeledImage, 'Centroid');
xCenter = blobMeasurements(1).Centroid(1);
yCenter = blobMeasurements(1).Centroid(2);
% 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 coins on the original grayscale image using the coordinates returned by bwboundaries.
subplot(2,2,3);
imshow(rgbImage);
axis on;
title('Outlines, from bwboundaries()', 'FontSize', fontSize);
hold on;
% Plot the centroid.
plot(xCenter, yCenter, 'r+', 'MarkerSize', 20, 'LineWidth', 3);
boundaries = bwboundaries(binaryImage);
numberOfBoundaries = size(boundaries);
for k = 1 : numberOfBoundaries
thisBoundary = boundaries{k};
plot(thisBoundary(:,2), thisBoundary(:,1), 'r', 'LineWidth', 3);
% for each boundary, calculate the angle an distance as a function of boundary point.
numberOfBoundaryPoints = length(thisBoundary);
angles = zeros(1, numberOfBoundaryPoints);
distances = zeros(1, numberOfBoundaryPoints);
for p = 1 : numberOfBoundaryPoints
xb = thisBoundary(p,2);
yb = thisBoundary(p,1);
angles(p) = atand((yb-yCenter) / (xb-xCenter));
distances(p) = sqrt((xb-xCenter)^2+(yb-yCenter)^2);
end
% Plot them
subplot(4, 2, 6);
plot(angles, 'LineWidth', 3);
caption = sprintf('Angles for blob #%d', k);
title(caption, 'FontSize', fontSize);
grid on;
subplot(4, 2, 8);
plot(distances, 'LineWidth', 3);
grid on;
caption = sprintf('Distances for blob #%d', k);
title(caption, 'FontSize', fontSize);
% Prompt user for the next blob, if there is one.
if numberOfBoundaries > 1
promptMessage = sprintf('Do you want to Continue processing,\nor Cancel processing?');
titleBarCaption = 'Continue?';
button = questdlg(promptMessage, titleBarCaption, 'Continue', 'Cancel', 'Continue');
if strcmpi(button, 'Cancel')
break;
end
end
end
hold off;
end

parul mittal
parul mittal 2016-11-13
plz tell me after segmenting image how to subsample inorder to get indexes only at every 10 degrees. Thnx plz help me sr

Community Treasure Hunt

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

Start Hunting!

Translated by