Thread Subject: help me to compute the distance from boundary points to centroid

Subject: help me to compute the distance from boundary points to centroid

From: Ibtesam Saleh

Date: 18 Mar, 2010 20:46:05

Message: 1 of 24

Dear all

can someone help me in my problem.
I have binary image and I compute the centroid of it, now I want to find the distance from the boundary points to the centroid to find the largest radius. After that, I will put my image inside a unit of circle which I compute before its centroid and radius.

please help me to compute the radius.

thanks

Subject: help me to compute the distance from boundary points to centroid

From: ImageAnalyst

Date: 19 Mar, 2010 00:35:32

Message: 2 of 24

Ibtesam Saleh:
You mean like this:

clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
clear all; % Erase all existing variables.
workspace; % Make sure the workspace panel is showing.
% Make demo image.
grayImage = 50 * ones(400,400);
% Make white blob;
grayImage(200:300, 200:300) = 255;
% subplot(2, 2, 1);
imshow(grayImage, []);
set(gcf, 'Position', get(0,'Screensize')); % Maximize figure.
title('Original Grayscale Image');

% Binarize the image so we can label it.
binaryImage = grayImage > 128;

% Label each blob so we can make measurements of it
labeledImage = bwlabel(binaryImage, 8);

% Get all the blob properties.
blobMeasurements = regionprops(labeledImage, 'Centroid');
% Get the centroid.
centroidX = blobMeasurements(1).Centroid(1)
centroidY = blobMeasurements(1).Centroid(2)
% Get the boundary.
boundaries = bwboundaries(binaryImage);
thisBoundary = boundaries{1};
% Get the distances of the boundary pixels from the centroid.
distances = sqrt((thisBoundary(:,1) - centroidX).^2 + (thisBoundary(:,
2) - centroidY).^2);
% Scan the boundary to find the pixel on it that is
% farthest from the centroid.
maxRadius = max(distances);

% Place a circle at the centroid with the largest radius.
boxX = centroidX - maxRadius;
boxY = centroidY - maxRadius;
rectangle('Position',[boxX, boxY, 2*maxRadius, 2*maxRadius],...
    'Curvature',[1,1], 'EdgeColor', 'r', 'LineWidth', 3);
daspect([1,1,1]);
axis square;
% Place a cross at the centroid.
hold on;
plot(centroidX, centroidY, 'r+', 'MarkerSize', 15);
caption = sprintf('Done!\nThe maximum radius = %.2f', maxRadius);
fprintf(1, '%s\n', caption);
msgbox(caption);

Subject: help me to compute the distance from boundary points to centroid

From: Ibtesam Saleh

Date: 19 Mar, 2010 01:00:22

Message: 3 of 24

ImageAnalyst <imageanalyst@mailinator.com> wrote in message <4e8b75c1-eff0-4254-b89a-beef4fcc217e@o30g2000yqb.googlegroups.com>...

thaaaaaaaaaaaaanks alot. Yes that is I mean. I just RUN your code and it gave me what I want. but how can I put my image insted of yours...

thanks bunches
Ibtesam

Subject: help me to compute the distance from boundary points to centroid

From: ImageAnalyst

Date: 19 Mar, 2010 01:07:54

Message: 4 of 24

Just say

grayImage = imread('c:\your folder\yourImageName.tif');
[rows columns numberOfColorBands] = size(grayImage);
if numberOfColorBands > 1
  % Convert RGB image to monochrome.
  grayImage = rgb2gray(grayImage);
end

or something similar

Subject: help me to compute the distance from boundary points to centroid

From: Ibtesam Saleh

Date: 20 Mar, 2010 07:01:07

Message: 5 of 24

ImageAnalyst <imageanalyst@mailinator.com> wrote in message <06e095f8-4b38-445b-bdda-27f50b1ba3c2@g11g2000yqe.googlegroups.com>...
> Just say
>
> grayImage = imread('c:\your folder\yourImageName.tif');
> [rows columns numberOfColorBands] = size(grayImage);
> if numberOfColorBands > 1
> % Convert RGB image to monochrome.
> grayImage = rgb2gray(grayImage);
> end
>
> or something similar

Thank you very much for replying
OK I read my image
inputImage=imread('c:\your folder\yourImageName.tif');
then I convert it to black/white image (binary image)
bw=im2bw(inputImage)
then I need to made edge detection to extract some features
edgeImage=edge(bw,'canny')
then I found the centroid of my image (I need the centroid and the radius to do Zernike Moment)
center=regionprops(bw, 'Centroid')
now I need the radius (the centroid and the radius will be my input to the Zernike function)

