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 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 RecoverLength(total_fractions, img_rgb, img_tag, rgb_dict, animation_show=False, show=False): #拾取出某个目标layer which_layer=Pick.PickLayer(total_fractions,img_rgb) #目标layer的edge which_edge=which_layer.edge '''HarrisM函数有问题''' #抓出几个角点 #map_corner_points=Cor.GetCorners(which_content,100,'mean',img_tag,img_rgb,True) '''层长恢复''' #节省调试时间,直接给出答案 #暴力增加角点 map_corner_points=Cor.AddCorners(which_edge,4,img_rgb,True) #初始化which_layer的角点 which_layer.corners=list(map_corner_points.values()) #计算曲线的长度 layer_length=Geom.CurvedLinesLength(which_edge,map_corner_points,2,img_rgb) #初始化面积 which_layer.InitArea() #层厚度(其实没什么意义) layer_thickness=which_layer.area/layer_length #初始化厚度和长度 which_layer.thickness=layer_thickness which_layer.length=layer_length #相关联的断层可能多个 """找出这个layer接壤的fault""" '''找到了,处理两侧都有断层的接应点情况''' which_faults=Pick.NeighborFault(which_layer,total_fractions) # print(len(which_faults)) '''一个fault的情况''' if len(which_faults)==1: #one and only which_fault=which_faults[0] #计算那几个角点哪几个离断层比较近 corner_points=list(map_corner_points.values()) #计算重心 fault_center=Geom.CalculateBaryCenter(which_fault.content) #重心到几个角点的距离 map_distances_fault2corners=Geom.DistancesMap(fault_center,corner_points) #距离最小的两个为接应点 map_attachment_points=Dict.DictSortByIndex(map_distances_fault2corners,sorted(list(map_distances_fault2corners.keys()))) #fraction上的两个接应点 fraction_attachment_points=list(map_attachment_points.values())[0:2] #fraction取两个接应点的中点 fraction_attachment_center=np.round(Geom.CalculateBaryCenter(fraction_attachment_points)).astype(int) #再去寻找fault上和接应点 map_distances_attachment2fault=Geom.DistancesMap(fraction_attachment_center,which_fault.edge) #fault上的接应点 fault_attachment_point=map_distances_attachment2fault[min(list(map_distances_attachment2fault.keys()))] #Dis.ShowOnePoint(fault_attachment_point,img_rgb) '''墙类型的填充函数(模型边界)''' #另一端的方向 directional_offset=which_layer.center[1]-fault_attachment_point[1] #边界的i坐标 i_boundary=fault_attachment_point[0] #左侧 if directional_offset<0: # directional_mode='left' #增长的方向 step_sign=+1 #边界的j坐标 j_boundary=fault_attachment_point[1]-layer_length #右侧 if directional_offset>0: # directional_mode='right' #增长的方向 step_sign=-1 #边界的坐标 j_boundary=fault_attachment_point[1]+layer_length #边界的坐标 boundary=np.round([i_boundary,j_boundary]).astype(int) #断层上接应点的索引 if fault_attachment_point in which_fault.edge: fault_attachment_index=which_fault.edge.index(fault_attachment_point) #计数器 count=1 #需要描绘的点 #第一行 points_to_draw=[[fault_attachment_point[0],j] for j in range(boundary[1],fault_attachment_point[1],step_sign)] #用于迭代的总面积 temp_area=len(points_to_draw) #循环结束的标志 flag=(temp_area>=which_layer.area) #建立一个新的tag矩阵 img_tag_temp=np.zeros(np.shape(img_tag)) #开始迭代 while not flag: #本次迭代需要画出的点 that_points_to_draw=[] count+=1 #根据count的奇偶性确定增量的符号 if count%2==0: sign=-1 if count%2==1: sign=1 #单向增量计数器 index_increment=sign*count//2 # print(index_increment) #现在的接应点 that_attachment_point=which_fault.edge[fault_attachment_index+index_increment] #计算这一次迭代面积增量 # area_increment=np.abs(that_attachment_point[1]-boundary[1]) # print(area_increment) # print(temp_area,which_fraction.area) Print=False #图像中填充新增的那一行 #根据边界与接应点的位置关系进行填充 if Print: print('right') print(that_attachment_point) # img_tag_temp[that_attachment_point[0],boundary[1]:that_attachment_point[1]]=which_fraction.tag #将这些点添加至列表 for j in range(boundary[1],that_attachment_point[1],step_sign): # print('plus',j) that_points_to_draw.append([that_attachment_point[0],j]) # print(img_tag_temp[that_attachment_point[0],boundary[1]:that_attachment_point[1]]) #加进大集合里 points_to_draw+=that_points_to_draw if animation_show: #把它们都绘制出 for this_point in points_to_draw: img_tag_temp[this_point[0],this_point[1]]=which_layer.tag #让图片停留几秒钟之后关闭,可以是整数也可以是浮点数 plt.imshow(Im.Tag2RGB(img_tag_temp,rgb_dict)) plt.pause(1) plt.close() #临时面积 temp_area=len(points_to_draw) #更新循环执行的条件 flag=(temp_area>=which_layer.area) '''多个fault的情况''' if len(which_faults)>1: #layer中点 center=np.array(which_layer.center).astype(int) #当下接应点 attachment_point=cp.deepcopy(center) '''以下内容要进入循环的噢''' #装所谓坐标的pocket pocket=[] #在layer的edge中找到一个横线确定以前的长度 for this_pos in which_layer.edge: if this_pos[0]==attachment_point[0]: pocket.append(this_pos[1]) # # #pocket中的两个接应点 # left_attachment_point=[this_pos[0],min(pocket)] # right_attachment_point=[this_pos[0],max(pocket)] # # #将这些点添加至列表 # for j in range(boundary[1],that_attachment_point[1],step_sign): # ## print('plus',j) # that_points_to_draw.append([that_attachment_point[0],j]) # # fitting_interval=FittingInterval(which_layer) #把它们都绘制出 for this_point in points_to_draw: img_tag_temp[this_point[0],this_point[1]]=which_layer.tag #显示呗 if show: plt.imshow(Im.Tag2RGB(img_tag_temp,rgb_dict)) #改变fraction的content which_layer.content=cp.deepcopy(points_to_draw) #更新which_fraction的一切? which_layer.UpdateAll() return which_layer
def CurvedLinesLength(which_content, map_corner_points, lines_amount, img_rgb): #按索引排序 map_corner_points = Dict.DictSortByIndex( map_corner_points, sorted(list(map_corner_points.keys()))) # print(map_corner_points) #计算出几个点的曲线距离 #索引 corner_index = list(map_corner_points.keys()) #索引列表做一下 curve_points = [] for k in range(len(corner_index)): #第一段特殊处理 if k == 0: curve_points.append(which_content[corner_index[-1]:] + which_content[:corner_index[0] + 1]) #其他正常 else: curve_points.append( which_content[corner_index[k - 1]:corner_index[k] + 1]) # print(curve_points) '''简单算法(用于检验)''' # #所有的 # curve_distances=[] # # #计算每一条曲线的曲线长度 # curve_distances=[ContinuousDistance(this_curve_points) for this_curve_points in curve_points] # # print(curve_distances) '''拾取算法''' curve_points_up_and_down = [] #获取两个点的坐标,确定两条层长线 for temp_point in plt.ginput(lines_amount): #点击获取曲线中的点的坐标并转化一下 point = [int(np.round(temp_point[1])), int(np.round(temp_point[0]))] # print(point) #多条线到这个点的距离 #以及用坐标集合当返回值 map_distance_curve_points = {} #先求每条线的中点 for this_curve_points in curve_points: #重心 center = CalculateBaryCenter(this_curve_points) #收录进映射关系 map_distance_curve_points[Distance(center, point)] = this_curve_points curve_points_up_and_down.append(map_distance_curve_points[min( list(map_distance_curve_points.keys()))]) #上下两条边的点集合 curve_points_up, curve_points_down = curve_points_up_and_down return curve_points_up, curve_points_down #上下两条边的长度 curve_length_up = ContinuousDistance(curve_points_up) curve_length_down = ContinuousDistance(curve_points_down) # print(curve_length_up) # print(curve_length_down) Dis.Line2Red(curve_points_up + curve_points_down, img_rgb) return (curve_length_up + curve_length_down) / 2
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 Find1stNeighbor(tag, flag_stop, edge, img_tag, index): #[i,j-1],[i+1,j-1],[i+1,j],[i+1,j+1],[i,j+1],[i-1,j+1],[i-1,j],[i-1,j-1] #邻域的索引和横纵坐标的索引(绝对索引) neighbordict = { 0: (0, -1), 1: (1, -1), 2: (1, 0), 3: (1, 1), 4: (0, 1), 5: (-1, 1), 6: (-1, 0), 7: (-1, -1) } #以最后一个edge点为指针进行检索 first_pixel = pixel() first_pixel.ypos = edge[-1][0] first_pixel.xpos = edge[-1][1] #3 S[K]从上一个目标点是S[k-1]逆时针进行遍历 #重新规划索引new_index后一个索引和前一个索引呈对角关系 #若索引大于4,归化 if index < 4: new_index = index + 4 else: new_index = index - 4 new_neighbordict = Dict.DictSortFromStart(neighbordict, new_index) #生成邻居列表,起始迭代邻居的索引 first_pixel.GenerateNeighbor(img_tag) #邻域内邻居数量 count = 0 for i in range(len(new_neighbordict)): #获取目标点的索引,转化为绝对索引 index = list(new_neighbordict.keys())[i] #符合tag的点计数 if first_pixel.neighbor[index] == tag: count += 1 #建立新的pixel对象 temp_pixel = pixel() temp_pixel.ypos = first_pixel.ypos + new_neighbordict[index][0] temp_pixel.xpos = first_pixel.xpos + new_neighbordict[index][1] pos = [temp_pixel.ypos, temp_pixel.xpos] #判断目标点和起点是否相同,不能是第一个点 if i > 0 and pos == edge[0]: flag_stop = True edge.append(pos) break #1 S[k-1]邻域内的第一个点已在边缘集合当中,则访问下一个点 if pos not in edge: edge.append(pos) break #*2 S[k]邻域内只有一个边缘点,即上一个点S[k-1],则访问S[k-1]邻域内下一个点 if len(edge) > 1 and pos == edge[-2] and count == 1 and i == 7: edge.append(pos) break return edge, index, flag_stop
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)
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]