# How to calculate width of each worm in the image?

**URL:** <https://forum.opencv.org/t/how-to-calculate-width-of-each-worm-in-the-image/999>\
**Category:** Python\
**Tags:** imgproc, ximgproc, contrib\
**Created:** [January 21, 2021, 12:48am UTC](https://forum.opencv.org/t/how-to-calculate-width-of-each-worm-in-the-image/999 "2021-01-21T00:48:27Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![amaze](https://avatars.discourse-cdn.com/v4/letter/a/13edae/32.png) [@amaze](https://forum.opencv.org/u/amaze)\
**Post date:** [January 21, 2021, 12:48am UTC](https://forum.opencv.org/t/how-to-calculate-width-of-each-worm-in-the-image/999/1 "2021-01-21T00:48:27Z")

</div>

Code

```auto
_, binary = cv2.threshold(gray, 225, 255, cv2.THRESH_BINARY_INV)

#find the contours from the thresholded image
contours, hierarchy = cv2.findContours(binary, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)[-2:]

#Calculate Maximum Width of each worms using Distance Transform

distance = cv2.distanceTransform(binary, cv2.DIST_L2,5)
print('gray',binary.shape)
minv,maxv,minp,maxp = cv2.minMaxLoc(distance)
print(distance)
print(maxv*2, maxp)# (half)width*2, pos
print('Min',minv*2,minp)
draw = cv2.cvtColor(binary, cv2.COLOR_GRAY2BGR)
cv2.line(draw, (0,maxp[1]), (binary.shape[1],maxp[1]), (250,0,0),2, -1)
cv2.line(draw, (0,minp[1]), (binary.shape[1],minp[1]), (250,0,0),2, -1)
plt.figure(figsize=(15, 15))
plt.imshow(draw)
plt.show()

```

Maximum distance: 17.575199127197266 (1144, 787)

Minimum distance: 0.0 (0, 0)

Output Image

 ![Output1](https://us1.discourse-cdn.com/flex020/uploads/opencv/original/1X/7d7b173771fcd653a240c6ae1a3a0c64c0714eee.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:** [January 21, 2021, 1:52am UTC](https://forum.opencv.org/t/how-to-calculate-width-of-each-worm-in-the-image/999/2 "2021-01-21T01:52:40Z")

</div>

related

> <https://stackoverflow.com/questions/65803555/is-there-any-way-to-compute-width-or-length-of-worms-using-medial-axis-in-scikit>

---

<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:** [January 21, 2021, 1:54am UTC](https://forum.opencv.org/t/how-to-calculate-width-of-each-worm-in-the-image/999/3 "2021-01-21T01:54:08Z")

</div>

your own code should have answered your question. do you agree?

---

<div class="post-metadata">

**Author:** ![amaze](https://avatars.discourse-cdn.com/v4/letter/a/13edae/32.png) [@amaze](https://forum.opencv.org/u/amaze)\
**Post date:** [January 21, 2021, 2:19am UTC](https://forum.opencv.org/t/how-to-calculate-width-of-each-worm-in-the-image/999/4 "2021-01-21T02:19:11Z")

</div>

Hi @crackwitz , I did not get accurate cross section width and I need width of each worms.

---

<div class="post-metadata">

**Author:** ![laurent.berger](https://avatars.discourse-cdn.com/v4/letter/l/ec9cab/32.png) [@laurent.berger](https://forum.opencv.org/u/laurent.berger)\
**Post date:** [January 21, 2021, 8:28am UTC](https://forum.opencv.org/t/how-to-calculate-width-of-each-worm-in-the-image/999/5 "2021-01-21T08:28:22Z")

</div>

You can use [thinining algorithm](https://docs.opencv.org/master/df/d2d/group__ximgproc.html#ga37002c6ca80c978edb6ead5d6b39740c) in opencv\_contrib same problem [here](https://answers.opencv.org/question/174300/how-to-determine-length-of-spline-eg-snake/)

---

<div class="post-metadata">

**Author:** ![amaze](https://avatars.discourse-cdn.com/v4/letter/a/13edae/32.png) [@amaze](https://forum.opencv.org/u/amaze)\
**Post date:** [January 21, 2021, 11:48pm UTC](https://forum.opencv.org/t/how-to-calculate-width-of-each-worm-in-the-image/999/6 "2021-01-21T23:48:25Z")

</div>

Thanks @laurent.berger for your solution, I have solved this problem with my own code. I am thinking of optimized approach and will try your too.

---

<div class="post-metadata">

**Author:** ![himanshukumar3199](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/himanshukumar3199/32/3848_2.png) [@himanshukumar3199](https://forum.opencv.org/u/himanshukumar3199)\
**Post date:** [April 8, 2022, 11:15am UTC](https://forum.opencv.org/t/how-to-calculate-width-of-each-worm-in-the-image/999/7 "2022-04-08T11:15:54Z")

</div>

can you tell what does this maximum distance 17.575199… represents, i mean is it in pixels or what? I want to report maximum width of surface crack using similar algorithm but what will this value obtained signify.
