# Finding the centroid of very irregular image

**URL:** <https://forum.opencv.org/t/finding-the-centroid-of-very-irregular-image/3272>\
**Category:** Uncategorized\
**Created:** [May 11, 2021, 12:45am UTC](https://forum.opencv.org/t/finding-the-centroid-of-very-irregular-image/3272 "2021-05-11T00:45:23Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![evanmc](https://avatars.discourse-cdn.com/v4/letter/e/45deac/32.png) [@evanmc](https://forum.opencv.org/u/evanmc)\
**Post date:** [May 11, 2021, 12:45am UTC](https://forum.opencv.org/t/finding-the-centroid-of-very-irregular-image/3272/1 "2021-05-11T00:45:23Z")

</div>

I’m very new to using opencv, so it’s likely the case that I’m just going about this wrong.

I have several irregularly shaped objects in an image, and need to find the centers an minimum enclosing circle. But, I end up finding centers all along the contours I find.

Any guidance here?

The python snippet I am using…

```
    # Convert to binary image
    ret,thresh = cv.threshold(self._cartooned,127,255,0)
    self.show("binary", thresh)

    # find contours in the binary image
    #im2, contours, hierarchy = cv.findContours(thresh,cv.RETR_TREE,cv.CHAIN_APPROX_SIMPLE)
    contours, hierarchy = cv.findContours(thresh,cv.RETR_TREE,cv.CHAIN_APPROX_SIMPLE)
    for c in contours:
        # calculate moments for each contour
        M = cv.moments(c)

        if M["m00"] != 0:
            cX = int(M["m10"] / M["m00"])
            cY = int(M["m01"] / M["m00"])
        else:
            cX, cY = 0, 0
        # calculate x,y coordinate of center
        # cX = int(M["m10"] / M["m00"])
        # cY = int(M["m01"] / M["m00"])
        cv.circle(self._image, (cX, cY), 5, (255, 255, 255), -1)
        cv.putText(self._image, "centroid", (cX - 25, cY - 25),cv.FONT_HERSHEY_SIMPLEX, 0.5, (255, 255, 255), 2)

    self.show("centers", self._image)
    cv.waitKey()

```

The source image

![binary](https://us1.discourse-cdn.com/flex020/uploads/opencv/original/2X/3/32b23a2dd9216dac56d8318eddd399efb3833e29.jpeg)

I would attach the result, but it looks like I am limited to including only one image.

Thanks,  
-Evan-

---

<div class="post-metadata">

**Author:** ![evanmc](https://avatars.discourse-cdn.com/v4/letter/e/45deac/32.png) [@evanmc](https://forum.opencv.org/u/evanmc)\
**Post date:** [May 11, 2021, 12:46am UTC](https://forum.opencv.org/t/finding-the-centroid-of-very-irregular-image/3272/2 "2021-05-11T00:46:16Z")

</div>

The result:  
 ![centers](https://us1.discourse-cdn.com/flex020/uploads/opencv/original/2X/4/4471bd7a052893896706702c9ed37520ff2eace5.jpeg)

---

<div class="post-metadata">

**Author:** ![crackwitz](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/crackwitz/32/14_2.png) [@crackwitz](https://forum.opencv.org/u/crackwitz)\
**Post date:** [May 11, 2021, 10:42am UTC](https://forum.opencv.org/t/finding-the-centroid-of-very-irregular-image/3272/3 "2021-05-11T10:42:57Z")

</div>

please post original full-resolution data, not plots.

---

<div class="post-metadata">

**Author:** ![berak](https://avatars.discourse-cdn.com/v4/letter/b/85f322/32.png) [@berak](https://forum.opencv.org/u/berak)\
**Post date:** [May 11, 2021, 11:00am UTC](https://forum.opencv.org/t/finding-the-centroid-of-very-irregular-image/3272/4 "2021-05-11T11:00:41Z")

</div>

you probably have many more contours, than you expected, so you would need to filter the contours for size/area (and keep only the largest few) before trying to find the center

---

<div class="post-metadata">

**Author:** ![evanmc](https://avatars.discourse-cdn.com/v4/letter/e/45deac/32.png) [@evanmc](https://forum.opencv.org/u/evanmc)\
**Post date:** [May 11, 2021, 1:45pm UTC](https://forum.opencv.org/t/finding-the-centroid-of-very-irregular-image/3272/5 "2021-05-11T13:45:31Z")

</div>

The binary image I’m working on:

 ![binary](https://us1.discourse-cdn.com/flex020/uploads/opencv/original/2X/6/68b7e0d41d500894675dc326f05cd3149906e071.jpeg)

---

<div class="post-metadata">

**Author:** ![evanmc](https://avatars.discourse-cdn.com/v4/letter/e/45deac/32.png) [@evanmc](https://forum.opencv.org/u/evanmc)\
**Post date:** [May 11, 2021, 1:46pm UTC](https://forum.opencv.org/t/finding-the-centroid-of-very-irregular-image/3272/6 "2021-05-11T13:46:21Z")

</div>

The source

 ![temporary](https://us1.discourse-cdn.com/flex020/uploads/opencv/original/2X/6/6c64bd22f05e1f73f608174c5db7f41622085a7c.jpeg)

---

<div class="post-metadata">

**Author:** ![evanmc](https://avatars.discourse-cdn.com/v4/letter/e/45deac/32.png) [@evanmc](https://forum.opencv.org/u/evanmc)\
**Post date:** [May 11, 2021, 1:48pm UTC](https://forum.opencv.org/t/finding-the-centroid-of-very-irregular-image/3272/7 "2021-05-11T13:48:34Z")

</div>

Unfortunately, I don’t know in advance how large the objects are or how many of them there are, so it seems like filtering them based on area would be a bit of a challenge here.

---

<div class="post-metadata">

**Author:** ![berak](https://avatars.discourse-cdn.com/v4/letter/b/85f322/32.png) [@berak](https://forum.opencv.org/u/berak)\
**Post date:** [May 11, 2021, 1:52pm UTC](https://forum.opencv.org/t/finding-the-centroid-of-very-irregular-image/3272/8 "2021-05-11T13:52:25Z")

</div>

[dilation / erosion](https://docs.opencv.org/master/d9/d61/tutorial_py_morphological_ops.html)

get rid of the small, noisy (artificial) blobs in your binary image, before finding contours

---

<div class="post-metadata">

**Author:** ![evanmc](https://avatars.discourse-cdn.com/v4/letter/e/45deac/32.png) [@evanmc](https://forum.opencv.org/u/evanmc)\
**Post date:** [May 11, 2021, 2:55pm UTC](https://forum.opencv.org/t/finding-the-centroid-of-very-irregular-image/3272/9 "2021-05-11T14:55:57Z")

</div>

Thanks – works much better now.

---

<div class="post-metadata">

**Author:** ![crackwitz](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/crackwitz/32/14_2.png) [@crackwitz](https://forum.opencv.org/u/crackwitz)\
**Post date:** [May 11, 2021, 4:46pm UTC](https://forum.opencv.org/t/finding-the-centroid-of-very-irregular-image/3272/10 "2021-05-11T16:46:02Z")

</div>

your threshold level is rubbish (127 is usually not!), or the input to `threshold` is rubbish.

see this? those squares are from jpeg compression (of the input to a threshold), but they should usually not be _this_ noticeable.

![image](https://us1.discourse-cdn.com/flex020/uploads/opencv/original/2X/6/6e0bb0fbfbccbd5783941f99879db63d9e143767.png)

what _exactly_ is the data in `self._cartooned`? show it to us.
