區域生長是一種串行區域分割的圖像分割方法。區域生長是指從某個像素出發,按照一定的準則,逐步加入鄰近像素,當滿足一定的條件時,區域生長終止。區域生長的好壞決定于1.初始點(種子點)的選取。2.生長準則。3.終止條件。區域生長是從某個或者某些像素點出發,最后得到整個區域,進而實現目標的提取。
區域生長的原理:
區域生長的基本思想是將具有相似性質的像素集合起來構成區域。具體先對每個需要分割的區域找一個種子像素作為生長起點,然后將種子像素和周圍鄰域中與種子像素有相同或相似性質的像素(根據某種事先確定的生長或相似準則來判定)合并到種子像素所在的區域中。將這些新像素當作新的種子繼續上面的過程,直到沒有滿足條件的像素可被包括進來。這樣一個區域就生長成了。
區域生長實現的步驟如下:
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直到圖像中的每個點都有歸屬時。生長結束。
Python實現
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
|
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簡單實現區域生長方式就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持服務器之家。
原文鏈接:https://blog.csdn.net/bagboy_taobao_com/article/details/5666091