def NeighborIndex(pos_A, pos_B): #用邻居原则来判断 #A,B各自的邻居 neighbor_A = [(pos_A[0] + step_x, pos_A[1] + step_y) for step_x in [-1, 0, 1] for step_y in [-1, 0, 1]] neighbor_B = [(pos_B[0] + step_x, pos_B[1] + step_y) for step_x in [-1, 0, 1] for step_y in [-1, 0, 1]] # print(neighbor_A) # print(neighbor_B) #判断是否在各自的8邻域内 A_in_B = tuple(pos_A) in neighbor_B B_in_A = tuple(pos_B) in neighbor_A #必须都成立哦 if not (A_in_B and B_in_A): print('this point is not available') return #通过位置建立索引 code_list = [k for k in range(8)] relative_pos_list = [(-1, 0), (-1, 1), (0, 1), (1, 1), (1, 0), (1, -1), (0, -1), (-1, -1)] #建立编码与位置的映射 map_code_relative_pos = dict(zip(code_list, relative_pos_list)) #如果为AB邻居 if A_in_B and B_in_A: pos_A = np.array(pos_A) pos_B = np.array(pos_B) #相对位置 relative_pos_B2A = tuple(pos_B - pos_A) relative_pos_A2B = tuple(pos_A - pos_B) #位置编码 code_B2A = Dict.DictKeyOfValue(map_code_relative_pos, relative_pos_B2A) code_A2B = Dict.DictKeyOfValue(map_code_relative_pos, relative_pos_A2B) # print('B:the position code',code_B2A,'of A') # print('A:the position code',code_A2B,'of B') return code_B2A, code_A2B
def RGB2Tag(img_rgb, rgb_dict, show=False, axis=False): img_tag = np.zeros((np.shape(img_rgb)[0], np.shape(img_rgb)[1])) #给img_tag矩阵赋值 for i in range(np.shape(img_tag)[0]): for j in range(np.shape(img_tag)[1]): img_tag[i, j] = Dict.DictKeyOfValue(rgb_dict, list(img_rgb[i, j].astype(int))) #显示 if show: plt.figure() plt.imshow(img_tag, cmap='gray') #显示坐标轴吗 if axis: plt.axis('off') return img_tag
def chipsOf(which_Chip, side): #先建立键值对 #根据values的值返回chips对象 map_J_total_chips = MapCenterchipsOf(which_Chip, 'J') #最左的即J最小的chips if side is 'left': return Dict.DictKeyOfValue(map_J_total_chips, min(list(map_J_total_chips.values()))) #最右的即J最大的chips if side is 'right': return Dict.DictKeyOfValue(map_J_total_chips, max(list(map_J_total_chips.values())))
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 InitDict(img_rgb, base_adjust=False, fault_exist=False): #临时变量 rgb_list_temp = [] for i in range(np.shape(img_rgb)[0]): for j in range(np.shape(img_rgb)[1]): if list(img_rgb[i, j].astype(int)) not in rgb_list_temp: rgb_list_temp.append(list(img_rgb[i, j].astype(int))) #判断背景色 if [255, 255, 255] in rgb_list_temp: rgb_list_temp.remove([255, 255, 255]) #只有layer的rgb layer_rgb_list = cp.deepcopy(rgb_list_temp) # print(layer_rgb_list) #fault #有断层的情况哦 if fault_exist: #各种颜色像素点数量的字典 rgb_number_dict = {} for k in range(len(rgb_list_temp)): rgb_number_dict[k] = np.sum(img_rgb == rgb_list_temp[k]) #比较像素点数量的多少 key = list(rgb_number_dict.keys()) value = list(rgb_number_dict.values()) #得到断层的rgb值 fault_rgb = rgb_list_temp[key[value.index(min(value))]] # print(fault_rgb) #删除fault的rgb layer_rgb_list.remove(fault_rgb) # print(layer_rgb_list) #生成rgb_dict,包括layer和fault rgb_dict = {} for i in range(len(layer_rgb_list)): rgb_dict[i + 1] = layer_rgb_list[i] # print(layer_rgb_list) #但是列表不可以作为索引,因此先转化为临时tag列表 tag_list = [index + 1 for index in range(len(layer_rgb_list))] #临时的tag_color索引 rgb_dict_temp = dict(zip(tag_list, layer_rgb_list)) # print(rgb_dict_temp) #比较他们的深度度 depth_list = [] for this_rgb in list(rgb_dict_temp.values()): # print(np.mean(list(np.where(img_rgb==list(this_rgb))[0]))) depth_list.append(np.mean(list( np.where(img_rgb == list(this_rgb))[0]))) # 建立颜色何深度的索引 map_tag_depth_temp = dict(zip(tag_list, depth_list)) # print(map_tag_depth_temp) #对depth进行排序 depth_list.sort() # print(depth_list) #老的tag要修改 tag_list_temp = [] #索引每一个深度值 for this_depth in depth_list: tag_list_temp.append( Dict.DictKeyOfValue(map_tag_depth_temp, this_depth)) # print(depth_list) # print(tag_list_temp) #再按照它找rgb rgb_list = [] for this_tag in tag_list_temp: rgb_list.append(rgb_dict_temp[this_tag]) #最终结果 rgb_dict = dict(zip(tag_list, rgb_list)) if fault_exist: #索引-1代表断层fault rgb_dict[-1] = fault_rgb #重新排序 rgb_dict = Dict.DictSortByIndex(rgb_dict, sorted(list(rgb_dict.keys()))) #base #调整基底哦babe if base_adjust: base_tag = list(rgb_dict.keys())[-1] # print(base_tag) base_rgb = rgb_dict[base_tag] #删除并重命名 del rgb_dict[base_tag] #base_tag的索引定义为-2 rgb_dict[-2] = base_rgb #blank if np.array([255, 255, 255]) in img_rgb: #0代表背景色 rgb_dict[0] = [255, 255, 255] #排序 rgb_dict = Dict.DictSortByIndex(rgb_dict, sorted(list(rgb_dict.keys()))) # print(rgb_dict) return rgb_dict
def GetExtremePoints(which_content, mode='big', show=False): # print('GetExtremePoints') #所有函数值 # values=ReOrder(which_content) values = cp.deepcopy(which_content) #边缘两个取隔壁和自己的差,中间取隔壁俩的差 gradients = np.gradient(values) #建立梯度和值的映射关系 map_values_gradients = dict(zip(values, gradients)) #得到极值的索引 index_extreme_points = [] #values用于显示的点 values_X_Y_to_plot = [] #gradients用于显示的点 gradients_X_Y_to_plot = [] #求得的极值列表 extreme_values = [] # print(gradients) # print(map_values_gradients) #寻找0极值,去掉首元素 for k in range(1, len(values)): # print(gradients[k-1],gradients[k]) #极大值 if mode == 'big': #左右两边一大一小 if gradients[k - 1] > 0 > gradients[k]: that_value_left = Dict.DictKeyOfValue(map_values_gradients, gradients[k - 1]) that_value_right = Dict.DictKeyOfValue(map_values_gradients, gradients[k]) that_value = max(that_value_left, that_value_right) else: continue #极小值 if mode == 'small': #左右两边一大一小 if gradients[k - 1] < 0 < gradients[k]: that_value_left = Dict.DictKeyOfValue(map_values_gradients, gradients[k - 1]) that_value_right = Dict.DictKeyOfValue(map_values_gradients, gradients[k]) that_value = min(that_value_left, that_value_right) else: continue # #所有极值 # if mode=='both': # # #左右两边一大一小 # if gradients[k-1]>0>gradients[k] or gradients[k-1]<0<gradients[k]: # # that_value_left=Dict.DictKeyOfValue(map_values_gradients,gradients[k-1]) # that_value_right=Dict.DictKeyOfValue(map_values_gradients,gradients[k]) # # that_value=max(that_value_left,that_value_right) # print(that_value_left,that_value_right) #极值列表上车 extreme_values.append(that_value) #该极值索引 that_index = list(values).index(that_value) index_extreme_points.append(that_index) #描绘的点上车吧 values_X_Y_to_plot.append([that_index, values[that_index]]) gradients_X_Y_to_plot.append([that_index, gradients[that_index]]) # print(extreme_values) # print(distances_X_Y_to_plot) # print(gradients_X_Y_to_plot) #显示吗 if show: # print('show') #绘制值曲线 plt.figure() plt.subplot(211) plt.plot(values, color='black', linestyle='-') for this_X_Y in values_X_Y_to_plot: plt.scatter(this_X_Y[0], this_X_Y[1], color='red') #绘制梯度曲线 plt.subplot(212) plt.plot(gradients, color='black', linestyle='--') for this_X_Y in gradients_X_Y_to_plot: plt.scatter(this_X_Y[0], this_X_Y[1], color='red') return extreme_values
def Cohere(Chips): #根据中点来判断 J_center_Chips = [this_Chip.center[1] for this_Chip in Chips] #建立Chip和J值的索引关系 map_J_center_Chips = dict(zip(Chips, J_center_Chips)) #min在左 Chip_left = Dict.DictKeyOfValue(map_J_center_Chips, min(list(map_J_center_Chips.values()))) #max在右 Chip_right = Dict.DictKeyOfValue(map_J_center_Chips, max(list(map_J_center_Chips.values()))) # print(Chip_left.center) # print(Chip_right.center) #取Chip_left中最右的 chips_left = chipsOf(Chip_left, 'right') #取Chip_right中最左的 chips_right = chipsOf(Chip_right, 'left') # print(chips_right,chips_left) #根据其坐标进行移动 # print(chips_right.center) # print(chips_left.center) # # print(chips_right.content) # print(chips_left.content) I_offset = SpecialPointOf(chips_right, 'left')[0] - SpecialPointOf( chips_left, 'right')[0] J_offset = SpecialPointOf(chips_right, 'left')[1] - SpecialPointOf( chips_left, 'right')[1] # print(I_offset,J_offset) #左右盘的位移 I_offset_left = int(np.floor(I_offset / 2)) J_offset_left = int(np.floor(J_offset / 2)) #右边的偏移距 I_offset_right = abs(abs(abs(I_offset) - abs(I_offset_left))) J_offset_right = abs(abs(abs(J_offset) - abs(J_offset_left))) #判断是否为0 #乘上算子 if I_offset_left != 0: I_offset_right *= (-I_offset_left / abs(I_offset_left)) if J_offset_left != 0: J_offset_right *= (-J_offset_left / abs(J_offset_left)) # print(I_offset_left,J_offset_left) # print(I_offset_right,J_offset_right) # print(Chip_left.center) # print(Chip_right.center) #移动Chip_left,Chip_right Chip_left.Move(I_offset_left, J_offset_left) Chip_right.Move(I_offset_right, J_offset_right) # print(Chip_left.center) # print(Chip_right.center) return [Chip_left, Chip_right]