find_lines vs cv2.HoughLines ... which is better?

The key statement for the max method is too complex. You’ll want to use the filter statement in python. See the pixy emulation scripts for examples on how to filter objects using the filter command. It’s quite easy.

In general, use magnitude to threshold lines and pick out garbage ones.

I’ll take a look at the filter command … but why isn’t this working?

for rl in img.find_line_segments(roi = r_r, threshold = 1000, theta_margin = 15, rho_margin = 15, segment_threshold = 100):
    for rl[6] != 0 : #rl[6] should be the theta value
        print(rl)

I’m getting a syntax error on the

for rl[6] != 0:

line … ?

Ok. That sort of solves an issue … but that returns a list … and I am trying to draw the lines returned as if returned from find_line_segments() .
here’s what I’m doing

.
.
.
def line_filter(line):
    if(line[6] == 0):
        return False
    elif(line[6] > 0):
        return ( max(img.find_line_segments(roi = r_r, threshold = 1000, theta_margin = 15, rho_margin = 15, segment_threshold = 100), key = lambda x: x.length()) )   

while(True):
    clock.tick()
    img = sensor.snapshot() if snapshot_source else img_reader.next_frame(copy_to_fb=True, loop=True)
    img.draw_rectangle(l_r, color = 155 )
    img.binary(binary_thresholds, invert = False)
    rl = list(filter(line_filter, img.find_line_segments(roi = r_r, threshold = 1000, theta_margin = 15, rho_margin = 15, segment_threshold = 100))) 
    print(rl)
    #img.draw_line(rl.line(), color = 155)
    print(clock.fps())

because it’s returning a ‘list’, img.draw_line() doesn’t know what to do … is there a good way to back out of that? Not have it return a list?
If I don’t stack it all in a list, the filter() object complains the ‘filter’ object has no attribute ‘line’

Filter just removed elements from a list by testing if your filter function returns false on the element. It still returns a list.

As for your loop. Please review python syntax. In general, in a for loop you have to iterate over a list and then you can put an if statement in the loop to check the element value.

I think I get what’s going on …
at some point, there’s no line segments found … so there’s nothing to filter … so rl doesn’t exist.

Ok. Now I’m thoroughly confused … this isn’t making sense to me.
Why does this portion of the script fail if “arg is an empty sequence”?

rl = max(img.find_line_segments(roi = r_r, threshold = 1000, theta_margin = 15, rho_margin = 15, segment_threshold = 100), key = lambda x: x.length())
    #print(rl)
    if rl is not None:
        if rl[6] > 0 :
            img.draw_line(rl.line(), color = 155)
            print("RL")
            print(rl)

The “if rl is not None:” should prohibit it from ever failing if max() doesn’t return a value …

EDIT:
I’m starting to think that there’s an error with the way that the max() function returns values. If I remove the if statement completely and have no other processing afterwards, I still get the error.

Do not indent unless you need to please. There’s an unnecessary indent.

Anyway, what’s the exact error you are getting and on which line? Post all the code?

This is how it was fixed:

segments_l = img.find_line_segments(roi = l_r, threshold = 1000, theta_margin = 15, rho_margin = 15, segment_threshold = 100)
        if segments_l :
            max_segment_l = max(segments_l, key = lambda x: x.length())

I broke the max() statement out in to this to get around the cases where max() doesn’t return a value … fine_line_segments seems to always return a line… but it seems like max() doesn’t always return a maximum line.
I can’t seem to get any more insight than that however.

Here is the error I was seeing :

  
  Traceback (most recent call last):
File "<stdin>", line 77, in <module>
ValueError: arg is an empty sequence
MicroPython d23b594 on 2017-07-05; OPENMV3 with STM32F765
Type "help()" for more information.

Ah, okay, I see. Max might not like the fact that find lines may not return a line sometimes. Then max complains.

that makes sense (although it’s weird because I don’t see any “skipping” in finding a line… although I don’t have insight at that level, really)


and now to figure out why it sometimes takes several resets for the IDE to load/run the program.

Any luck? It’s so annoying having to keep resetting to get it to run.

Hi, have you upgraded to OpenMV IDE v1.8.0?