Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Including an area into a circle

Subject: Including an area into a circle

From: Spulber

Date: 16 Dec, 2010 22:21:05

Message: 1 of 21

Hello there,
I want to analyze an image, and I tried many codes without achieving my goal. What I have to do, is to find some errors from a picture. The errors looks like blob, but the margins are not compact, fact that gives me the headaches. I would like to include the error into a cercle, and i tried some algorithms but none of them worked for me.
Thank you in advance for your help and if I find out how to upload an image to illustrate what I mean.
Regards.

Subject: Including an area into a circle

From: Sean de

Date: 16 Dec, 2010 22:38:07

Message: 2 of 21

"Spulber " <remove.this@yahoo.com> wrote in message <iee3ch$173$1@fred.mathworks.com>...
> Hello there,
> I want to analyze an image, and I tried many codes without achieving my goal. What I have to do, is to find some errors from a picture. The errors looks like blob, but the margins are not compact, fact that gives me the headaches. I would like to include the error into a cercle, and i tried some algorithms but none of them worked for me.
> Thank you in advance for your help and if I find out how to upload an image to illustrate what I mean.
> Regards.

Post you image on a free 3rd party file sharing site. Preferably with the blobs and margins you speak of pronounced so we can tell _exactly_ what you want.

Subject: Including an area into a circle

From: Spulber

Date: 16 Dec, 2010 23:03:05

Message: 3 of 21

Here is the picture, thanks for your fast reply. I think that you will recognise the errors, in this picture are 4 of them.

Subject: Including an area into a circle

From: Spulber

Date: 16 Dec, 2010 23:05:23

Message: 4 of 21

Hi, this is the picture as you can see. http://picasaweb.google.com/sergiuspulber96/ShapeRecognition?authkey=Gv1sRgCOqSsNmV2NjX-AE#5551417838610969058 and the errors are the 4 blobs in the middle.
Thanks for your fast reply

Subject: Including an area into a circle

From: ImageAnalyst

Date: 17 Dec, 2010 00:42:14

Message: 5 of 21

I think you need to look at texture segmentation. For example, look
at this paper on object classification by using random forests:

http://www.robots.ox.ac.uk/~vgg/publications/papers/schroff08.pdf

Subject: Including an area into a circle

From: ImageAnalyst

Date: 17 Dec, 2010 00:58:38

Message: 6 of 21

On Dec 16, 7:42 pm, ImageAnalyst <imageanal...@mailinator.com> wrote:
> I think you need to look at texture segmentation.  For example, look
> at this paper on object classification by using random forests:
>
> http://www.robots.ox.ac.uk/~vgg/publications/papers/schroff08.pdf

Look at the latter half of the refereed papers on this page:
http://wws2.uncc.edu/tpw/papers/papers.html
for more texture segmentation methods.

Subject: Including an area into a circle

From: Spulber

Date: 17 Dec, 2010 01:16:20

Message: 7 of 21

Hi, thank you very much for the references. I will investigate them tomorrow morning.
Looks like it will work for me.

Subject: Including an area into a circle

From: Spulber

Date: 19 Dec, 2010 21:01:07

Message: 8 of 21

Hi, I managed to get here with my project http://picasaweb.google.com/sergiuspulber96/ShapeRecognition?authkey=Gv1sRgCOqSsNmV2NjX-AE#5552498001431693538 . Now, what I really did not find is a way to calculate the coordonates of a pixel surrounded by other pixels with the same colour as itself. In my case, I am interested in the gray pixels. I want to find the coordinates where I can draw circles with a radius of 5 let's say, without covering the errors, or the blobs painted with other colours than gray.
Thanks

Subject: Including an area into a circle

From: ImageAnalyst

Date: 19 Dec, 2010 22:26:02

Message: 9 of 21

