无聊之作-区域生长、区域分裂与聚合

本文深入探讨了图像处理中的区域生长与区域分裂聚合算法,详细介绍了两种算法的工作原理及其实现过程。区域生长算法从种子点出发,通过判断相邻像素的相似度进行区域扩展;而区域分裂聚合算法则是通过递归地分割和聚合区域来实现图像的分割。

区域生长就是从一或者多个人为设定的种子点,根据周围8领域的像素进行以此判断,如果在阈值之间则分割出来并压入种子栈中,直到种子栈为空

 

import cv2 as cv
import numpy as np

def region_seed_grow(img,seed,low_bound,high_bound):
    stack=[seed]

    res=np.zeros(img.shape)

    while stack:
        s=stack.pop()
        i=s[0]
        j=s[1]
        res[i][j]=255
        #print(i,j)
        if i!=0 and j!=0 and res[i-1][j-1]==0 and abs(int(img[i][j])-int(img[i-1][j-1]))>=low_bound and abs(int(img[i][j])-int(img[i-1][j-1]))<=high_bound:
            stack.append([i-1,j-1])
        if i!=0 and res[i-1][j]==0 and abs(int(img[i][j])-int(img[i-1][j]))>=low_bound and abs(int(img[i][j])-int(img[i-1][j]))<=high_bound:
            stack.append([i-1,j])
        if i!=0 and j!=img.shape[1]-1 and res[i-1][j+1]==0 and abs(int(img[i][j])-int(img[i-1][j+1]))>=low_bound and abs(int(img[i][j])-int(img[i-1][j+1]))<=high_bound:
            stack.append([i-1,j+1])
        if j!=0 and res[i][j-1]==0 and abs(int(img[i][j])-int(img[i][j-1]))>=low_bound and abs(int(img[i][j])-int(img[i][j-1]))<=high_bound:
            stack.append([i,j-1])
        if j!=img.shape[1]-1 and res[i][j+1]==0 and abs(int(img[i][j])-int(img[i][j+1]))>=low_bound and abs(int(img[i][j])-int(img[i][j+1]))<=high_bound:
            stack.append([i,j+1])
        if i!=img.shape[0]-1 and j!=0 and res[i+1][j-1]==0 and abs(int(img[i][j])-int(img[i+1][j-1]))>=low_bound and abs(int(img[i][j])-int(img[i+1][j-1]))<=high_bound:
            stack.append([i+1,j-1])
        if i!=img.shape[0]-1 and res[i+1][j]==0 and abs(int(img[i][j])-int(img[i+1][j]))>=low_bound and abs(int(img[i][j])-int(img[i+1][j]))<=high_bound:
            stack.append([i+1,j])
        if i!=img.shape[0]-1 and j!=img.shape[1]-1 and res[i+1][j+1]==0 and abs(int(img[i][j])-int(img[i+1][j+1]))>=low_bound and abs(int(img[i][j])-int(img[i+1][j+1]))<=high_bound:
            stack.append([i+1,j+1])

    return res


image = cv.imread('grow.png')
gray = cv.cvtColor(image, cv.COLOR_RGB2GRAY)

res=region_seed_grow(gray,[60,580],0,10)

cv.namedWindow("gray")
cv.imshow("gray",gray)
cv.waitKey(0)

cv.namedWindow("res")
cv.imshow("res",res)
cv.waitKey(0)



原图入上图所示,结果如下图

 

 

区域分裂与聚合就是判断一个区域的均值和方差是不是在人为设定的阈值中,如果是的话这个区域分割出来,不是的话就将这个区域分为左上、右上、左下、右下四个部分再递归判断,直到最后结束

import cv2 as cv
import numpy as np

class region_div:
    def __init__(self,img):
        self.img=img
        self.res=np.zeros(img.shape)


    def region_div_group(self,range1,range2):
        if range1[1]-range1[0]==0 or range2[1]-range2[0]==0:
            return

        mean=self.img[range1[0]:range1[1],range2[0]:range2[1]].mean()
        var=self.img[range1[0]:range1[1],range2[0]:range2[1]].var()
        #print(self.img[range1[0]:range1[1],range2[0]:range2[1]])
        print(range1, range2,var)
        if var<10:
            self.res[range1[0]:range1[1],range2[0]:range2[1]]=255
        else:
            if range1[1]-range1[0]>=2 and range2[1]-range2[0]>=2:
                self.region_div_group([range1[0],(range1[0]+range1[1])//2],[range2[0],(range2[0]+range2[1])//2])
                self.region_div_group([ (range1[0] + range1[1]) // 2,range1[1]], [range2[0], (range2[0] + range2[1]) // 2])
                self.region_div_group( [range1[0], (range1[0] + range1[1]) // 2], [(range2[0] + range2[1]) // 2, range2[1]])
                self.region_div_group( [(range1[0] + range1[1]) // 2,range1[1]], [(range2[0] + range2[1]) // 2, range2[1]])


image = cv.imread('grow.png')
gray = cv.cvtColor(image, cv.COLOR_RGB2GRAY)
print(gray.shape)
print(gray[0:1,5:11])
res=np.zeros(gray.shape)
rd=region_div(gray)
rd.region_div_group([0,gray.shape[0]],[0,gray.shape[1]])

res=rd.res
cv.namedWindow("gray")
cv.imshow("gray",gray)
cv.waitKey(0)

cv.namedWindow("res")
cv.imshow("res",res)
cv.waitKey(0)

结果如下:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值