I am sorry for bothering but I have confused about compute the maximum radius, my Zernike function will get the centroid and the radius to put my image in a unit of circle/disk

thanks alot
Ibtesam

Subject: help me to compute the distance from boundary points to centroid

From: ImageAnalyst

Date: 20 Mar, 2010 20:19:06

Message: 6 of 24

You don't call canny on a binary image. You're supposed to call the
edge detection function BEFORE you binarize the image. If you want
edges of a binary image, call bwperim().

But anyway, I didn't see a question in your latest response. Do you
have any questions? I don't have any zernicke functions in the
toolboxes I have, so I can't answer any questions about that.

Subject: help me to compute the distance from boundary points to centroid

From: Ibtesam Saleh

Date: 20 Mar, 2010 21:37:04

Message: 7 of 24

ImageAnalyst <imageanalyst@mailinator.com> wrote in message <0ed21bb8-e07d-4f86-b96a-5b897272edef@z3g2000yqz.googlegroups.com>...
> You don't call canny on a binary image. You're supposed to call the
> edge detection function BEFORE you binarize the image. If you want
> edges of a binary image, call bwperim().
>
> But anyway, I didn't see a question in your latest response. Do you
> have any questions? I don't have any zernicke functions in the
> toolboxes I have, so I can't answer any questions about that.
-----------------------------------------------------------------------------------------
thanks alot for your quick responds, I am really appreciated you.
I did what you are suggest, but an error message appear
This is my code:
>> inputImage=imread('Water lilies.jpg');
>> edgeImage=edge(inputImage,'canny');
-----------------------------------------------------------------------------------------
And this is the error message:
??? Error using ==> images\private\checkinput>check_attributes
Function EDGE expected its first input argument, I,
to be two-dimensional.

