0

I am trying to detect crop-rows using Segmentation and HoughLines and I have a script from github that I am trying to modify.

A merging function was applied after the HoughLines to merge lines that are close to each other based on distance

But I don't seems to understand the reason for that.

from what I can tell multiple lines where detected for individual crop-row even after varying the HoughLine parameters. So merging the lines was a way to optimize the result of the HoughLine process.

def draw_lines(image,mask):
    mask = mask*255
    mask = cv2.GaussianBlur(mask,(5,5),1)
    mask = cv2.Canny(mask.astype(np.uint8),80,255)
    lines = cv2.HoughLinesP(mask,1,np.pi / 180,threshold=50,
                        minLineLength=50,maxLineGap=250)
    lines = np.squeeze(lines, axis=1)
    
    for line in lines:
        x1, y1, x2, y2 = line.astype(int)
        cv2.line(image, (x1, y1), (x2, y2), (255, 0, 0), 2)
    
    return image

image result from Houghline method test_1, test_2

.

Here is the merging function

##merge lines that are near to each other based on distance
from numpy.polynomial import polynomial as P
def merge_lines(lines):
    clusters =  []
    idx = []
    total_lines = len(lines)
    if total_lines < 30:
        distance_threshold = 20
    elif total_lines <75:
        distance_threshold = 15
    elif total_lines<120:
        distance_threshold = 10
    else:
        distance_threshold = 7
    for i,line in enumerate(lines):
        x1,y1,x2,y2 = line
        if [x1,y1,x2,y2] in idx:
            continue
        parameters = P.polyfit((x1, x2),(y1, y2), 1)
        slope = parameters[0]#(y2-y1)/(x2-x1+0.001)
        intercept = parameters[1]#((y2+y1) - slope *(x2+x1))/2
        a = -slope
        b = 1
        c = -intercept
        d = np.sqrt(a**2+b**2)
        cluster = [line]
    for d_line in lines[i+1:]:
        x,y,xo,yo= d_line
        mid_x = (x+xo)/2
        mid_y = (y+yo)/2
        distance = np.abs(a*mid_x+b*mid_y+c)/d
        if distance < distance_threshold:
            cluster.append(d_line)
            idx.append(d_line.tolist())
    clusters.append(np.array(cluster))
    merged_lines = [np.mean(cluster, axis=0) for cluster in clusters]
    return merged_lines
chuky pedro
  • 756
  • 1
  • 8
  • 26
  • what do you mean by merge? – DrBwts Jan 13 '21 at 19:23
  • @DrBwts I am following a script on github to detect crop row lines using segmentation and HoughLine transform. a merging function was applied after the HoughLines and I am yet to figure out the reason for that. I have update the above question with the merging function – chuky pedro Jan 13 '21 at 19:51
  • Are the images you include at their best resolution, i.e. what you are processing? If not please include the input images at full resolution into your question. – DisappointedByUnaccountableMod Jan 13 '21 at 23:22
  • My experience is that the scikit-image hough line transforms are better than opencv at working with photographic images - have you tried them? – DisappointedByUnaccountableMod Jan 13 '21 at 23:24
  • Does this answer your question? [How to merge lines after HoughLinesP?](https://stackoverflow.com/questions/45531074/how-to-merge-lines-after-houghlinesp) – HansHirse Jan 14 '21 at 10:01
  • @barne on the first question, the images are at the best resolution and I just updated my question with the full resolution image. 2. I haven't tried that yet but I will do so – chuky pedro Jan 14 '21 at 11:34
  • @HansHirse That does not answer my question it is just a bunch of code with no explanation – chuky pedro Jan 14 '21 at 11:35

1 Answers1

1

I did this by sorting the list of hough lines by angle first. Then, I would iterate through the sorted list and evaluate/average rho to a given pre-set rho-tolerance. When the angle changes more than a pre-set given theta-tolerance, then I would restart the rho-tolerance evaluation. When implmenting the function, look out for angles greater than 90 which produce a negative rho, in your tolerance analysis. The function ultimately takes a list of lines, a rho-tolerance and a theta-tolerance as inputs.

Ediaz
  • 11
  • 1