# Python实现区域生长算法（regionGrow）

OpenCV 同时被 2 个专栏收录
11 篇文章 0 订阅
8 篇文章 0 订阅

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直到图像中的每个点都有归属时。生长结束。

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)

import numpy as np

def grow(img, seed, t):
"""
img: ndarray, ndim=3
An image volume.

seed: tuple, len=3
Region growing starts from this point.

t: int
The image neighborhood radius for the inclusion criteria.
"""
seg = np.zeros(img.shape, dtype=np.bool)
checked = np.zeros_like(seg)

seg[seed] = True
checked[seed] = True
needs_check = get_nbhd(seed, checked, img.shape)

while len(needs_check) > 0:
pt = needs_check.pop()

# Its possible that the point was already checked and was
# put in the needs_check stack multiple times.
if checked[pt]: continue

checked[pt] = True

# Handle borders.
imin = max(pt[0]-t, 0)
imax = min(pt[0]+t, img.shape[0]-1)
jmin = max(pt[1]-t, 0)
jmax = min(pt[1]+t, img.shape[1]-1)
kmin = max(pt[2]-t, 0)
kmax = min(pt[2]+t, img.shape[2]-1)

if img[pt] >= img[imin:imax+1, jmin:jmax+1, kmin:kmax+1].mean():
# Include the voxel in the segmentation and
# add its neighbors to be checked.
seg[pt] = True
needs_check += get_nbhd(pt, checked, img.shape)

return seg

def on_mouse(event, x,y, flags , params):
if event == cv2.EVENT_LBUTTONDOWN:
print('Seed: ' + 'Point' + '('+str(x) + ', ' + str(y)+')', imger[y, x])
clicks.append((y, x))
cv2.setMouseCallback('input', on_mouse, 0, )

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

11-16

07-09 8839
12-29 1600
12-10 3420
07-26 6173
09-18
08-21
09-25 1440
04-16
©️2020 CSDN 皮肤主题: 游动-白 设计师:白松林

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