I want to determine length of filaments from image

I have this image of multiple filaments that shielding can interrupt on top. (In the image both are interrupted by shielding). I need to determine the length of the filaments and include interrupted segments in it if possible. I binarized the image using this tutorial. Unfortunately, the interrupted segments on top didn’t go through. But as I said, it’s not so important.

But I don’t know how to proceed and get the lengths of the filaments and include in the length interrupted segments.

You need to use Hogh transformation Hough Transforms in Image Processing - Scaler Topics

I tried this code:

img = cv2.imread(‘DSC_7987.jpg’)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
#Apply edge detection
edges = cv2.Canny(gray, 50, 150, apertureSize=3)
#Apply Hough transform
lines = cv2.HoughLines(edges, rho=1, theta=np.pi/180, threshold=100)
#Draw detected lines on the original image
for line in lines:
rho, theta = line[0]
a = np.cos(theta)
b = np.sin(theta)
x0 = a * rho
y0 = b * rho
x1 = int(x0 + 1000 * (-b))
y1 = int(y0 + 1000 * (a))
x2 = int(x0 - 1000 * (-b))
y2 = int(y0 - 1000 * (a))
cv2.line(img, (x1, y1), (x2, y2), (0, 0, 255), 2)
#Display the result
cv2.imshow(‘Image’, img)

And I’m getting this error:
Traceback (most recent call last):

File ~\anaconda3\Lib\site-packages\spyder_kernels\py3compat.py:356 in compat_exec
exec(code, globals, locals)

File c:\users\janko\onedrive\muni\bakalářka\fotky_k_analyze\analyza_fotek2.py:53
for line in lines:

TypeError: ‘NoneType’ object is not iterable

nobody can see the code you get that TypeError from.


Thank you, I fixed it

Hint: Your code is very fragile because you don’t check whether your function calls succeed and what they return, for example HoughLines call.

And what should I do? I try it line by line and from line “edges = cv.Canny(gray, 50, 150, apertureSize=3)” I get only a violet image.

trying to solve any problem with canny and hough is the usual mistake that beginners make.

you thought you were well advised to follow the suggestion of a stranger on the internet that just made their first post in this forum.