基于区域方法的Python图像分割
我正在尝试用生长区域法对n个区域进行图像分割。在下面的代码中,我对我正在采取的不同步骤进行了注释。在打印一些东西时,似乎我没有遍历整个图像,我不知道为什么。我有什么明显的遗漏吗基于区域方法的Python图像分割,python,Python,我正在尝试用生长区域法对n个区域进行图像分割。在下面的代码中,我对我正在采取的不同步骤进行了注释。在打印一些东西时,似乎我没有遍历整个图像,我不知道为什么。我有什么明显的遗漏吗 def neighbouring(): return [[-1, -1], [0, -1], [1, -1], [1, 0], [1, 1], [0, 1], [-1, 1], [-1, 0]] def GrowingRegions(img, seeds, thresh): height, weight
def neighbouring():
return [[-1, -1], [0, -1], [1, -1], [1, 0], [1, 1], [0, 1], [-1, 1], [-1, 0]]
def GrowingRegions(img, seeds, thresh):
height, weight = img.shape
neighbours = neighbouring()
means = []
regions = []
seen = []
for p in seeds: #for each point in the seeds
#if p is already associated to a region, skip it
for points in seen:
if np.array_equal(p, points):
continue
#create a new region r
r = [p]
#compute the mean
mean_r = img[p[0], p[1]]
#create a list of neighbours for the point p
n = [np.array((p[0] + neighbours[i][0], p[1] + neighbours[i][1])) for i in range(8)]
#for each point in the neighbours
for point in n:
(tempX, tempY) = point
#check if you are within the image
if tempX < 0 or tempY < 0 or tempX >= height or tempY >= weight:
continue
#compute the gray level difference between the image at the point and the mean of the region
gray_diff = abs(img[point[0]][point[1]] - mean_r)
#check if the point is already associated to a region
res = True
for points in seen:
if np.array_equal(point, points):
res = False
#if it is not and the gray level difference is within threshold
if res and (gray_diff < thresh):
#we add the point to the region
r.append(point)
#we add it to the seen list as the point has been associated to the region
seen.append(point)
#we updated the neighhbours with the neighbours of the point
neigh_p = [np.array((point[0] + neighbours[i][0], point[1] + neighbours[i][1])) for i in range(8)]
n += neigh_p #add the neighbours of the point we just considered
#update the mean
mean_r = np.mean(r) #update the mean
regions.append(r) #add the final region to the regions list
#here we want to associated each x,y to a label
#the regions list to have [[points in region 1], [points in region 2], [points in region 3]...]
#we take each point in region 1 (for example), and set its label to the mean of region 1 and so on
labels = np.zeros(img.shape)
means = []
for r in regions:
mean = 0
for i in range(len(r)):
mean += img[r[i][0], r[i][1]]
means.append(mean/len(r))
for i,r in enumerate(regions):
for points in r:
x,y = points
labels[x,y] = means[i] #set the label of x,y as the mean of the region
return labels
def():
返回[-1,-1]、[0,-1]、[1,-1]、[1,0]、[1,1]、[0,1]、-1,1]、-1,0]]
def生长区域(img、种子、脱粒):
高度、重量=img.shape
邻居
平均数=[]
区域=[]
SEED=[]
对于种子中的p:#对于种子中的每个点
#如果p已关联到某个区域,请跳过它
对于seen中的点:
如果np.数组_相等(p,点):
持续
#创建一个新的区域r
r=[p]
#计算平均数
平均值=img[p[0],p[1]]
#创建点p的邻居列表
n=[np.数组((p[0]+邻域[i][0],p[1]+邻域[i][1]),用于范围(8)中的i)
#对于邻居中的每个点
对于n点:
(tempX,tempY)=点
#检查您是否在图像中
如果tempX<0或tempY<0或tempX>=身高或tempY>=体重:
继续
#计算点处图像与区域平均值之间的灰度差
灰色差异=绝对值(img[点[0]][点[1]]-平均值)
#检查该点是否已关联到某个区域
res=真
对于seen中的点:
如果np.数组_相等(点,点):
res=False
#如果不是,则灰度差在阈值内
如果分辨率和(灰度差值<阈值):
#我们将该点添加到该区域
r、 附加(点)
#我们将其添加到SEED列表中,因为该点已与该区域关联
seen.append(点)
#我们用该点的邻居更新了附近的街道
neigh_p=[np.数组((点[0]+邻域[i][0],点[1]+邻域[i][1]),用于范围(8)内的i)
n+=neigh_p#加上我们刚才考虑的点的邻域
#更新平均值
平均值r=np.mean(r)#更新平均值
区域。追加(r)#将最后一个区域添加到区域列表中
#这里我们要将每个x,y关联到一个标签
#区域列表中有[[区域1中的点]、[区域2中的点]、[区域3中的点]…]
#我们以区域1中的每个点为例,将其标签设置为区域1的平均值,依此类推
标签=np.零(img.形状)
平均数=[]
对于区域内的r:
平均值=0
对于范围内的i(len(r)):
平均值+=img[r[i][0],r[i][1]]
平均值。附加(平均值/长度(r))
对于枚举中的i,r(区域):
对于r中的点:
x、 y=点
labels[x,y]=表示[i]#将x,y的标签设置为区域的平均值
退货标签
编辑:
我使用10个种子点,每个像素值被归一化为0到1之间,但我仍然收到一个黑色图像
def neighbouring():
return [[-1, -1], [0, -1], [1, -1], [1, 0], [1, 1], [0, 1], [-1, 1], [-1, 0]]
def GrowingRegions(img, seeds, thresh):
height, weight = img.shape
neighbours = neighbouring()
regions = []
seen = []
for p in seeds:
for points in seen:
if np.array_equal(p, points):
continue
r = [p]
mean_r = img[p[0], p[1]]
n = [np.array([p[0] + neighbours[i][0], p[1] + neighbours[i][1]]) for i in range(8)]
new_n = n.copy()
while len(new_n) > 0:
for point in n:
new_n.remove(point)
(tempX, tempY) = point
if tempX < 0 or tempY < 0 or tempX >= height or tempY >= weight:
continue
gray_diff = abs(img[point[0]][point[1]] - mean_r)
res = True
for points in seen:
if np.array_equal(point, points):
res = False
if res and (gray_diff < thresh):
r.append(point)
seen.append(point)
neigh_p = [np.array((point[0] + neighbours[i][0], point[1] + neighbours[i][1])) for i in range(8)]
n.extend(neigh_p)
new_n.extend(neigh_p)
mean_r = np.mean(r)
n = new_n
regions.append(r)
labels = np.zeros(img.shape)
means = []
for r in regions:
mean = 0
for i in range(len(r)):
mean += img[r[i][0], r[i][1]]
means.append(mean/len(r))
for i,r in enumerate(regions):
for points in r:
x,y = points
labels[x,y] = means[i] #set the label of x,y as the mean of the region
return labels
def():
返回[-1,-1]、[0,-1]、[1,-1]、[1,0]、[1,1]、[0,1]、-1,1]、-1,0]]
def生长区域(img、种子、脱粒):
高度、重量=img.shape
邻居
区域=[]
SEED=[]
对于种子中的磷:
对于seen中的点:
如果np.数组_相等(p,点):
持续
r=[p]
平均值=img[p[0],p[1]]
n=[np.数组([p[0]+邻域[i][0],p[1]+邻域[i][1]]),用于范围(8)中的i)
新的拷贝
而len(new_n)>0:
对于n点:
新删除(点)
(tempX,tempY)=点
如果tempX<0或tempY<0或tempX>=身高或tempY>=体重:
继续
灰色差异=绝对值(img[点[0]][点[1]]-平均值)
res=真
对于seen中的点:
如果np.数组_相等(点,点):
res=False
如果分辨率和(灰度差值<阈值):
r、 附加(点)
seen.append(点)
neigh_p=[np.数组((点[0]+邻域[i][0],点[1]+邻域[i][1]),用于范围(8)内的i)
n、 扩展(neigh_p)
新扩展(neigh\p)
平均值=np.平均值(r)
n=新的
区域。追加(r)
标签=np.零(img.形状)
平均数=[]
对于区域内的r:
平均值=0
对于范围内的i(len(r)):
平均值+=img[r[i][0],r[i][1]]
平均值。附加(平均值/长度(r))
对于枚举中的i,r(区域):
对于r中的点:
x、 y=点
labels[x,y]=表示[i]#将x,y的标签设置为区域的平均值
退货标签
种子位置是否完全覆盖图像的所有区域?您可能需要使用更多的种子位置我也尝试了10个种子,但最终的图像对我来说只是黑色的。我怀疑,因为我的循环没有循环通过所有像素,我实际上没有正确地“增长”我的区域。每当你将它添加到一个区域的相邻点时,你需要将它添加到某个队列中。然后,您需要不断地遍历该队列,对于排队点的每个相邻点,执行相同的操作,添加未分配的邻居,直到队列最终为空。现在,除了将相邻点指定给区域外,您不使用它们。谢谢您的回答,但我使用我的邻居列表n(我将poi的邻居添加到该列表中)