答案 0 :(得分:0)
这是一个天真的解决方案,但它的工作原理。从中间点开始,搜索左右两侧的急剧下降。
def get_start_end(projection):
middle_indx = int(len(projection)/2)
middle_value = projection[middle_indx]
print "middle index is = ", middle_indx, " it's value is ", middle_value
#-- search for sharp dropping right (end)
for i, v in enumerate(projection[middle_indx+1:]):
diff = int(middle_value) - v
if(diff > 0.5*middle_value):
end = i + middle_indx+1
break
#-- search for sharp dropping left (start)
for i, v in enumerate(projection[:middle_indx]):
diff = int(middle_value) - v
if(diff > 0.5*middle_value):
start = i
return start, end
- 编辑
如果我们没有发现高于ratio*middle_value
的跌幅,请找到最大跌幅。
def get_plate_y_coordinates(projection_vector):
ratio = 0.5
middle_indx = int(len(projection_vector)/2)
middle_value = projection_vector[middle_indx]
start = 0
end = len(projection_vector)
print "middle index is = ", middle_indx, " it's value is ", middle_value
#-- search for sharp dropping right (end)
saved_diff = []
for i, v in enumerate(projection_vector[middle_indx+1:]):
diff = int(middle_value) - v
if(diff > ratio*middle_value):
end = i + middle_indx+1
break
else:
saved_diff.append((diff, i + middle_indx+1))
if (end == len(projection_vector)) and (len(saved_diff)>0): #didn't chage
saved_diff = np.array(saved_diff)
sorted_diff = saved_diff[saved_diff[:,0].argsort()[::-1],:]
end = int(sorted_diff[0,1])
#-- search for sharp dropping left (start)
saved_diff=[]
for i, v in enumerate(projection_vector[:middle_indx]):
diff = int(middle_value) - v
if(diff > ratio*middle_value):
start = i
else:
saved_diff.append((diff, i))
if (start == 0) and (len(saved_diff)>0): #didn't chage
saved_diff = np.array(saved_diff)
sorted_diff = saved_diff[saved_diff[:,0].argsort()[::-1],:]
start = int(sorted_diff[0,1])
return start, end