概述
单元格密度计算
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)
最后
以上就是鳗鱼老鼠为你收集整理的Python提取感兴趣区域ROI案例(二)实现过程单元格密度计算感兴趣区域提取的全部内容,希望文章能够帮你解决Python提取感兴趣区域ROI案例(二)实现过程单元格密度计算感兴趣区域提取所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复