種子生長演算法

2021-08-20 23:31:23 字數 2767 閱讀 3702

# -*- coding: utf-8 -*-

''' * @author: zq.pei

* @date: 2018-06-03 16:59:35

* @last modified by: zq.pei

* @last modified time: 2018-06-03 16:59:35

'''from _stack import stack

import cv2

import numpy as np

import time

class growseedalgo(object):

'''only for binary image

'''def __init__(self, im=none, im_path=none):

self.stack = stack()

if im is not none:

self.im = im

elif im_path is not none:

self.imread(im_path)

else:

raise valueerror("no input image!")

self.im_size = self.im.shape

self.im_height, self.im_width = self.im_size

self.im_label = np.full_like(self.im, 0)

#import ipdb; ipdb.set_trace()

self.max_label = 0

def imread(self, im_path):

self.im_path = im_path

self.im = cv2.imread(im_path)

def start(self):

for x0 in range(self.im_height):

for y0 in range(self.im_width):

if self.im_label[x0,y0] == 0 and self.im[x0,y0] != 0: # self.im[x0,y0]!=0 get rid of background

self.max_label += 1

self.im_label[x0,y0] = self.max_label

self.stack.push((x0,y0))

while not self.stack.is_empty():

x,y = self.stack.pop()

self.grow(x,y)

cv2.imshow('growseed',self.im_label/self.max_label)

cv2.waitkey(0)

def grow(self, x0,y0):

current_label = self.im_label[x0,y0]

for x,y in self._get_neighbour(x0,y0):

if self.im_label[x,y] == 0 and self.im[x,y] == self.im[x0,y0]: # threshold

self.im_label[x,y] = current_label

self.stack.push((x,y))

#print((self.im_label/self.max_label).shape)

#cv2.imshow('growseed',self.im_label/self.max_label)

#cv2.waitkey(1)

def _get_neighbour(self, x0, y0):

neighbour =

for i in (-1,0,1):

for j in (-1,0,1):

if (i,j) == (0,0):

continue

x = x0+i

y = y0+j

if self._in_region(x,y):

return neighbour

def _in_region(self, x,y):

return true if 0<=x125)*255).astype(np.uint8)[:,:]

# cv2.imshow('bin_im',bin_im)

# cv2.waitkey(0)

gs = growseedalgo(im=bin_im)

gs.start()

if __name__ == '__main__':

main()

1.對影象順序掃瞄,找到第乙個還沒有標記的畫素,設該畫素為(x0,y0),若之前已有k類,則標記(x0,y0)為第k+1類,並把(x0,y0)壓入堆疊stack

2.,從堆疊中取出乙個畫素當做(x0,y0),以(x0,y0)為中心考慮(x0,y0)的8領域(或者4領域)的畫素(x,y),如果(x,y)(x0,y0)畫素值在一定範圍內,把(x,y)也標記為第k+1類,並把(x,y)壓入堆疊stack

3.重複執行步驟2直到堆疊為空,當堆疊為空時,返回到步驟1

4.重複執行步驟1-3直到影象中的每個點都被標記,生長結束。

區域生長演算法

區域生長演算法 既是根據事先定義的準則將畫素或者子區域聚合成更大的區域。基本方法是以 一組 種子開始,將與種子性質相似 灰度級或顏色的特定範圍 的相鄰畫素附加到生長區域的種子上。halcon中的區域生長運算元 區域生長演算法,將圖象被分割為區域 regiongrowing image regions...

區域生長演算法

把影象進行分割為多個區域。方案1,尋找灰度級不連續區域間的邊界 方案2,基於畫素性質分布的閾值處理 方案3,直接尋找區域的分割技術 區域生長就是方案三,它是根據事先定義的準則將畫素或者子區域聚合成更大區域的過程。其基本思想是從一組生長點開始 生長點可以是單個畫素,也可以是某個小區域 將與該生長點性質...

種子填充演算法

在多邊形區域內部填充某元素。種子演算法,即從內部某一點開始填充,再遞迴填充周圍的點 上下左右 四連通 直到遇到邊界。void tianchong int x,int y,int color 在實際應用中,出現了遞迴呼叫棧溢位的情況,先在這裡留坑,下次再補上具體的資料吧。基礎演算法在實際應用中,除了上...