当前位置: 移动技术网 > IT编程>脚本编程>Python > Python简单实现区域生长方式

Python简单实现区域生长方式

2020年03月09日  | 移动技术网IT编程  | 我要评论

10658456,叶海燕行为艺术照片,脂老虎同路人俱乐部

区域生长是一种串行区域分割的图像分割方法。区域生长是指从某个像素出发,按照一定的准则,逐步加入邻近像素,当满足一定的条件时,区域生长终止。区域生长的好坏决定于1.初始点(种子点)的选取。2.生长准则。3.终止条件。区域生长是从某个或者某些像素点出发,最后得到整个区域,进而实现目标的提取。

区域生长的原理:   

区域生长的基本思想是将具有相似性质的像素集合起来构成区域。具体先对每个需要分割的区域找一个种子像素作为生长起点,然后将种子像素和周围邻域中与种子像素有相同或相似性质的像素(根据某种事先确定的生长或相似准则来判定)合并到种子像素所在的区域中。将这些新像素当作新的种子继续上面的过程,直到没有满足条件的像素可被包括进来。这样一个区域就生长成了。

区域生长实现的步骤如下:

1. 对图像顺序扫描!找到第1个还没有归属的像素, 设该像素为(x0, y0);

2. 以(x0, y0)为中心, 考虑(x0, y0)的4邻域像素(x, y)如果(x0, y0)满足生长准则, 将(x, y)与(x0, y0)合并(在同一区域内), 同时将(x, y)压入堆栈;

3. 从堆栈中取出一个像素, 把它当作(x0, y0)返回到步骤2;

4. 当堆栈为空时!返回到步骤1;

5. 重复步骤1 - 4直到图像中的每个点都有归属时。生长结束。

python实现

import numpy as np
import cv2

class point(object):
 def __init__(self,x,y):
  self.x = x
  self.y = y

 def getx(self):
  return self.x
 def gety(self):
  return self.y

def getgraydiff(img,currentpoint,tmppoint):
 return abs(int(img[currentpoint.x,currentpoint.y]) - int(img[tmppoint.x,tmppoint.y]))

def selectconnects(p):
 if p != 0:
  connects = [point(-1, -1), point(0, -1), point(1, -1), point(1, 0), point(1, 1), \
     point(0, 1), point(-1, 1), point(-1, 0)]
 else:
  connects = [ point(0, -1), point(1, 0),point(0, 1), point(-1, 0)]
 return connects

def regiongrow(img,seeds,thresh,p = 1):
 height, weight = img.shape
 seedmark = np.zeros(img.shape)
 seedlist = []
 for seed in seeds:
  seedlist.append(seed)
 label = 1
 connects = selectconnects(p)
 while(len(seedlist)>0):
  currentpoint = seedlist.pop(0)

  seedmark[currentpoint.x,currentpoint.y] = label
  for i in range(8):
   tmpx = currentpoint.x + connects[i].x
   tmpy = currentpoint.y + connects[i].y
   if tmpx < 0 or tmpy < 0 or tmpx >= height or tmpy >= weight:
    continue
   graydiff = getgraydiff(img,currentpoint,point(tmpx,tmpy))
   if graydiff < thresh and seedmark[tmpx,tmpy] == 0:
    seedmark[tmpx,tmpy] = label
    seedlist.append(point(tmpx,tmpy))
 return seedmark


img = cv2.imread('lean.png',0)
seeds = [point(10,10),point(82,150),point(20,300)]
binaryimg = regiongrow(img,seeds,10)
cv2.imshow(' ',binaryimg)
cv2.waitkey(0)

以上这篇python简单实现区域生长方式就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持移动技术网。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网