Python関心領域ROIを抽出するケース(二)実現過程

49928 ワード

セル密度の計算
1メッシュ座標の生成
x_l = input('    x   ')
y_l = input('    y   ')
x_l = int(x_l)
y_l = int(y_l)
#        
def set_ax():
    ax = plt.axes([0.05, 0.05, 0.95, 0.95]) #[xmin,ymin,xmax,ymax]
    ax.set_xlim(-1,x_l)
    ax.set_ylim(-1,y_l)
    ax.xaxis.set_major_locator(plt.MultipleLocator(1.0))#  x      1
    ax.xaxis.set_minor_locator(plt.MultipleLocator(0.5))#  x      0.5
    ax.yaxis.set_major_locator(plt.MultipleLocator(1.0))#  y      1
    ax.yaxis.set_minor_locator(plt.MultipleLocator(0.5))#  y      0.5
    ax.grid(which='major', axis='x', linewidth=0.75, linestyle='-', color='0.75')#   x      x       x    
    ax.grid(which='minor', axis='x', linewidth=0.25, linestyle='-', color='0.75')#   x      x       x    
    ax.grid(which='major', axis='y', linewidth=0.75, linestyle='-', color='0.75')
    ax.grid(which='minor', axis='y', linewidth=0.25, linestyle='-', color='0.75')
    ax.set_xticks(np.arange(0, x_l + 2, 1))
    ax.set_yticks(np.arange(0, y_l + 2, 1))
    return ax

2ランダムに複数のトラックを生成
num = input('       ')
num = int(num)
 #    
    for i in range(num):
        x0 = np.random.randint(0,x_l + 1)
        y0 = np.random.randint(0,y_l + 1)
        x1 = np.random.randint(0,x_l + 1)
        y1 = np.random.randint(0,y_l + 1)
        while(x1 == x0 and y1 == y0):
            x1 = np.random.randint(0,x_l + 1)
            y1 = np.random.randint(0,y_l + 1)
        
        plt.plot([x0,x1],[y0,y1],c='black')    

3セル密度の計算
#        
def mult(a, b, c):
       return (a[0] - c[0]) * (b[1] - c[1]) - (b[0] - c[0]) * (a[1] - c[1])

#         
def line_Flag(a,b,c,d):
   if (max(a[0], b[0]) < min(c[0], d[0])):
   	return False
   if (max(a[1], b[1]) < min(c[1], d[1])):
   	return False
   if (max(c[0], d[0]) < min(a[0], b[0])):
   	return False
   if (max(c[1], d[1]) < min(a[1], b[1])):
   	return False
   if (mult(c, b, a) * mult(b, d, a) < 0):
   	return False 
   if (mult(a, d, c) * mult(d, b, c) < 0):
   	return False
   return True


#        【x,y】     
def judge_Cell(p1,p2,c):
   #p1 p2        c 【x,y】
   x = c[0]
   y = c[1]
   if(line_Flag(p1,p2,[x - 0.5,y - 0.5],[x - 0.5,y + 0.5]) or line_Flag(p1,p2,[x + 0.5,y + 0.5],[x + 0.5,y - 0.5]) or line_Flag(p1,p2,[x + 0.5,y + 0.5],[x - 0.5,y + 0.5]) or line_Flag(p1,p2,[x + 0.5,y - 0.5],[x - 0.5,y - 0.5])):
       return True
   else:
       return False

#              +1
       for x in range(p_s[0],p_f[0] + 1):
           for y in range(p_s[1],p_f[1] + 1):
               if(judge_Cell([x0,y0],[x1,y1],[x,y])):
                   X[y][x]+=1
                   

4密度に基づいてホットマップを描画
min_density = input('          ')
        min_density = math.ceil(float(min_density))
    
        X1 = set(X,min_density)
    
        im = ax.imshow(X,cmap=plt.cm.summer,interpolation='spline36',vmin=d_min,vmax=d_max,aspect='equal')
        plt.colorbar(im, shrink=0.5)
        plt.show()

関心領域抽出
1密度>=しきい値のセル降順のセット
def set(X,min_density):
    set = []
    d_max = X[0][0]
    for i in X:
        for j in i:
            if j > d_max:
                d_max = j

    for density in range(d_max,min_density - 1,-1):
        index = np.argwhere(X == density)
        set.append(index)
    return set

2開拓区域
# [x,y]           
def extend(X,use,point,min_density):
    
    #print("------")

    region = [point,point]#                
    
    avg_density = X[point[1]][point[0]]
    
    #            
    while(avg_density >= min_density):
        x0 = region[0][0]
        y0 = region[0][1]
        x1 = region[1][0]
        y1 = region[1][1]
        left = 0
        right = 0
        up = 0
        down = 0
        #      
        if(x0 - 1 >= 0):    
            if(not (use_Region(use,[[x0 - 1,y0],[x0 - 1,y1]]))):
                region_l = [[x0 - 1,y0],[x1,y1]]
                left = get_Avg_Density(X,region_l)
        if(x1 + 1 <= x_l):
            if(not(use_Region(use,[[x1 + 1,y0],[x1 + 1,y1]]))):
                region_r = [[x0,y0],[x1 + 1,y1]]
                right = get_Avg_Density(X,region_r)
        if(y1 + 1 <= y_l):
            if(not(use_Region(use,[[x0,y1 + 1],[x1,y1 + 1]]))):
                region_u = [[x0,y0],[x1,y1 + 1]]
                up = get_Avg_Density(X,region_u)
        if(y0 - 1 >= 0):    
            if(not(use_Region(use,[[x0,y0 - 1],[x1,y0 - 1]]))):
                region_d = [[x0,y0 - 1],[x1,y1]]
                down = get_Avg_Density(X,region_d)

       # print("4:",left,right,up,down)
        max_density = max(left,right,up,down)
        #print("max",max_density)
        avg_density = max_density
        if(max_density >= min_density):           
            if(left == max_density):
                region = region_l
            elif (right == max_density):
                region = region_r
            elif(up == max_density):
                region = region_u
            else:
                region = region_d
            change_To_Use(use,region)
            #print(avg_density)
        else:
            draw_Region(region)

3図中枠選択ROI
#     
def draw_Region(region):
    x0 = region[0][0]
    y0 = region[0][1]
    x1 = region[1][0]
    y1 = region[1][1]
    #for x in range(x0,x1+1):
    #    for y in range(y0,y1+1):
    #        plt.scatter(x,y,c='black')
    rect = plt.Rectangle((x0 - 0.5,y0 - 0.5),x1 - x0 + 1,y1 - y0 + 1,linewidth=2,edgecolor='r',facecolor='none')
    plt.gca().add_patch(rect)