def sort_line(line1,line2):
for x1,y1,x2,y2 in line1:
mn1 = min(x1,x2)
for x1,y1,x2,y2 in line2:
mn2 = min(x1,x2)
return mn1 < mn2
lines.sort(sort_line)
Here lines has 4 int in every row. From what I understood from python wiki is that I need to pass a compare function as argument in the sort() function. But I get the following error,
<ipython-input-107-b0e2f3c756cf> in draw_lines(img, lines, color, thickness)
69 """
70
---> 71 lines.sort(sort_line)
72
73 for line in lines:
TypeError: an integer is required (got type function)
I tried with sorted() but couldn't make it work either.
Python and anaconda version
Python 3.5.2 |Anaconda 4.2.0 (64-bit)| (default, Jul 5 2016, 11:41:13) [MSC v.1900 64 bit (AMD64)] on win32
I'm running the code in jupyter notebook.
My implementation I think is same as the following question but mine doesn't work.
After following Martijn's answer I modified my code to this
lineList = lines.tolist() //lines is a numpy array so I converted it to list
print ('lineList: ',lineList)
lineList.sort(key=lambda l: min(l[0], l[2]))
lines = np.array(lineList)
Gives me the following error
<ipython-input-115-06412e8f5aba> in <lambda>(l)
72 lineList = lines.tolist()
73 print ('lineList: ',lineList)
---> 74 lineList.sort(key=lambda l: min(l[0], l[1]))
75 lines = np.array(lineList)
76
IndexError: list index out of range