How To Count Lines In Image With Python, Opencv
I want to count paper,So I think about using the line detection. I have tried some methods like Canny, HoughLines,FLD. But I only get the processed photo, I have no idea how to cou
Solution 1:
I think you could count the number of lines (papers) based on how many straight lines do you have. My idea is that
- You should calculate the distances for all points that you get from HoughLinesP by using
np.linalg.norm(point1 - point2)
for more details. - Then you could adjust the proper distance that used to identify the lines to ignore the noise (small) lines. I recommend using
min_line_length
in HoughLinesP. - Count the number of distances (lines) that are bigger than the proper distance.
This is the code that I used for your image :
# After you apply Hough on edge detected image
lines = cv.HoughLinesP(img, rho, theta, threshold, np.array([]),
min_line_length, max_line_gap)
# calculate the distances between points (x1,y1), (x2,y2) :
distance = []
for line in lines:
distance.append(np.linalg.norm(line[:,:2] - line[:,2:]))
print('max distance:',max(distance),'\nmin distance:',min(distance))
# Adjusting the best distance
bestDistance=1110
numberOfLines=[]
count=0for x in distance:
if x>bestDistance:
numberOfLines.append(x)
count=count+1print('Number of lines:',count)
Output:
max distance:1352.8166912039487min distance:50.0Number of lines:17
Post a Comment for "How To Count Lines In Image With Python, Opencv"