python+opencv如何初始種子自動(dòng)選取的區(qū)域生長-創(chuàng)新互聯(lián)

這篇文章主要介紹python+opencv如何初始種子自動(dòng)選取的區(qū)域生長,文中介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們一定要看完!

成都創(chuàng)新互聯(lián)公司專業(yè)為企業(yè)提供市南網(wǎng)站建設(shè)、市南做網(wǎng)站、市南網(wǎng)站設(shè)計(jì)、市南網(wǎng)站制作等企業(yè)網(wǎng)站建設(shè)、網(wǎng)頁設(shè)計(jì)與制作、市南企業(yè)網(wǎng)站模板建站服務(wù),十載市南做網(wǎng)站經(jīng)驗(yàn),不只是建網(wǎng)站,更提供有價(jià)值的思路和整體網(wǎng)絡(luò)服務(wù)。

算法中,初始種子可自動(dòng)選擇(通過不同的劃分可以得到不同的種子,可按照自己需要改進(jìn)算法),圖分別為原圖(自己畫了兩筆為了分割成不同區(qū)域)、灰度圖直方圖、初始種子圖、區(qū)域生長結(jié)果圖。

另外,不管時(shí)初始種子選擇還是區(qū)域生長,閾值選擇很重要。

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中對應(yīng)像素值改為255

  #連通分量算法,先對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中對應(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ū)域生長
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í)候生長停止
 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]

   #檢測邊界點(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)

python+opencv如何初始種子自動(dòng)選取的區(qū)域生長

python+opencv如何初始種子自動(dòng)選取的區(qū)域生長

python+opencv如何初始種子自動(dòng)選取的區(qū)域生長

python+opencv如何初始種子自動(dòng)選取的區(qū)域生長

以上是“python+opencv如何初始種子自動(dòng)選取的區(qū)域生長”這篇文章的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對大家有幫助,更多相關(guān)知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)成都網(wǎng)站設(shè)計(jì)公司行業(yè)資訊頻道!

另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時(shí)售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務(wù)可用性高、性價(jià)比高”等特點(diǎn)與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場景需求。

分享標(biāo)題:python+opencv如何初始種子自動(dòng)選取的區(qū)域生長-創(chuàng)新互聯(lián)
轉(zhuǎn)載注明:http://www.muchs.cn/article44/deoghe.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站設(shè)計(jì)公司、Google、微信公眾號(hào)、網(wǎng)站導(dǎo)航、標(biāo)簽優(yōu)化網(wǎng)站改版

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

外貿(mào)網(wǎng)站建設(shè)