關(guān)于初始種子自動(dòng)選取的區(qū)域生長(zhǎng)實(shí)例(python+opencv)
算法中,初始種子可自動(dòng)選擇(通過不同的劃分可以得到不同的種子,可按照自己需要改進(jìn)算法),圖分別為原圖(自己畫了兩筆為了分割成不同區(qū)域)、灰度圖直方圖、初始種子圖、區(qū)域生長(zhǎng)結(jié)果圖。
另外,不管時(shí)初始種子選擇還是區(qū)域生長(zhǎng),閾值選擇很重要。
import cv2 import numpy as np import matplotlib.pyplot as plt #初始種子選擇 def originalSeed(gray, th): ret, thresh = cv2.cv2.threshold(gray, th, 255, cv2.THRESH_BINARY)#二值圖,種子區(qū)域(不同劃分可獲得不同種子) kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (3,3))#3×3結(jié)構(gòu)元 thresh_copy = thresh.copy() #復(fù)制thresh_A到thresh_copy thresh_B = np.zeros(gray.shape, np.uint8) #thresh_B大小與A相同,像素值為0 seeds = [ ] #為了記錄種子坐標(biāo) #循環(huán),直到thresh_copy中的像素值全部為0 while thresh_copy.any(): Xa_copy, Ya_copy = np.where(thresh_copy > 0) #thresh_A_copy中值為255的像素的坐標(biāo) thresh_B[Xa_copy[0], Ya_copy[0]] = 255 #選取第一個(gè)點(diǎn),并將thresh_B中對(duì)應(yīng)像素值改為255 #連通分量算法,先對(duì)thresh_B進(jìn)行膨脹,再和thresh執(zhí)行and操作(取交集) for i in range(200): dilation_B = cv2.dilate(thresh_B, kernel, iterations=1) thresh_B = cv2.bitwise_and(thresh, dilation_B) #取thresh_B值為255的像素坐標(biāo),并將thresh_copy中對(duì)應(yīng)坐標(biāo)像素值變?yōu)? Xb, Yb = np.where(thresh_B > 0) thresh_copy[Xb, Yb] = 0 #循環(huán),在thresh_B中只有一個(gè)像素點(diǎn)時(shí)停止 while str(thresh_B.tolist()).count("255") > 1: thresh_B = cv2.erode(thresh_B, kernel, iterations=1) #腐蝕操作 X_seed, Y_seed = np.where(thresh_B > 0) #取處種子坐標(biāo) if X_seed.size > 0 and Y_seed.size > 0: seeds.append((X_seed[0], Y_seed[0]))#將種子坐標(biāo)寫入seeds thresh_B[Xb, Yb] = 0 #將thresh_B像素值置零 return seeds #區(qū)域生長(zhǎng) def regionGrow(gray, seeds, thresh, p): seedMark = np.zeros(gray.shape) #八鄰域 if p == 8: connection = [(-1, -1), (-1, 0), (-1, 1), (0, 1), (1, 1), (1, 0), (1, -1), (0, -1)] elif p == 4: connection = [(-1, 0), (0, 1), (1, 0), (0, -1)] #seeds內(nèi)無元素時(shí)候生長(zhǎng)停止 while len(seeds) != 0: #棧頂元素出棧 pt = seeds.pop(0) for i in range(p): tmpX = pt[0] + connection[i][0] tmpY = pt[1] + connection[i][1] #檢測(cè)邊界點(diǎn) if tmpX < 0 or tmpY < 0 or tmpX >= gray.shape[0] or tmpY >= gray.shape[1]: continue if abs(int(gray[tmpX, tmpY]) - int(gray[pt])) < thresh and seedMark[tmpX, tmpY] == 0: seedMark[tmpX, tmpY] = 255 seeds.append((tmpX, tmpY)) return seedMark path = "_rg.jpg" img = cv2.imread(path) gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) #hist = cv2.calcHist([gray], [0], None, [256], [0,256])#直方圖 seeds = originalSeed(gray, th=253) seedMark = regionGrow(gray, seeds, thresh=3, p=8) #plt.plot(hist) #plt.xlim([0, 256]) #plt.show() cv2.imshow("seedMark", seedMark) cv2.waitKey(0)
以上這篇關(guān)于初始種子自動(dòng)選取的區(qū)域生長(zhǎng)實(shí)例(python+opencv)就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
python實(shí)現(xiàn)人像動(dòng)漫化的示例代碼
這篇文章主要介紹了python實(shí)現(xiàn)人像動(dòng)漫化的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-05-05Python從函數(shù)參數(shù)類型引出元組實(shí)例分析
這篇文章主要介紹了Python從函數(shù)參數(shù)類型引出元組,結(jié)合實(shí)例形式分析了Python函數(shù)定義與使用中常見的三種參數(shù)類型,并簡(jiǎn)單分析了元組類型參數(shù)的使用,需要的朋友可以參考下2019-05-05Python實(shí)現(xiàn)簡(jiǎn)單生成驗(yàn)證碼功能【基于random模塊】
這篇文章主要介紹了Python實(shí)現(xiàn)簡(jiǎn)單生成驗(yàn)證碼功能,結(jié)合實(shí)例形式分析了Python基于random模塊生成隨機(jī)字符串的相關(guān)操作技巧,需要的朋友可以參考下2018-02-02python 定時(shí)器,實(shí)現(xiàn)每天凌晨3點(diǎn)執(zhí)行的方法
今天小編就為大家分享一篇python 定時(shí)器,實(shí)現(xiàn)每天凌晨3點(diǎn)執(zhí)行的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2019-02-02Python學(xué)習(xí)筆記之變量與轉(zhuǎn)義符
這篇文章主要介紹了Python學(xué)習(xí)筆記之變量與轉(zhuǎn)義符,本文從零開始學(xué)習(xí)Python,知識(shí)點(diǎn)很細(xì),有共同目標(biāo)的小伙伴可以一起來學(xué)習(xí)2023-03-03