On Dec 19, 4:01 pm, "Spulber " <remove.t...@yahoo.com> wrote:
> Hi, I managed to get here with my projecthttp://picasaweb.google.com/sergiuspulber96/ShapeRecognition?authkey=.... Now, what I really did not find is a way to calculate the coordonates of a pixel surrounded by other pixels with the same colour as itself. In my case, I am interested in the gray pixels. I want to find the coordinates where I can draw circles with a radius of 5 let's say, without covering the errors, or the blobs painted with other colours than gray.
> Thanks
-----------------------------------------------------------------------------------------------------------------
Simple. You get a binary image of the gray stuff. Then call bwdist()
on that. Then threshold at 5 to get a new binary image of places
where you can draw a circle of radius 5 or larger. Then you can pass
that into find() or bwlabel and regionprops to get the row,column
coordinates of those places.

Subject: Including an area into a circle

From: Spulber

Date: 20 Dec, 2010 01:53:04

Message: 10 of 21


> -----------------------------------------------------------------------------------------------------------------
> Simple. You get a binary image of the gray stuff. Then call bwdist()
> on that. Then threshold at 5 to get a new binary image of places
> where you can draw a circle of radius 5 or larger. Then you can pass
> that into find() or bwlabel and regionprops to get the row,column
> coordinates of those places.

 I searched on matlab search for threshold but I could not find something related with what you have said, to threshold at 5. Because I am at the begin with Matlab, I have to read alot before I write few lines of code and I will ask for your help if i will have any other questions. Thanks

Subject: Including an area into a circle

From: ImageAnalyst

Date: 20 Dec, 2010 04:29:45

Message: 11 of 21

D = bwdist(BW) computes the Euclidean distance transform of the binary
image BW. For each pixel in BW, the distance transform assigns a
number that is the distance between that pixel and the nearest nonzero
pixel of BW. bwdist uses the Euclidean distance metric by default.

So any pixel in the EDT that is greater than 5 will be greater than 5
pixels away from an object and thus you could fit a circle of radius 5
at that location. Thresholding just tells you what pixels have that
condition or not

% Calculate the EDT.
EDT_image = bwdist(binaryImage);

% Find which pixels are 5 or more away.
fartherThan5Away = EDT_image >= 5;

% Determine the row and column of
% those pixels that are farther than 5 away.
[rows cols] = find(fartherThan5Away);

Subject: Including an area into a circle

From: Spulber

Date: 20 Dec, 2010 20:21:05

Message: 12 of 21

Thank you very much man, the lines are working very well. I am trying for a long time already to get to this point.

Subject: Including an area into a circle

From: Spulber

Date: 21 Dec, 2010 00:08:05

Message: 13 of 21

Hi, I tried to draw a circle on top of my current figure, so I can see where the circles are putted, but it was an impossible task to do. The closest algorythm that I found is this one http://www.mathworks.com/matlabcentral/fileexchange/23121-circle-on-image and I tried to figure out how to remove the part where the inputs from the mouse are read and I get no results. Is there an easier way to do that?

Subject: Including an area into a circle

From: ImageAnalyst

Date: 21 Dec, 2010 01:02:44

Message: 14 of 21

Chances are that you'll have thousands of pixels where a circle of
radius 5 would be located. Thus you'd have thousands of circles,
every circle would be overlapped by many, many other circles. If you
were to write all of those circles into the image, the end result
would be the same as if you did a dilation of your binary image by 5.
So...look into imdilate().

Subject: Including an area into a circle

From: ImageAnalyst

Date: 21 Dec, 2010 01:07:03

Message: 15 of 21

Re-reading back over your old posts, I think you can just take your
gray stuff and erode it by 5 pixels to get pixels where a circle of
radius 5 could be placed without touching any other regions. Look
into imerode(). Now, explain why you want to do that in the first
place.

Can you give the code to go from your original picture to the
segmented picture?

Subject: Including an area into a circle

From: Spulber

Date: 21 Dec, 2010 22:10:25

Message: 16 of 21

