您好,登錄后才能下訂單哦!
區(qū)域生長(zhǎng)是一種串行區(qū)域分割的圖像分割方法。區(qū)域生長(zhǎng)是指從某個(gè)像素出發(fā),按照一定的準(zhǔn)則,逐步加入鄰近像素,當(dāng)滿足一定的條件時(shí),區(qū)域生長(zhǎng)終止。區(qū)域生長(zhǎng)的好壞決定于1.初始點(diǎn)(種子點(diǎn))的選取。2.生長(zhǎng)準(zhǔn)則。3.終止條件。區(qū)域生長(zhǎng)是從某個(gè)或者某些像素點(diǎn)出發(fā),最后得到整個(gè)區(qū)域,進(jìn)而實(shí)現(xiàn)目標(biāo)的提取。
區(qū)域生長(zhǎng)的原理:
區(qū)域生長(zhǎng)的基本思想是將具有相似性質(zhì)的像素集合起來構(gòu)成區(qū)域。具體先對(duì)每個(gè)需要分割的區(qū)域找一個(gè)種子像素作為生長(zhǎng)起點(diǎn),然后將種子像素和周圍鄰域中與種子像素有相同或相似性質(zhì)的像素(根據(jù)某種事先確定的生長(zhǎng)或相似準(zhǔn)則來判定)合并到種子像素所在的區(qū)域中。將這些新像素當(dāng)作新的種子繼續(xù)上面的過程,直到?jīng)]有滿足條件的像素可被包括進(jìn)來。這樣一個(gè)區(qū)域就生長(zhǎng)成了。
區(qū)域生長(zhǎng)實(shí)現(xiàn)的步驟如下:
1. 對(duì)圖像順序掃描!找到第1個(gè)還沒有歸屬的像素, 設(shè)該像素為(x0, y0);
2. 以(x0, y0)為中心, 考慮(x0, y0)的4鄰域像素(x, y)如果(x0, y0)滿足生長(zhǎng)準(zhǔn)則, 將(x, y)與(x0, y0)合并(在同一區(qū)域內(nèi)), 同時(shí)將(x, y)壓入堆棧;
3. 從堆棧中取出一個(gè)像素, 把它當(dāng)作(x0, y0)返回到步驟2;
4. 當(dāng)堆棧為空時(shí)!返回到步驟1;
5. 重復(fù)步驟1 - 4直到圖像中的每個(gè)點(diǎn)都有歸屬時(shí)。生長(zhǎng)結(jié)束。
Python實(shí)現(xiàn)
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)
以上這篇Python簡(jiǎn)單實(shí)現(xiàn)區(qū)域生長(zhǎng)方式就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持億速云。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。