python实现区域生长及其原理

区域生长是一种串行区域分割的图像分割方法。区域生长是指从某个像素出发,按照一定的准则,逐步加入邻近像素,当满足一定的条件时,区域生长终止。区域生长的好坏决定于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实现:

二维平面图像

  1. import numpy as np

  2. import cv2

  3.  
  4. class Point(object):

  5. def __init__(self,x,y):

  6. self.x = x

  7. self.y = y

  8.  
  9. def getX(self):

  10. return self.x

  11. def getY(self):

  12. return self.y

  13.  
  14. def getGrayDiff(img,currentPoint,tmpPoint):

  15. return abs(int(img[currentPoint.x,currentPoint.y]) - int(img[tmpPoint.x,tmpPoint.y]))

  16.  
  17. def selectConnects(p):

  18. if p != 0:

  19. connects = [Point(-1, -1), Point(0, -1), Point(1, -1), Point(1, 0), Point(1, 1), \

  20. Point(0, 1), Point(-1, 1), Point(-1, 0)]

  21. else:

  22. connects = [ Point(0, -1), Point(1, 0),Point(0, 1), Point(-1, 0)]

  23. return connects

  24.  
  25. def regionGrow(img,seeds,thresh,p = 1):

  26. height, weight = img.shape

  27. seedMark = np.zeros(img.shape)

  28. seedList = []

  29. for seed in seeds:

  30. seedList.append(seed)

  31. label = 1

  32. connects = selectConnects(p)

  33. while(len(seedList)>0):

  34. currentPoint = seedList.pop(0)

  35.  
  36. seedMark[currentPoint.x,currentPoint.y] = label

  37. for i in range(8):

  38. tmpX = currentPoint.x + connects[i].x

  39. tmpY = currentPoint.y + connects[i].y

  40. if tmpX < 0 or tmpY < 0 or tmpX >= height or tmpY >= weight:

  41. continue

  42. grayDiff = getGrayDiff(img,currentPoint,Point(tmpX,tmpY))

  43. if grayDiff < thresh and seedMark[tmpX,tmpY] == 0:

  44. seedMark[tmpX,tmpY] = label

  45. seedList.append(Point(tmpX,tmpY))

  46. return seedMark

  47.  
  48.  
  49. img = cv2.imread('lean.png',0)

  50. seeds = [Point(10,10),Point(82,150),Point(20,300)]

  51. binaryImg = regionGrow(img,seeds,10)

  52. cv2.imshow(' ',binaryImg)

  53. cv2.waitKey(0)

三维体素数据:

  1. import numpy as np

  2.  
  3. def grow(img, seed, t):

  4. """

  5. img: ndarray, ndim=3

  6. An image volume.

  7.  
  8. seed: tuple, len=3

  9. Region growing starts from this point.

  10.  
  11. t: int

  12. The image neighborhood radius for the inclusion criteria.

  13. """

  14. seg = np.zeros(img.shape, dtype=np.bool)

  15. checked = np.zeros_like(seg)

  16.  
  17. seg[seed] = True

  18. checked[seed] = True

  19. needs_check = get_nbhd(seed, checked, img.shape)

  20.  
  21. while len(needs_check) > 0:

  22. pt = needs_check.pop()

  23.  
  24. # Its possible that the point was already checked and was

  25. # put in the needs_check stack multiple times.

  26. if checked[pt]: continue

  27.  
  28. checked[pt] = True

  29.  
  30. # Handle borders.

  31. imin = max(pt[0]-t, 0)

  32. imax = min(pt[0]+t, img.shape[0]-1)

  33. jmin = max(pt[1]-t, 0)

  34. jmax = min(pt[1]+t, img.shape[1]-1)

  35. kmin = max(pt[2]-t, 0)

  36. kmax = min(pt[2]+t, img.shape[2]-1)

  37.  
  38. if img[pt] >= img[imin:imax+1, jmin:jmax+1, kmin:kmax+1].mean():

  39. # Include the voxel in the segmentation and

  40. # add its neighbors to be checked.

  41. seg[pt] = True

  42. needs_check += get_nbhd(pt, checked, img.shape)

  43.  
  44. return seg

区域生长涉及种子选取,提供一个获取图像zuo'坐标的函数:

  1. def on_mouse(event, x,y, flags , params):

  2. if event == cv2.EVENT_LBUTTONDOWN:

  3. print('Seed: ' + 'Point' + '('+str(x) + ', ' + str(y)+')', imger[y, x])

  4. clicks.append((y, x))

  5. cv2.setMouseCallback('input', on_mouse, 0, )

‘input’是你显示图像的命名。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值