首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >种子区域生长图像分割方法及Python实例

种子区域生长图像分割方法及Python实例

作者头像
Minerva
修改2020-05-23 10:21:03
3.7K0
修改2020-05-23 10:21:03
举报

种子区域生长法

是从一组代表不同生长区域的种子像素开始,接下来将种子像素邻域里符合条件的像素合并到种子像素所代表的生长区域中,并将新添加的像素作为新的种子像素继续合并过程,直到找不到符合条件的新像素为止。该方法的关键是选择合适的初始种子像素以及合理的生长准则。

下面给大家展示一个区域生长的例子和Python代码

#首先是区域生长一些函数的定义:
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

首先我们创建一张含圆圈的图:

import numpy as np
import cv2
image = np.ones((256, 256))
cv2.circle(image, (256//2, 256//2), int(100), (0,0,0), -1)
cv2.circle(image, (256//2, 256//2), int(50), (255,255,255), -1)
cv2.imwrite('test.png', 255*image)

运行之后会输出一张图如下:

下面我们采用区域生长法只保留中间的白色圆圈

image_copy = image.copy()//255
seeds = [Point(256//2,256//2)]
binaryImg = regionGrow(image_copy,seeds,1)
cv2.imwrite('test1.png', 255 * binaryImg)

区域生长法需要设定种子点,我们将种子点设为图像的中心点,即白色圆圈的中心点,区域生长只能长出白色圆圈的部分,其他部分为0,就是黑色。

本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2020-03-05,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 Python编程和深度学习 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 种子区域生长法
相关产品与服务
图像处理
图像处理基于腾讯云深度学习等人工智能技术,提供综合性的图像优化处理服务,包括图像质量评估、图像清晰度增强、图像智能裁剪等。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档