# Calculate shrimp length by image processing

**URL:** <https://forum.opencv.org/t/calculate-shrimp-length-by-image-processing/17132>\
**Category:** Python\
**Tags:** imgproc\
**Created:** [April 3, 2024, 6:01am UTC](https://forum.opencv.org/t/calculate-shrimp-length-by-image-processing/17132 "2024-04-03T06:01:12Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Tung\_Le](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/tung_le/32/10108_2.png) [@Tung\_Le](https://forum.opencv.org/u/Tung_Le)\
**Post date:** [April 3, 2024, 6:01am UTC](https://forum.opencv.org/t/calculate-shrimp-length-by-image-processing/17132/1 "2024-04-03T06:01:12Z")

</div>

The images I am working on are like this one  
 ![shrimp](https://us1.discourse-cdn.com/flex020/uploads/opencv/original/2X/6/6be5d80f9290fca36db330a0d05f5ef70ccf5e87.jpeg)

The image above is a thresholded shrimp image. How can I calculate the length of a shrimp? I tried using the ximproc.thinning() function but it still didn’t work.  
Thanks for your help!

---

<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:** [April 3, 2024, 8:26am UTC](https://forum.opencv.org/t/calculate-shrimp-length-by-image-processing/17132/2 "2024-04-03T08:26:15Z")

</div>

> [@Tung\_Le](#):
>
> shrimp image

I don’t know what is shrimp length .  
May be you can find contour and use [approxPolyDP](https://docs.opencv.org/4.x/d3/dc0/group __imgproc__ shape.html#ga0012a5fdaea70b8a9970165d98722b4c)() to get some length

---

<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:** [April 3, 2024, 8:57am UTC](https://forum.opencv.org/t/calculate-shrimp-length-by-image-processing/17132/3 "2024-04-03T08:57:48Z")

</div>

such questions show up occasionally.

last time, someone wanted to measure snakes, fish, or microscopic worms.

the usual approach:

- morphological thinning (with a good thinning method!), resulting in a binary image
- trace those lines, resulting in a graph. it’s ok to trace from one pixel to all its 8 neighbors. you can collapse “lines” in a second step.
- make sure the graph is a tree (i.e. no cycles). some forks may cause you trouble at the pixel level, appearing as “cycles” of several adjacent pixels. either deal with it at the pixel level somehow, or accept it and instead collapse cycles that span a small area.
- find the longest path in this tree
- hope that it’s a good representation of the midline

I would not recommend working with contours here. no need for approxpolyDP at all. even if you have a contour of a thin thing, you’d have to figure out how to calculate a midline in this contour. that would require you to match every contour point to another contour point “half a circumference” away but spatially closest. that could be made to work but I haven’t explored that, so I would not pick that approach first.

---

<div class="post-metadata">

**Author:** ![Tung\_Le](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/tung_le/32/10108_2.png) [@Tung\_Le](https://forum.opencv.org/u/Tung_Le)\
**Post date:** [April 3, 2024, 9:56am UTC](https://forum.opencv.org/t/calculate-shrimp-length-by-image-processing/17132/4 "2024-04-03T09:56:25Z")

</div>

I can’t make the shape thinner but still keep its original shape. Can you show me the thinning method and how to calculate the length through code? I’m new to this field so it’s difficult to apply the in-depth things. Thank you very much for your reply!  
This is image input

 ![img](https://us1.discourse-cdn.com/flex020/uploads/opencv/original/2X/a/a59c4a0c1f45d4f89ed6435e8f2c9c6416e02beb.png)

---

<div class="post-metadata">

**Author:** ![Tung\_Le](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/tung_le/32/10108_2.png) [@Tung\_Le](https://forum.opencv.org/u/Tung_Le)\
**Post date:** [April 3, 2024, 9:59am UTC](https://forum.opencv.org/t/calculate-shrimp-length-by-image-processing/17132/6 "2024-04-03T09:59:40Z")

</div>

thank you so much about your reply
