def SlideGradient(which_content, offset, show=False): #留一点边界消除边界效应 boundary = 10 #自己定义一个梯度核函数 kernel_gradient = [-0.25, -0.5, 0, 0.5, 0.25] #定义gauss核函数作为平滑 kernel_smooth = GaussianKernel(0, 1, 5) #判断kernel的臂展是否为奇数 if len(kernel_gradient) % 2 != 1 and len(kernel_smooth) % 2 != 1: print('ERROR:redefine the window_size') else: #臂展 kernel_length = len(kernel_gradient) // 2 #梯度计算的结果 gradients = [] #临时content列表 temp_content = Dict.SortFromStart(which_content, offset)[0] # print(len(temp_content)) #总的content列表,plus些许边界 total_content = which_content[: offset] + temp_content + temp_content[: boundary] # print(len(total_content)) #直接迭代吧 for index in range(len(which_content)): # total_content[offset+k] # print(index) # print(kernel_length) #初始化它 that_gradient = 0 #这个核内部的数值有哪些 this_points = [ total_content[index + offset + ix] for ix in range(-kernel_length, kernel_length + 1) ] #对应相乘 if len(this_points) == len(kernel_gradient) == len(kernel_smooth): for k in range(len(this_points)): that_gradient += this_points[k] * kernel_gradient[ k] * kernel_smooth[k] gradients.append(abs(that_gradient)) # print(len(which_content)) # print(len(gradients)) #绘图 if show: plt.figure() plt.subplot(211), plt.plot(temp_content) plt.subplot(212), plt.plot(gradients) return gradients
def GetCorners(which_content, offset, energy_mode, img_tag, img_rgb, show=False): #滑动窗口求取梯度诶 #平均值 if energy_mode is 'mean': gradients = SlideGradient(AverageEnergy(which_content, 5, img_tag), offset) #最大值法 if energy_mode is 'max': gradients = SlideGradient(MaximalEnergy(which_content, 5, img_tag), offset) #压制非极值点点,使它们为0 peak_content = PickPeak(gradients, True) #寻找脉冲极值点的办法 temp_peak_index = [] for k in range(len(peak_content)): if peak_content[k] != 0: temp_peak_index.append(k) #极值窗口 open_length = 10 #在这个自定义区间内的都是极值 temp_peak_index.sort() #定义一个字典 #索引和周边索引的对应关系 map_index_neighbor = {} #索引和他身边被录用的索引 map_index_indexes = {} #开始创造其中元素 for this_index in temp_peak_index: #单个的索引 map_index_neighbor[this_index] = [ this_index + k for k in range(-open_length, +open_length) ] #被录用的索引(肯定有自己) map_index_indexes[this_index] = [] #判断每个索引和索隐门邻域的对应关系 for this_index in temp_peak_index: for this_neighbor in list(map_index_neighbor.values()): #判断在哪里 if this_index in this_neighbor: map_index_indexes[Dict.DictKeyOfValue( map_index_neighbor, this_neighbor)].append(this_index) #print(map_index_indexes) #真假索引了 temp_indexes = list(map_index_indexes.values()) #真实的indexes集合 indexes = [] #唯一识别 map_sum_indexes = {} #迭代合并同类项 for this_indexes in temp_indexes: indexes.append(sorted(list(set(this_indexes)))) #它们的和 map_sum_indexes[sum(indexes[-1])] = indexes[-1] #print(indexes) #print(map_sum_indexes) #用于加工的indexes列表 true_indexes = list(map_sum_indexes.values()) #求取小区间里的最大值噢 #消除偏移距之前的peak索引 peak_index = [] for this_indexes in true_indexes: #建立小区间内的索引与值的对应关系哦 map_index_value = {} for this_index in this_indexes: map_index_value[this_index] = gradients[this_index] #找到最大值噢 peak_value = np.max(list(map_index_value.values())) #获取最大值的索引 peak_index.append(Dict.DictKeyOfValue(map_index_value, peak_value)) #在gradients曲线上显示计算结果 plt.figure() plt.plot(gradients) #画出峰值点吧 for this_index in peak_index: plt.plot(this_index, gradients[this_index], marker='o', color='red') plt.axis('tight') #即将消除偏移距 map_new_old_index = Dict.SortFromStart(which_content, offset)[1] #还原到真实的叻 corner_index = [map_new_old_index[this_index] for this_index in peak_index] #在图像上还原真实的角点儿 corner_points = [which_content[this_index] for this_index in corner_index] #显示吧 if show: plt.figure() for this_pos in corner_points: Dis.ShowOnePoint(this_pos, img_rgb) return Dict.DictSortByIndex(dict(zip(corner_index, corner_points)), sorted(corner_index))
def EdgeIndex(which_fraction, derivative_format, index_plot=False, derivative_plot=False, pick_from_img=False, pick_from_plot=False): #建立which_fraction.edge坐标索引 x = [k for k in range(len(which_fraction.edge) - 1)] content = [ which_fraction.edge[k] for k in range(len(which_fraction.edge) - 1) ] map_x_content = dict(zip(x, content)) #边界位置索引的序列 index_A = [] index_B = [] #头尾一致 for k in range(len(which_fraction.edge) - 1): pos_A = which_fraction.edge[k] pos_B = which_fraction.edge[k + 1] #生成邻居索引 index_A.append(Geom.NeighborIndex(pos_A, pos_B)[0]) index_B.append(Geom.NeighborIndex(pos_A, pos_B)[1]) #轮转的影子索引列表 another_index_A, map_original_another_x_A = Dict.SortFromStart(index_A, 10) another_index_B, map_original_another_x_B = Dict.SortFromStart(index_B, 10) # print(len(index_A)) # print(len(index_B)) # # print(len(another_index_A)) # print(len(another_index_B)) # print(map_original_another_x_A) # print(map_original_another_x_B) #梯度模式 if derivative_format is 'gradient': #从index中找出梯度较大的点 gradient_A = np.gradient(index_A) gradient_B = np.gradient(index_B) derivative_A = cp.deepcopy(np.array(gradient_A)) derivative_B = cp.deepcopy(np.array(gradient_B)) #another another_gradient_A = np.gradient(another_index_A) another_gradient_B = np.gradient(another_index_B) another_derivative_A = cp.deepcopy(np.array(another_gradient_A)) another_derivative_B = cp.deepcopy(np.array(another_gradient_B)) #差分格式 if derivative_format is 'difference': #差分计算法 difference_A = [ index_A[k] - index_A[k + 1] for k in range(len(index_A) - 1) ] difference_B = [ index_B[k] - index_B[k + 1] for k in range(len(index_B) - 1) ] derivative_A = cp.deepcopy(np.array(difference_A)) derivative_B = cp.deepcopy(np.array(difference_B)) #another another_difference_A = [ another_index_A[k] - another_index_A[k + 1] for k in range(len(another_index_A) - 1) ] another_difference_B = [ another_index_B[k] - another_index_B[k + 1] for k in range(len(another_index_B) - 1) ] another_derivative_A = cp.deepcopy(np.array(another_difference_A)) another_derivative_B = cp.deepcopy(np.array(another_difference_B)) #极值大小阈值 threshold = 4 '''使用轮转换位的方法计算角点,然后取并集''' #潜在可疑点 points_x_A = list(np.where(abs(derivative_A) >= threshold)[0]) points_x_B = list(np.where(abs(derivative_B) >= threshold)[0]) #另一半 another_points_x_A = list( np.where(abs(another_derivative_A) >= threshold)[0]) another_points_x_B = list( np.where(abs(another_derivative_B) >= threshold)[0]) # print(points_x_A) # print(points_x_B) # print(another_points_x_A) # print(another_points_x_B) '''有问题!!!''' """梯度的计算需要加以改进,多点平滑""" #消除红利 for k in range(len(another_points_x_A)): another_points_x_A[k] = map_original_another_x_A[another_points_x_A[k]] for k in range(len(another_points_x_B)): another_points_x_B[k] = map_original_another_x_B[another_points_x_B[k]] # print(another_points_x_A) # print(another_points_x_B) #可以点列表 points_x_temp=points_x_A+another_points_x_A\ +points_x_B+another_points_x_B #最后取个并集 points_x = [] for item in list(set(points_x_temp)): if item == len(another_derivative_B) or item == len( another_derivative_A): points_x.append(0) else: points_x.append(item + 1) print(points_x) '''再处理一波''' #从img上获取点做实验 if pick_from_img: #返回四个点 points = PickPointsFromImg(4, which_fraction.edge) #横坐标哦 that_x = [content.index(this_point) for this_point in points] #显示吗哥 if index_plot: plt.figure() if pick_from_img: #纵坐标哦 index_A_y = [index_A[this_x] for this_x in that_x] index_B_y = [index_B[this_x] for this_x in that_x] #逐个输出可疑点 for k in range(len(that_x)): plt.subplot(211), plt.plot(that_x[k], index_A_y[k], marker='o', color='red') plt.subplot(212), plt.plot(that_x[k], index_B_y[k], marker='o', color='red') #绘制整体曲线 plt.subplot(211), plt.plot(index_A) plt.subplot(212), plt.plot(index_B) #看看梯度8 if derivative_plot: #逐个输出可疑点 for this_x in points_x: plt.subplot(211), plt.plot(this_x, index_A[this_x], marker='o', color='red') plt.subplot(212), plt.plot(this_x, index_B[this_x], marker='o', color='red') plt.figure() #从img上获取点做实验 if pick_from_img: #纵坐标哦 derivative_A_y = [derivative_A[this_x] for this_x in that_x] derivative_B_y = [derivative_B[this_x] for this_x in that_x] #逐个输出 for k in range(len(that_x)): plt.subplot(211), plt.plot(that_x[k], derivative_A_y[k], marker='o', color='red') plt.subplot(212), plt.plot(that_x[k], derivative_B_y[k], marker='o', color='red') #整体曲线 plt.subplot(211), plt.plot(derivative_A) plt.subplot(212), plt.plot(derivative_B) #逐个输出可疑点 for this_x in points_x: plt.subplot(211), plt.plot(this_x, derivative_A[this_x], marker='o', color='red') plt.subplot(212), plt.plot(this_x, derivative_B[this_x], marker='o', color='red') return [content[this_x] for this_x in points_x] if pick_from_plot: return PickPointsFromPlot(4, map_x_content)