python一段代码看不懂
'''这是某一个类中的一个函数,step2按照x1排序这一步看不懂
sorted(cleaned, key =operator.itemgetter(0))这个我知道是按照列表的第一维度也就是x1排序
sorted(cleaned, key =operator.itemgetter(1))这个我也知道是按照列表的第二维度也就是y1排序
sorted(cleaned, key =operator.itemgetter(0, 1))为什么这个还是按照x1排序呢?
'''
def identify_blocks(self,image, lines, make_copy=True):
if make_copy:
new_image = np.copy(image)
#Step 1: 过滤部分直线
cleaned = []
for line in lines:
for x1,y1,x2,y2 in line:
if abs(y2-y1) <=1 and abs(x2-x1) >=25 and abs(x2-x1) <= 55:
cleaned.append((x1,y1,x2,y2))
#Step 2: 对直线按照x1进行排序
import operator
list1 = sorted(cleaned, key=operator.itemgetter(0, 1))
#Step 3: 找到多个列,相当于每列是一排车
clusters = {}
dIndex = 0
clus_dist = 10
for i in range(len(list1) - 1):
distance = abs(list1 - list1)
if distance <= clus_dist:
if not dIndex in clusters.keys(): clusters = []
clusters.append(list1)
clusters.append(list1)
else:
dIndex += 1
#Step 4: 得到坐标
rects = {}
i = 0
for key in clusters:
all_list = clusters
cleaned = list(set(all_list))
if len(cleaned) > 5:
cleaned = sorted(cleaned, key=lambda tup: tup)
avg_y1 = cleaned
avg_y2 = cleaned[-1]
avg_x1 = 0
avg_x2 = 0
for tup in cleaned:
avg_x1 += tup
avg_x2 += tup
avg_x1 = avg_x1/len(cleaned)
avg_x2 = avg_x2/len(cleaned)
rects = (avg_x1, avg_y1, avg_x2, avg_y2)
i += 1
print("Num Parking Lanes: ", len(rects))
#Step 5: 把列矩形画出来
buff = 7
for key in rects:
tup_topLeft = (int(rects - buff), int(rects))
tup_botRight = (int(rects + buff), int(rects))
cv2.rectangle(new_image, tup_topLeft,tup_botRight,(0,255,0),3)
return new_image, rects
这是为什么呢? itemgetter(0, 1) 先按第 0 个排序,如果相等按第 1 个排序
页:
[1]