0

So I have the following image:

Binary Image - Shapes

Now I wish to find the center point of each individual disconnected shape. I would also like to find the width and height if possible.

I'm using Java and the java.awt.image package but even just a general solution to this problem would help.

My own idea is to:

  • Iterate through and find the first white pixel.
  • Perform BFS from that pixel marking the white pixels it finds with the number 1.
  • Once finished, continue iterating through the image stopping at the first white pixel with no assigned/marked number.
  • Once it finds this, perform BFS and mark all pixels found with the number 2.
  • Lather, rinse and repeat. I mean... continue doing this until end of the image.
  • The number you are up to is the number of distinct shapes.

BUT this seems a bit intensive and i would then have to find the center, width and height. Is there a simpler way? Any ideas?

Z-Mehn
  • 268
  • 1
  • 2
  • 12
  • 3
    Do you mean connected component labeling? There's a good article on Wikipedia, including a pseudocode algorithm: http://en.wikipedia.org/wiki/Connected-component_labeling – Niki Apr 15 '13 at 11:40
  • 1
    Ah okay, pretty much. Its hard to find these things unless you know what to call them. Doesn't exactly help find the center or size though but were getting there. – Z-Mehn Apr 15 '13 at 11:51
  • 1
    You can find the centre of the component by looking for its centroid (http://en.wikipedia.org/wiki/Image_moment). For it size, you can either count the number of white pixels in the component, or if you prefer a bounding box, find the top-most, left-most, bottom-most and right-most white pixels and construct a rectangle with this information. – Zaphod Apr 15 '13 at 12:55

1 Answers1

0

I dont know if it is a good idea but maybe this will help you:

I would send randomWalks from edges and divide the image into smaller boxes and continue sending randomWalks from smaller boxes until some point(I dont know what will be that point but you will know that here there is no smaller box that has more than 1 element)

-If a randomWalk cut another randomWalk without seeing any whitecolor, then, there is a new bow. For example, in my image, let say 1st randomWalk(rw1) start from (0,200) and rw2 start from (100,0) and they hit in (100,200) without touching any whitecolor, then you have a box (0,0,100,200). Add this to your arraylist of myBoxes.

-Now come to my mind, you can do something like that: if sum of colors of pixels in a box is equal to zero(or is less than a small number), then there is no image in that box. So you can add it to another list, lets call it myEmptyBoxes.

-Next thing, when you are sure that there is only one element in every box, then sum color of every box and if two boxes are equal or really close, then they may be same image. if not automatically they are not same image, no need to work on that two image/box.

-I couldnt think of the next step, that is all come to my mind.

-Also, I offer you to use opencv which is a good library for that kind of problems

Hope this will help

image

Community
  • 1
  • 1
smttsp
  • 4,011
  • 3
  • 33
  • 62