【youcans 的 OpenCV 例程200篇】168.图像分割之区域生长

欢迎关注 『youcans 的 OpenCV 例程 200 篇』 系列,持续更新中
欢迎关注 『youcans 的 OpenCV学习课』 系列,持续更新中


【youcans 的 OpenCV 例程200篇】168.图像分割之区域生长


4. 区域分割的基本方法

4.1 区域生长

区域生长方法将具有相似性质的像素或子区域组合为更大区域。

区域生长方法是以区域为处理对象,基于区域内部和区域之间的同异性,尽量保持区域中像素的临近性和一致性的统一 。

区域生长的基本方法是,对于一组“种子”点,通过把与种子具有相同预定义性质(如灰度或颜色范围)的邻域像素合并到种子像素所在的区域中,再将新像素作为新的种子不断重复这一过程,直到没有满足条件的像素为止。

种子点的选取经常采用人工交互方法实现,也可以寻找目标物体并提取物体内部点,或利用其它算法找到的特征点作为种子点。

区域增长方法的步骤:
(1)对图像自上而下、从左向右扫描,找到第 1 个还没有访问过的像素,将该像素作为种子 (x0, y0);
(2)以 (x0, y0) 为中心, 考虑其 4 邻域或 8 邻域像素 (x, y),如果其邻域满足生长准则 则将 (x, y) 与 (x0, y0) 合并到同一区域,同时将 (x, y) 压入堆栈;
(3)从堆栈中取出一个像素,作为种子 (x0, y0) 继续步骤(2);
(4)当堆栈为空时返回步骤(1);
(5)重复步骤(1)-(4),直到图像中的每个点都被访问过,算法结束。


例程 11.25:图像分割之区域生长

    # # 11.25 图像分割之区域生长
    def getGrayDiff(image, currentPoint, tmpPoint):  # 求两个像素的距离
        return abs(int(image[currentPoint[0], currentPoint[1]]) - int(image[tmpPoint[0], tmpPoint[1]]))

    # 区域生长算法
    def regional_growth(img, seeds, thresh=5):
        height, weight = img.shape
        seedMark = np.zeros(img.shape)
        seedList = []
        for seed in seeds:
            if (0<seed[0]<height and 0<seed[1]<weight): seedList.append(seed)
        label = 1  # 种子位置标记
        connects = [(-1,-1), (0,-1), (1,-1), (1,0), (1,1), (0,1), (-1,1), (-1,0)]  # 8 邻接连通
        while (len(seedList) > 0):  # 如果列表里还存在点
            currentPoint = seedList.pop(0)  # 将最前面的那个抛出
            seedMark[currentPoint[0], currentPoint[1]] = label  # 将对应位置的点标记为 1
            for i in range(8):  # 对这个点周围的8个点一次进行相似性判断
                tmpX = currentPoint[0] + connects[i][0]
                tmpY = currentPoint[1] + connects[i][1]
                if tmpX<0 or tmpY<0 or tmpX>=height or tmpY>=weight:  # 是否超出限定阈值
                    continue
                grayDiff = getGrayDiff(img, currentPoint, (tmpX, tmpY))  # 计算灰度差
                if grayDiff<thresh and seedMark[tmpX,tmpY]==0:
                    seedMark[tmpX, tmpY] = label
                    seedList.append((tmpX, tmpY))
        return seedMark


    # 区域生长 主程序
    img = cv2.imread("../images/Fig1051a.tif", flags=0)
    # # 灰度直方图
    # histCV = cv2.calcHist([img], [0], None, [256], [0, 256])  # 灰度直方图
    # OTSU 全局阈值处理
    ret, imgOtsu = cv2.threshold(img, 127, 255, cv2.THRESH_OTSU)  # 阈值分割, thresh=T
    # 自适应局部阈值处理
    binaryMean = cv2.adaptiveThreshold(img, 255, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY, 5, 3)
    # 区域生长图像分割
    # seeds = [(10, 10), (82, 150), (20, 300)]  # 直接给定 种子点
    imgBlur = cv2.blur(img, (3,3))  # cv2.blur 方法
    _, imgTop = cv2.threshold(imgBlur, 250, 255, cv2.THRESH_BINARY)  # 高百分位阈值产生种子区域
    nseeds, labels, stats, centroids = cv2.connectedComponentsWithStats(imgTop)  # 过滤连通域,获得质心点 (x,y)
    seeds = centroids.astype(int)  # 获得质心像素作为种子点
    imgGrowth = regional_growth(img, seeds, 8)

    plt.figure(figsize=(8, 6))
    plt.subplot(221), plt.axis('off'), plt.title("Origin")
    plt.imshow(img, 'gray')
    plt.subplot(222), plt.axis('off'), plt.title("OTSU(T={})".format(ret))
    plt.imshow(imgOtsu, 'gray')
    plt.subplot(223), plt.axis('off'), plt.title("Adaptive threshold")
    plt.imshow(binaryMean, 'gray')
    plt.subplot(224), plt.axis('off'), plt.title("Region grow")
    plt.imshow(255-imgGrowth, 'gray')
    plt.tight_layout()
    plt.show()

【youcans 的 OpenCV 例程200篇】168.图像分割之区域生长_第1张图片


(本节完)


版权声明:

youcans@xupt 原创作品,转载必须标注原文链接:(https://blog.csdn.net/youcans/article/details/124472048)

Copyright 2022 youcans, XUPT
Crated:2022-4-30


欢迎关注 『youcans 的 OpenCV 例程 200 篇』 系列,持续更新中
欢迎关注 『youcans 的 OpenCV学习课』 系列,持续更新中

【youcans 的 OpenCV 例程200篇】168.图像分割之区域生长
【youcans 的 OpenCV 例程200篇】169.图像分割之区域分离
【youcans 的 OpenCV 例程200篇】170.图像分割之K均值聚类
更多内容,请见:
【OpenCV 例程200篇 总目录-202206更新】

你可能感兴趣的:(opencv,python,图像处理,计算机视觉,算法)