Error in ==> images\private\checkinput at 37
check_attributes(A, attributes, function_name, variable_name, ...

Error in ==> edge>parse_inputs at 502
checkinput(I,{'double','logical','uint8','uint16'},...

Error in ==> edge at 161
[a,method,thresh,sigma,H,kx,ky] = parse_inputs(varargin{:});
--------------------------------------------------------------------------------------------
I try to use 'canny' before binarize the image but it is not respond until I convert my image to binary.(I try that before your suggest, and when the error message appear I convert it to binary and no errors face, that why I am convert it to binary the use edge function)
bw=im2bw(inputImage);
edgeImage=edge(bw,'canny');
--------------------------------------------------------------------------------------------
I try to view the both 2 ways
edge1=edge(bw,'canny'); %after I binarize the image
edge2=bwperim(bw); %after I binarize the image
The output appear SAME. Would you please explain what the different.
---------------------------------------------------------------------------------------------
Thank you very much.
Ibtesam

  

Subject: help me to compute the distance from boundary points to centroid

From: Ibtesam Saleh

Date: 20 Mar, 2010 23:56:05

Message: 8 of 24

Also I face another problem
fileName ='C:\MATLAB7\work\images\0000023.gif';
inputImage=imread(fileName);
-----------------------------------------------------------------------
This error message appeared:
-----------------------------------------------------------------------
??? Attempt to execute SCRIPT imread as a function.


I try to put my folder 'images' in another path but it still appear the previous message.

please help me to fix this problem.
thanks in advance
Ibtesam

Subject: help me to compute the distance from boundary points to centroid

From: ImageAnalyst

Date: 21 Mar, 2010 00:02:41

Message: 9 of 24

Ibtesam:
Yes, I see. Say, did you ever read my second reply to you? Did you
recall seeing this code:
[rows columns numberOfColorBands] = size(grayImage);
if numberOfColorBands > 1
  % Convert RGB image to monochrome.
  grayImage = rgb2gray(grayImage);
end

Now where in your most recent code is this code?

OK, so despite me writing robust code in anticipation of you passing
it a color image, you chose to not put that code in. It tells you
that your image is not two dimensional, so did you ever check the
dimensions (like the above code)? Did you realize that a color image
is a 3D matrix as far as matrices are concerned? OK, so now don't you
think you want to incorporate the code like I told you to?

Subject: help me to compute the distance from boundary points to centroid

From: Ibtesam Saleh

Date: 21 Mar, 2010 17:10:05

Message: 10 of 24

Dear Image Analyst (the fantastic teacher who helped me),
I am really thankful for your help. I am not understand your last message but I feel you are upset from some thing. Your robust code is help me sooooooooo much and I use it, but I take what I need from your code, and I learn new things that I don't know before. I am appreciate you support and I am sorry if I cause bothering.
 
I am really still not understand the difference between:
> edge1=edge(bw,'canny'); %after I binarize the image
> edge2=bwperim(bw); %after I binarize the image
The outputs are the same. and what are you mean in this part of code:

[rows columns numberOfColorBands] = size(grayImage);
if numberOfColorBands > 1
  % Convert RGB image to monochrome.
  grayImage = rgb2gray(grayImage);
end
------------------------------------------------------------------------------------------------------
Also about the error message that I faced (??? Attempt to execute SCRIPT imread as a function.), I solved that error :) which caused because I named my file (imread.m) so the Matlab has confused which imread it will call to implement (the build one OR my file).
-------------------------------------------------------------------------------------------------------

I reiterate my thanks and my highest consideration
Ibtesam
   

Subject: help me to compute the distance from boundary points to centroid

From: ImageAnalyst

Date: 21 Mar, 2010 17:31:40

Message: 11 of 24

Ibtesam:
Your first error on dimensions was caused by your not passing it a 2D
gray scale image. By using my code you will have that fixed. And it
sounds like you fixed your second error by not redefining imread().
So all is well now, correct?

Subject: help me to compute the distance from boundary points to centroid

From: Ibtesam Saleh

Date: 21 Mar, 2010 19:41:03

Message: 12 of 24

I read my images as you told, and this is my code (readImages.m)
   
    imageName = '0000001.gif'
    fileName = strcat('C:\MATLAB7\work\images\',imageName)
    img=imread(fileName);
    %bw=im2bw(img);
    %edgeImage = bwperim(bw);
    
    [rows columns numberOfColorBands] = size(img);
    if numberOfColorBands > 1
    % Convert RGB image to monochrome.
    grayImage = rgb2gray(img);
    imshow(grayImage);
    end
    edgeImage = edge(grayImage,'canny');
   

the 'if statement' not implement i.e (numberOfColorBands <= 1) (my images are not colored they are black an white by default)
and this error message appeared
??? Undefined function or variable "grayImage".
--------------------------------------------------------------------------------------------
Also I want to ask you about
blobMeasurements = regionprops(double(img),'Centroid');
it will compute the center of my shape that drawn in my image or will compute the center of the whole image?? also I used your way to draw the circle but the shape is some times appear out the circle, I want my shape to be exactly inside the circle and the boundary of the shape never out the circle. Would you please help me how can do that??

Ibtesam

Subject: help me to compute the distance from boundary points to centroid

From: Ibtesam Saleh

Date: 22 Mar, 2010 22:54:04

Message: 13 of 24

please can any one help me in my problem. I post it in the previous post.

Ibtesam

Subject: help me to compute the distance from boundary points to centroid

From: ImageAnalyst

Date: 22 Mar, 2010 23:48:20

Message: 14 of 24

Ibtesam:
I don't have your image so I can't do anything about the "circle
outside image" problem. You can post your image to http://drop.io

You have to think about your code. WHY does it say that grayImage is
undefined? Could it be that you read it in like this:
img=imread(fileName);
instead of this:
grayImage = imread(fileName);

Subject: help me to compute the distance from boundary points to centroid

From: Ibtesam Saleh

Date: 23 Mar, 2010 01:25:10

Message: 15 of 24

Yes you are right :) I am mistake I wrote:
> img=imread(fileName);
> instead of:
> grayImage = imread(fileName);

OK, Now this problem is solved. thank you soooo much.

for the second problem, I send my code and my images to your e-mail.

thanks alot
Ibtesam

Subject: help me to compute the distance from boundary points to centroid

From: ImageAnalyst

Date: 23 Mar, 2010 01:31:10

Message: 16 of 24

Well I ignore that email - it's just for spam protection. It can't
take attachments. Just post your code here in case anyone else wants
to comment on it, and post your image to http://drop.io like I
mentioned earlier.

Subject: help me to compute the distance from boundary points to centroid

From: Ibtesam Saleh

Date: 23 Mar, 2010 01:57:05

Message: 17 of 24

I am sorry, I think its better to send by e-mail.
------------------------------------------
This is my (readImages.m)
------------------------------------------
for i = 20:20
    leftName ='0000000';
    rightName = int2str(i);
    imageName = strcat(leftName,rightName);
    imageName = imageName(length(rightName)+1:length(imageName));
    imageName = strcat(imageName,'.gif');
    fileName = strcat('C:\MATLAB7\work\images\',imageName)
    %img=imread(fileName);
    %bw=im2bw(img);
    %edgeImage = bwperim(bw);
    %edgeImage = edge(img,'canny');
    %imshow(img);
    % img is the image file you need
    grayImage = imread(fileName);
    [rows columns numberOfColorBands] = size(grayImage);
    if numberOfColorBands > 1
    % Convert RGB image to monochrome.
    grayImage = rgb2gray(grayImage);
    end
    edgeImage = edge(grayImage,'canny');
    [centroidX centroidY]= imageCentroid(edgeImage);
    
    %grayImage = imread(fileName);
    %[rows columns numberOfColorBands] = size(grayImage);
    %if numberOfColorBands > 1
    % Convert RGB image to monochrome.
    %grayImage = rgb2gray(img);
    %imshow(grayImage);
    %end
    %imshow(grayImage);
end
------------------------------------------
And this (imageCentroid.m)
------------------------------------------
function [centroidX centroidY]= imageCentroid(img)
% Get all the blob properties.
blobMeasurements = regionprops(double(img),'Centroid');
% Get the centroid.
centroidX = blobMeasurements(1).Centroid(1)
centroidY = blobMeasurements(1).Centroid(2)

 %Get the boundary.
boundaries = bwboundaries(img);
thisBoundary = boundaries{1};
% Get the distances of the boundary pixels from the centroid.
distances = sqrt((thisBoundary(:,1) - centroidX).^2 + (thisBoundary(:,2) - centroidY).^2);
%d=distances;
% Scan the boundary to find the pixel on it that is
% farthest from the centroid.
maxRadius = max(distances);
%myArea = regionprops(double(img),'Area');
%ECD = sqrt(4 * myArea / pi);
%radius=ECD/2;
imshow(img);

% Place a circle at the centroid with the largest radius.
boxX = centroidX - maxRadius;
boxY = centroidY - maxRadius;
rectangle('Position',[boxX, boxY, 2*maxRadius, 2*maxRadius],...
   'Curvature',[1,1], 'EdgeColor', 'b', 'LineWidth', 2);
daspect([1,1,1]);
axis square;

% Place a cross at the centroid.
hold on;
plot(centroidX, centroidY, 'r*', 'MarkerSize', 6);
%caption = sprintf('Done!\nThe maximum radius = %.2f', maxRadius);
%fprintf(1, '%s\n', caption);
%msgbox(caption);
--------------------------------------------------------------------------------------------------------------
here is my problems:
1- the maximum radius is the largest distance from the center (x0,y0) to the farthest point on the boundary (xn,yn), that mean the circle circumference should pass this point and the shape boundary never appear out the circle circumference i.e the shape boundary must fit in the circle.

please see image 0000020, the circle circumference doesn't pass on any point of the shape boundary.

The circle not drawn in image 0000022 and I think the bat shape is changed. These problems also appear in image 0000024.

see also image 0000005, the shape boundary is out the circumference.

I upload my images here: http://drop.io/l1kgosj#
Kindly change the range of the for loop to see the output of my images
 
thanks in advance,
Ibtesam

Subject: help me to compute the distance from boundary points to centroid

From: Ibtesam Saleh

Date: 24 Mar, 2010 06:39:04

Message: 18 of 24

Please solve my problems.

Ibtesam

Subject: help me to compute the distance from boundary points to centroid

From: Ibtesam Saleh

Date: 29 Mar, 2010 18:47:10

Message: 19 of 24

Please help me. I compute the maximum radius of my image, but my image boundary drawn out the circumference, I test my code many times I found that this problem from using canny edge detection, because some points of the image boundary not drawn or not get by the canny edge detection i.e the boundary not connected so the further point not computed<--- this is my guessing I don't know if it is correct or not.

So, I search for another method that may be help me to get my goal, and I found 2 methods which compute the image boundary (bwboundaries AND bwtraceboundary)
I used the first one because I don't know the initial point on the object boundary.

This is my code after using (bwboundaries) method
------------------------------------------------------------------------------------
[B,L] = bwboundaries(img);
    imshow(img);
    hold on
    for k = 1:length(B)
    boundary = B{1};
    plot(boundary(:,2), boundary(:,1), 'r', 'LineWidth',1) %the object boundary drawn %correctly :)
    end
    distances = sqrt((boundary(:,1) - centroidX).^2 + (boundary(:,2) - centroidY).^2);
% Scan the boundary to find the pixel on it that is
% farthest from the centroid.
    maxRadius = max(distances)
------------------------------------------------------------------------------------
Please help me if you can, How can I compute the maximum radius??

Thanks in advance
Ibtesam

Subject: help me to compute the distance from boundary points to centroid

From: joveria

Date: 21 Jun, 2011 10:46:05

Message: 20 of 24

ImageAnalyst <imageanalyst@mailinator.com> wrote in message <4e8b75c1-eff0-4254-b89a-beef4fcc217e@o30g2000yqb.googlegroups.com>...
> Ibtesam Saleh:
> You mean like this:
>
> clc; % Clear the command window.
> close all; % Close all figures (except those of imtool.)
> clear all; % Erase all existing variables.
> workspace; % Make sure the workspace panel is showing.
> % Make demo image.
> grayImage = 50 * ones(400,400);
> % Make white blob;
> grayImage(200:300, 200:300) = 255;
> % subplot(2, 2, 1);
> imshow(grayImage, []);
> set(gcf, 'Position', get(0,'Screensize')); % Maximize figure.
> title('Original Grayscale Image');
>
> % Binarize the image so we can label it.
> binaryImage = grayImage > 128;
>
> % Label each blob so we can make measurements of it
> labeledImage = bwlabel(binaryImage, 8);
>
> % Get all the blob properties.
> blobMeasurements = regionprops(labeledImage, 'Centroid');
> % Get the centroid.
> centroidX = blobMeasurements(1).Centroid(1)
> centroidY = blobMeasurements(1).Centroid(2)
> % Get the boundary.
> boundaries = bwboundaries(binaryImage);
> thisBoundary = boundaries{1};
> % Get the distances of the boundary pixels from the centroid.
> distances = sqrt((thisBoundary(:,1) - centroidX).^2 + (thisBoundary(:,
> 2) - centroidY).^2);
> % Scan the boundary to find the pixel on it that is
> % farthest from the centroid.
> maxRadius = max(distances);
>
> % Place a circle at the centroid with the largest radius.
> boxX = centroidX - maxRadius;
> boxY = centroidY - maxRadius;
> rectangle('Position',[boxX, boxY, 2*maxRadius, 2*maxRadius],...
> 'Curvature',[1,1], 'EdgeColor', 'r', 'LineWidth', 3);
> daspect([1,1,1]);
> axis square;
> % Place a cross at the centroid.
> hold on;
> plot(centroidX, centroidY, 'r+', 'MarkerSize', 15);
> caption = sprintf('Done!\nThe maximum radius = %.2f', maxRadius);
> fprintf(1, '%s\n', caption);
> msgbox(caption);


hi ImageAnalyst
i want to calculate the distance from center to boundary pixels. i searched and found this code of yours but it is for one object. I want to know how can i modify it to calculate distances of many objects present in image as i have tried bit did not get result. Can you tell me?

thanks
jia

Subject: help me to compute the distance from boundary points to centroid

From: joveria

Date: 21 Jun, 2011 17:20:06

Message: 21 of 24

please ImageAnalyst help me.

Thanks
jia

Subject: help me to compute the distance from boundary points to centroid

From: Image Analyst

Date: 22 Jun, 2011 01:22:05

Message: 22 of 24

Distances from what to what? From center to edge? Did you try bwdist? Post an image and then we can talk.

Subject: help me to compute the distance from boundary points to centroid

From: joveria

Date: 23 Jun, 2011 12:05:08

Message: 23 of 24

"Image Analyst" wrote in message <itrg3t$qv8$1@newscl01ah.mathworks.com>...
> Distances from what to what? From center to edge? Did you try bwdist? Post an image and then we can talk.


i have written "distance from center to boundary pixels of object" if you have read and the code which i found of yours was given above my question. i only want to modify it for objects present in image.

Subject: help me to compute the distance from boundary points to centroid

From: Image Analyst

Date: 23 Jun, 2011 12:24:05

Message: 24 of 24

"joveria" wrote in message <itva5k$ki5$1@newscl01ah.mathworks.com>...
> i have written "distance from center to boundary pixels of object" if you have read and the code which i found of yours was given above my question. i only want to modify it for objects present in image.
--------------------------------------------------------
joveria:
You didn't post an image like I asked so I'm unable to help you any further. And I'll be leaving for two weeks today (in a few hours) and will have very rare, if any, participation in this newsgroup as I won't have internet access, so you'll have to rely on the others for help. But I'm sure they'll ask the same things:
1) Did you try bwdist?
2) Where did you post your image?
Good luck

Tags for this Thread

Add a New Tag:

Separated by commas
Ex.: root locus, bode

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

rssFeed for this Thread

Contact us at files@mathworks.com