1

I have a grey scale image with a red contour scored ontop of it (all contained in one image file). I have searched for pixels in this red contour by defining some threshold and limiting r,g,b values to count as a red pixel. The search for these pixels happens left to right, top to bottom through the image array - so the points in the contour aren't listed in any meaningful order.

contour plot

I would like to implement an alogrithm that finds the visually clear path through these contour points. My initial thought was to order the contour points by their polar angle from the centroid (blue) of the points, however if the contour slightly doubles back on itself then this doesn't make sense (pictured below).

contour plot doubling back

Are there python libraries/functions for this kind of problem, or does anyone have insight as to a good algorithm to try and implement?

Here is an example image:

Example Image

And my code to find red pixels:

r_thres = 0.39

g_lim = 0.25

b_lim = 0.25


red_pixels = np.where((cine_image[:,:,0] >= r_thres) & 
(cine_image[:,:,1] <= g_lim) & (cine_image[:,:,2] <= b_lim))




red_pixels_y = [ red_pixels[0][i] for i in range(0, 
len(red_pixels[0]))]

red_pixels_x = [ red_pixels[1][i] for i in range(0, len(red_pixels[1]))]

red_pixels_coords = [ [ red_pixels[1][i] , red_pixels[0][i]] for i in 
range(0, len(red_pixels[1]))]
Adam
  • 11
  • 2
  • Please post an example image and your existing code. For more information, please see: https://stackoverflow.com/help/minimal-reproducible-example – Kapocsi Nov 17 '21 at 20:37
  • I would recommend representing the points by their x,y coordinates and using [Breadth First Search](https://en.wikipedia.org/wiki/Breadth-first_search) to find the shortest path. – c0der Nov 18 '21 at 05:02
  • Added code and image – Adam Nov 18 '21 at 09:43
  • It sounds like you want to generate the [concave hull](https://stackoverflow.com/questions/83593/is-there-an-efficient-algorithm-to-generate-a-2d-concave-hull) (or maybe even convex hull, which is much easier to generate, if your problem will allow it) – BlueRaja - Danny Pflughoeft Jun 05 '22 at 12:47

0 Answers0