# [R] Calculating diameters of cirkels in a picture.

Moshe Olshansky m_olshansky at yahoo.com
Tue Aug 28 02:02:58 CEST 2007

```Hi Bart,

Let's assume that you situation was simpler - you have
a BW (Black and White) image containing circles (in
white) and you need to find the diameter of each
circle (and of course to know how many circles you
have). This can be done with labeling of connected
components. You say that two pixels are neighbors if
they have common edge (4-connectivity) or at least a
common vertex (8-connectivity). So now you can treat
your image (white pixels) as a graph (with edges
connecting any two neighbors). Then each connected
component of that graph corresponds to a circle. There
exists a well know algorithm to do this. It takes the
original BW image (where every image pixel has the
value of 1 and background pixel the value of 0) and
produces an image where every background pixel still
has the value of 0, every pixel of the first connected
component has the value of 1, every pixel of the
second connected component has the value of 2, etc.
So no you can process each connected component (circle
Basically this is all you need. You can either count
the number of pixels having the value of k to find the
area (and then the diameter) or just take (maximal x
value) - (minimal x value) + 1.
In your case it can happen that after you convert your
image into BW image some circles will have holes
inside with some small objects inside these holes, and
you do not want to consider these small objects as
additional circles. So I thought of using
morphological closing to get rid of small holes, but
as I wrote in the following note you do not need this.
When you get the BW image take the complimentary one
(i.e. background pixels have the value of 1 and image
pixels the value of 0). Label the connected components
of the background. Only one of them is real background
- all others are inside circles. Real background
touches the image boundaries. Now go to the original
BW image and give all the pixels outside the "real"
background the value of 1. Now all your circles are
full (no holes) and you can proceed as above.

Best regards,

Moshe.

--- Bartjoosen <bartjoosen at hotmail.com> wrote:

>
> Hi All,
>
> I really like to thank you for the answers, while I
> was searching for some
> edge detection and clustering algorithms, Moshe came
> with a simple but
> effective solution: use the area to find the
> diameter!
>
> But I tried Moshe's solution, but I couldn't figure
> out what you mean with
> morphological closing and the labeling to split the
> images.
> Could you please clarify this a bit?
>
>
>
> Bart
>
>
> Moshe Olshansky-2 wrote:
> >
> > Hi Bart,
> >
> > One more comment:
> >
> > You do not really need the morphological closing
> to
> > close the "holes" inside the circles. Another
> > possibility is to reverse the black-and-withe
> picture,
> > i.e. make the holes and background be 1 and the
> > circles 0, label the connected components and then
> > only the component which touches the boundaries is
> the
> > background while all other components are "holes"
> and
> > you can make them white (1) in the original
> > black-and-white image.
> >
> > --- Moshe Olshansky <m_olshansky at yahoo.com> wrote:
> >
> >> Hi Bart,
> >>
> >> I have never used image processing software in R
> (I
> >> was doing this with Matlab), but here is what I
> >> would
> >> have done algorithmically:
> >> 1) convert the picture to gray-scale
> >> 2) find a threshold value which separates the
> >> circles
> >> from the background and convert your image to
> black
> >> and white
> >> 3) if the circles are far apart use morphological
> >> closing to fill in small holes inside the circles
> >> (may
> >> be do this several times)
> >> 4) use labeling to split the image into connected
> >> components
> >> 5) for each connected component get it's area
> (the
> >> number of pixels) and use the formula S = Pi*R^2
> to
> >> find the approximate radii.
> >>
> >> Regards,
> >>
> >> Moshe.
> >>
> >> --- Julian Burgos <jmburgos at u.washington.edu>
> wrote:
> >>
> >> > Hi Bart,
> >> >
> >> > If you only have 36 circles, the fastest way
> would
> >> > be to use some image
> >> > processing software and measure the circles "by
> >> > hand".  One option is to
> >> >
> >> > http://rsb.info.nih.gov/ij/
> >> >
> >> > Julian
> >> >
> >> > Bart Joosen wrote:
> >> > > Hi,
> >> > >
> >> > > Maybe this is more a programming questions
> than
> >> a
> >> > specific R-project question, but maybe there is
> >> > someone who can point me in the right
> direction.
> >> > >
> >> > > I have a picture of cirkels which I took with
> a
> >> > digital camera.
> >> > > Now I want to use the diameter of the cirkels
> on
> >> > the picture for analysis in R.
> >> > > I can use pixmap to import the picture, but
> how
> >> do
> >> > I find the outside cirkels and calculate the
> >> > diameter?
> >> > > I pointed out that I can use the edci
> package,
> >> but
> >> > then I need to preprocess the data to reduce
> the
> >> > points, otherwise it takes a long time, and my
> >> > computer crashes.
> >> > >
> >> > > If you want to see such a picture, I cropped
> a
> >> > larger one, and highlighted the cirkel which is
> of
> >> > interest.
> >> > > In a real world, this is a plate with 36
> >> cirkels,
> >> > which all should be measured.
> >> > >
> www.users.skynet.be/fa244930/fotos/outlined.jpg
> >> > >
> >> > >
> >> > > Thanks for your time
> >> > >
> >> > > Bart
> >> > > 	[[alternative HTML version deleted]]
> >> > >
> >> > >
> ______________________________________________
> >> > > R-help at stat.math.ethz.ch mailing list
> >> > > https://stat.ethz.ch/mailman/listinfo/r-help
> >> > http://www.R-project.org/posting-guide.html
> >> > > and provide commented, minimal,
> self-contained,
> >> > reproducible code.
> >> > >
> >> > >
> >> >
> >> > ______________________________________________
> >> > R-help at stat.math.ethz.ch mailing list
> >> > https://stat.ethz.ch/mailman/listinfo/r-help
> >> > http://www.R-project.org/posting-guide.html
> >> > and provide commented, minimal, self-contained,
> >> > reproducible code.
> >> >
> >>
> >> ______________________________________________
> >> R-help at stat.math.ethz.ch mailing list
> >> https://stat.ethz.ch/mailman/listinfo/r-help
> >> http://www.R-project.org/posting-guide.html
> >> and provide commented, minimal, self-contained,
> >> reproducible code.
> >>
> >
> > ______________________________________________
> > R-help at stat.math.ethz.ch mailing list
> > https://stat.ethz.ch/mailman/listinfo/r-help
> > http://www.R-project.org/posting-guide.html
> > and provide commented, minimal, self-contained,
> reproducible code.
> >
> >
>
> --
> View this message in context:
>
http://www.nabble.com/Calculating-diameters-of-cirkels-in-a-picture.-tf4319669.html#a12343143
> Sent from the R help mailing list archive at
> Nabble.com.
>
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained,
> reproducible code.
>

```