激情久久久_欧美视频区_成人av免费_不卡视频一二三区_欧美精品在欧美一区二区少妇_欧美一区二区三区的

腳本之家,腳本語言編程技術及教程分享平臺!
分類導航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|

服務器之家 - 腳本之家 - Python - 關于初始種子自動選取的區域生長實例(python+opencv)

關于初始種子自動選取的區域生長實例(python+opencv)

2020-04-19 11:15清熱降火 Python

今天小編就為大家分享一篇關于初始種子自動選取的區域生長實例(python+opencv),具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧

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

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

?
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
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)#二值圖,種子區域(不同劃分可獲得不同種子)
 kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (3,3))#3×3結構元
 
 thresh_copy = thresh.copy() #復制thresh_A到thresh_copy
 thresh_B = np.zeros(gray.shape, np.uint8) #thresh_B大小與A相同,像素值為0
 
 seeds = [ ] #為了記錄種子坐標
 
 #循環,直到thresh_copy中的像素值全部為0
 while thresh_copy.any():
 
  Xa_copy, Ya_copy = np.where(thresh_copy > 0) #thresh_A_copy中值為255的像素的坐標
  thresh_B[Xa_copy[0], Ya_copy[0]] = 255 #選取第一個點,并將thresh_B中對應像素值改為255
 
  #連通分量算法,先對thresh_B進行膨脹,再和thresh執行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的像素坐標,并將thresh_copy中對應坐標像素值變為0
  Xb, Yb = np.where(thresh_B > 0)
  thresh_copy[Xb, Yb] = 0
 
  #循環,在thresh_B中只有一個像素點時停止
  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) #取處種子坐標
  if X_seed.size > 0 and Y_seed.size > 0:
   seeds.append((X_seed[0], Y_seed[0]))#將種子坐標寫入seeds
  thresh_B[Xb, Yb] = 0 #將thresh_B像素值置零
 return seeds
 
#區域生長
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內無元素時候生長停止
 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]
 
   #檢測邊界點
   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)

關于初始種子自動選取的區域生長實例(python+opencv)

關于初始種子自動選取的區域生長實例(python+opencv)

關于初始種子自動選取的區域生長實例(python+opencv)

 

以上這篇關于初始種子自動選取的區域生長實例(python+opencv)就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持服務器之家。

原文鏈接:https://www.cnblogs.com/er-gou-zi/p/12016951.html

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 羞羞视频一区二区 | 精品久久久久99 | 午夜视频大全 | 欧美日韩高清在线观看 | 欧美一级黄色网 | 日日操夜| 九九黄色| 长泽雅美av | 污黄视频在线观看 | 成人毛片在线免费观看 | 成人福利电影在线观看 | 日韩男女在线 | 国产精品一区二区日韩 | 成人午夜在线免费观看 | 日韩黄色片免费看 | 黄色羞羞视频在线观看 | 羞羞视频免费网站 | 国产精品久久久久久久午夜片 | 曰本三级日本三级日本三级 | 露脸各种姿势啪啪的清纯美女 | 久久久成人免费视频 | 黄色影院网站 | 亚洲精品午夜国产va久久成人 | 国产毛片aaa一区二区三区视频 | 国产精品一区二区三区在线 | 国产午夜精品久久久久久久蜜臀 | 女教师~淫辱の动漫在线 | 国产精品嘿咻嘿咻在线播放 | 久久久久九九九女人毛片 | 亚洲影视在线观看 | 亚洲网在线| 午夜精品小视频 | 久久99精品久久久久久园产越南 | 成人免费av在线播放 | 日日操夜| 国产亚洲欧美日韩高清 | 免费毛片小视频 | 欧美日韩成人一区二区 | 亚洲一区二区在线视频 | 亚洲va国产va | 成人久久久精品国产乱码一区二区 |