> Re-reading back over your old posts, I think you can just take your
> gray stuff and erode it by 5 pixels to get pixels where a circle of
> radius 5 could be placed without touching any other regions. Look
> into imerode(). Now, explain why you want to do that in the first
> place.
>
> Can you give the code to go from your original picture to the
> segmented picture?
----------------------------------------------------------------------------------------------------------
Thanks again for your fast reply.
What I want to do for this project is to place an object which will creat a mark, like a circle of 5 mm diameter, into an area where there are no errors. The errors are the blobs inside the main circle that I posted here (4 in the actual picture). For that I need to calculate an area where it is ok to place that object which will create the circle. The teacher asked me to make the algorithm in such a manner that creates some extra circles (let's say a maximum of 5 extra circles), although the ideal would be to draw all the areas where a future circle can be placed.
But before I start to research how to command a robotic arm to place the object into the center of the future circle, I must find the coordinates of the circles, which I did with your help and to draw the circles on top of the picture that I have.

I = imread('proba41.jpg');
BW = im2bw(I, graythresh(I));
BW = medfilt2(BW, [5 5]);
EDT_image = bwdist(BW);
fartherThan5Away = EDT_image >= 5;
[rows cols] = find(fartherThan5Away);
rc = [rows cols]
imshow(BW)
axis on

This is the code that I am working with after you replyed to me and I have a bounch of other m files related with this one, trying what I have found before and with what you have suggested me to try.
The code that I got to before I asked for help here is

I = imread('proba41.jpg'); % se citeste o imagine
BW = im2bw(I, graythresh(I)); %folosind
BW = medfilt2(BW, [5 5]); %se calculeaza o medie cu pixelii vecini
imshow(BW)
L = bwlabel(BW,4);
[r,c] = find(L==0);
rc = [r c]
[B,L] = bwboundaries(BW,'noholes');
imshow(label2rgb(L, @jet, [.5 .5 .5]))
grid on
 hold on
for k = 1:length(B)
    boundary = B{k};
    plot(boundary(:,2), boundary(:,1), 'w', 'LineWidth', 2)
end
imshow(BW)
 axis on

The rc array that I got using your algorithm, the first one posted here, I tried to find a way to split it in smaller arrays, and to make somehow to group pixels so the circles I draw wouldn't touch other circles. I am working on this for 2 months already, and it is very hard because I only was told what to do, and nothing about how to do it, or any hints. What you told me to do probably saved me few extra weeks of attempts.
Thank you!

Subject: Including an area into a circle

From: Spulber

Date: 3 Jan, 2011 23:47:05

Message: 17 of 21

"Spulber " <remove.this@yahoo.com> wrote in message <ier8kh$t0u$1@fred.mathworks.com>...
> > Re-reading back over your old posts, I think you can just take your
> > gray stuff and erode it by 5 pixels to get pixels where a circle of
> > radius 5 could be placed without touching any other regions. Look
> > into imerode(). Now, explain why you want to do that in the first
> > place.
> >
> > Can you give the code to go from your original picture to the
> > segmented picture?
> ----------------------------------------------------------------------------------------------------------
> Thanks again for your fast reply.
> What I want to do for this project is to place an object which will creat a mark, like a circle of 5 mm diameter, into an area where there are no errors. The errors are the blobs inside the main circle that I posted here (4 in the actual picture). For that I need to calculate an area where it is ok to place that object which will create the circle. The teacher asked me to make the algorithm in such a manner that creates some extra circles (let's say a maximum of 5 extra circles), although the ideal would be to draw all the areas where a future circle can be placed.
> But before I start to research how to command a robotic arm to place the object into the center of the future circle, I must find the coordinates of the circles, which I did with your help and to draw the circles on top of the picture that I have.
>
> I = imread('proba41.jpg');
> BW = im2bw(I, graythresh(I));
> BW = medfilt2(BW, [5 5]);
> EDT_image = bwdist(BW);
> fartherThan5Away = EDT_image >= 5;
> [rows cols] = find(fartherThan5Away);
> rc = [rows cols]
> imshow(BW)
> axis on
>
> This is the code that I am working with after you replyed to me and I have a bounch of other m files related with this one, trying what I have found before and with what you have suggested me to try.
> The code that I got to before I asked for help here is
>
> I = imread('proba41.jpg'); % se citeste o imagine
> BW = im2bw(I, graythresh(I)); %folosind
> BW = medfilt2(BW, [5 5]); %se calculeaza o medie cu pixelii vecini
> imshow(BW)
> L = bwlabel(BW,4);
> [r,c] = find(L==0);
> rc = [r c]
> [B,L] = bwboundaries(BW,'noholes');
> imshow(label2rgb(L, @jet, [.5 .5 .5]))
> grid on
> hold on
> for k = 1:length(B)
> boundary = B{k};
> plot(boundary(:,2), boundary(:,1), 'w', 'LineWidth', 2)
> end
> imshow(BW)
> axis on
>
> The rc array that I got using your algorithm, the first one posted here, I tried to find a way to split it in smaller arrays, and to make somehow to group pixels so the circles I draw wouldn't touch other circles. I am working on this for 2 months already, and it is very hard because I only was told what to do, and nothing about how to do it, or any hints. What you told me to do probably saved me few extra weeks of attempts.
> Thank you!

Subject: Including an area into a circle

From: Spulber

Date: 19 Jan, 2011 20:07:20

Message: 18 of 21

Hello, I have been waiting for a new response, and I have been trying to figure it out myself but I ended up with an insucces.
Can anyone please tell me how to generate a number of circles without them being on top of each other, or how to take each line from a vector?
What I have been trying to do is that:
-I have been trying to take each line and to compare it with the next one, so if the difference between one of the elements is 1, that means they are neighbours.
Let's take an example:
rc=[ 100 67; 100 68; 100 74; ...]
My question is how to put each value into a variable, or something like this?
I am open minded, because I realise that there might be an easier way, but I am not aware of it.
Thank you!

Subject: Including an area into a circle

From: ImageAnalyst

Date: 19 Jan, 2011 20:48:04

Message: 19 of 21

On Jan 19, 3:07 pm, "Spulber " <remove.t...@yahoo.com> wrote:
> Hello, I have been waiting for a new response, and I have been trying to figure it out myself but I ended up with an insucces.
> Can anyone please tell me how to generate a number of circles without them being on top of each other, or how to take each line from a vector?
> What I have been trying to do is that:
> -I have been trying to take each line and to compare it with the next one, so if the difference between one of the elements is 1, that means they are neighbours.
> Let's take an example:
> rc=[ 100 67; 100 68; 100 74; ...]
> My question is how to put each value into a variable, or something like this?
> I am open minded, because I realise that there might be an easier way, but I am not aware of it.
> Thank you!

-----------------------------------------------------------
Spulber :
I didn't understand your rc example. Each number IS in a variable -
it's in the rc variable.

If you want to know the difference between pixel values, you can use
imfilter, conv2, or diff.
If you want to know which pixels are connected to each other
(neighbors), you can use bwlabel or bwconncomp.
ImageAnalyst

Subject: Including an area into a circle

From: Spulber

Date: 19 Jan, 2011 22:50:30

Message: 20 of 21

> Spulber :
> I didn't understand your rc example. Each number IS in a variable -
> it's in the rc variable.
>
> If you want to know the difference between pixel values, you can use
> imfilter, conv2, or diff.
> If you want to know which pixels are connected to each other
> (neighbors), you can use bwlabel or bwconncomp.
> ImageAnalyst
-----------------------------------------------------------
Hi, thanks again for your fast reply. I want to draw circles with radius of 3 mm, but on the same time I don't want them be one on top of the other circles.
If I use bwlabel and I get the a big area, I wouldn't know how to draw there the optim number of circles. Plus of that, I want to know the center of the circles, so I can place later an object exactly on that location.

Subject: Including an area into a circle

From: ImageAnalyst

Date: 20 Jan, 2011 02:43:24

Message: 21 of 21

Here's a circle drawing demo I have lying around. See if you can
adapt it to draw what you want. It allows overlapping circles so
you'll have to do a check before plopping down a circle to make sure
that won't happen if you don't want that. But that shouldn't be very
hard for you to do.

% IMPORTANT: The newsreader may break long lines into multiple lines.
% Be sure to join any long lines that got split into multiple single
lines.
% These can be found by the red lines on the left side of your
% text editor, which indicate syntax errors, or else just run the
% code and it will stop at the split lines with an error.


% M-file to place multiple small circles inside a big circle.
% Clean up
close all;
clc;
% Initialize some parameters.
numberOfSmallCircles = 25; % Number of small circles
smallCircleOutsideValue = 0.2;
smallCircleInsideValue = 0.8;
smallCircleRadius = 25; % small circle radius
bigImageWidth = 500;
bigImageHeight = 500; % square area 0f 500*500
bigCircleRadius = 250; % big circle radius


% Initialize an image to hold one single big circle.
bigCircleImage = zeros(bigImageHeight, bigImageWidth);
[x, y] = meshgrid(1:bigImageWidth, 1:bigImageHeight);
bigCircleImage((x - bigImageWidth/2).^2 + (y - bigImageHeight/2).^2 <=
bigCircleRadius.^2) = 1;
% Display it in the upper left plot.
subplot(3,2,1);
imshow(bigCircleImage, []);
title('Big Circle Mask');
set(gcf, 'Position', get(0,'Screensize')); % Maximize figure.

% Initialize an image to hold one single small circle.
smallCircleImage = zeros(2*smallCircleRadius, 2*smallCircleRadius);
[x, y] = meshgrid(1:smallCircleRadius*2, 1:smallCircleRadius*2);
singleCircleImage = zeros(2*smallCircleRadius, 2*smallCircleRadius);
singleCircleImage((x - smallCircleRadius).^2 + (y -
smallCircleRadius).^2 <= smallCircleRadius.^2) =
smallCircleInsideValue;
% Display it in the upper right plot.
subplot(3,2,2);
imshow(singleCircleImage, []);
title('Single Small Circle (scaled to fit)');


singleWidth = size(singleCircleImage, 2);
singleHeight = size(singleCircleImage, 1);
% Get random coordinates in the big image where
% we will place the upper left corner of the small circle.
widthThatWillFit = bigImageWidth - 2 * smallCircleRadius;
heightThatWillFit = bigImageHeight - 2 * smallCircleRadius;
smallUpperLeftX = widthThatWillFit * rand(numberOfSmallCircles, 1);
smallUpperLeftY = heightThatWillFit * rand(numberOfSmallCircles, 1);
% Initialize an output image to hold many small overlapping circles.
manySmallCircles = zeros(bigImageHeight, bigImageWidth);
% Place the small circles one by one.
for k = 1 : numberOfSmallCircles
        % Find the square in the big image where we're going to add a
small circle.
        x1 = int16(smallUpperLeftX(k));
        y1 = int16(smallUpperLeftY(k));
        x2 = int16(x1 + singleWidth - 1);
        y2 = int16(y1 + singleHeight - 1);
        % Add in one small circle to the existing big image.
        manySmallCircles(y1:y2, x1:x2) = manySmallCircles(y1:y2,
x1:x2) + singleCircleImage;
end
% Make outside the circles the outside color.
manySmallCircles(manySmallCircles == 0) = smallCircleOutsideValue;
% Display it in the lower left plot.
subplot(3,2,3);
imshow(manySmallCircles);
title('Many Small Overlapping Circles');


% Multiply the big circle mask by the many small circles image to
clip
% those small circles that lie outside the big circle.
maskedByBigCircle = bigCircleImage .* manySmallCircles;
% Display it in the lower right plot.
subplot(3,2,4);
imshow(maskedByBigCircle);
title('Many Small Circles Masked by Big Circle');


% Take the histogram and display it in the bottom row.
subplot(3,2,5);
hist(maskedByBigCircle(:));

Tags for this Thread

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.

Contact us