Calculate shrimp length by image processing

The images I am working on are like this one
shrimp

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!

I don’t know what is shrimp length .
May be you can find contour and use approxPolyDP() to get some length

1 Like

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.

1 Like

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

thank you so much about your reply