python實現基於八方向判斷的斷裂連線
'''
八方向連線演算法
'''
import numpy as np
import matplotlib.image as mpimg
from PIL import Image
def compute_conv(fm, kernel, judge=None, kernel_num=3):
[h, w] = fm.shape
k = kernel_num
r = int(k / 2)
padding_fm = np.zeros([h + 2, w + 2])
rs = np.zeros([h, w])
padding_fm[1:h + 1, 1:w + 1] = fm
for i in range(1, h + 1):
for j in range(1, w + 1):
if padding_fm[i, j] != 0:
rs[i - 1][j - 1] = 128
continue
if judge is not None:
if judge[i, j] == 0:
continue
# (0,0),(3,3)
i0 = i - r
i1 = i + r + 1
j0 = j - r
j1 = j + r + 1
roi = padding_fm[i0:i1, j0:j1]
rs[i - 1][j - 1] = np.sum(roi * kernel)
return rs
def compute_conv_plus(fm, kernel):
[h, w] = fm.shape # 512 , 512
k = 5
r = int(k / 2) # 2
padding_fm = np.zeros([h + 4, w + 4]) # 516 , 516
print(padding_fm.shape)
rs_plus = np.zeros外匯跟單gendan5.com([h, w]) # 512 , 512
padding_fm[2:h + 2, 2:w + 2] = fm # 儲存原畫素值
for i in range(2, h + 2):
for j in range(2, w + 2):
# (0,0),(4,4)
i0 = i - r
i1 = i + r + 1
j0 = j - r
j1 = j + r + 1
roi = padding_fm[i0:i1, j0:j1]
print("roi.shape({})".format(roi.shape))
print("kernel.shape({})".format(kernel.shape))
rs_plus[i - 2][j - 2] = np.sum(roi * kernel)
# 為什麼最後一個輸出的 roi 大小是( 5 , 4 )
return rs_plus
def kernel_i():
weights_data = [
[1, 1, 1],
[1, 0, 1],
[1, 1, 1]
]
weights = np.asarray(weights_data)
return weights
def kernel_j():
weights_data = [
[1, 1, 1, 1, 1],
[1, 1, 1, 1, 1],
[1, 1, 0, 1, 1],
[1, 1, 1, 1, 1],
[1, 1, 1, 1, 1]
]
weights = np.asarray(weights_data)
return weights
# 上方向
def kernel_up():
weights_data = [
[1, 1, 1],
[0, 0, 0],
[0, 0, 0]
]
weights = np.asarray(weights_data)
return weights
# 下方向
def kernel_down():
weights_data = [
[0, 0, 0],
[0, 0, 0],
[1, 1, 1]
]
weights = np.asarray(weights_data)
return weights
def kernel_left():
weights_data = [
[1, 0, 0],
[1, 0, 0],
[1, 0, 0]
]
weights = np.asarray(weights_data)
return weights
def kernel_right():
weights_data = [
[0, 0, 1],
[0, 0, 1],
[0, 0, 1]
]
weights = np.asarray(weights_data)
return weights
def kernel_left_up():
weights_data = [
[1, 1, 0],
[1, 0, 0],
[0, 0, 0]
]
weights = np.asarray(weights_data)
return weights
def kernel_right_down():
weights_data = [
[0, 0, 0],
[0, 0, 1],
[0, 1, 1]
]
weights = np.asarray(weights_data)
return weights
def kernel_right_up():
weights_data = [
[0, 1, 1],
[0, 0, 1],
[0, 0, 0]
]
weights = np.asarray(weights_data)
return weights
def kernel_left_down():
weights_data = [
[0, 0, 0],
[1, 0, 0],
[1, 1, 0]
]
weights = np.asarray(weights_data)
return weights
def main():
for i in range(c):
l1 = temp[:, :, i]
kernel_1 = [kernel_up(), kernel_left(), kernel_left_up(), kernel_right_up()]
kernel_2 = [kernel_down(), kernel_right(), kernel_right_down(), kernel_left_down()]
input = np.asarray(l1)
# 八方向判斷
kernel_1_res = []
kernel_2_res = []
for weight1, weight2 in zip(kernel_1, kernel_2):
kernel_1_res.append(compute_conv(input, weight1))
kernel_2_res.append(compute_conv(input, weight2))
# 構建判斷矩陣,用來判斷某個畫素是否進行卷積
judge = np.zeros((h + 2, w + 2))
for x in range(h):
for y in range(w):
>
for w1_res, w2_res in zip(kernel_1_res, kernel_2_res):
if (w1_res[x, y] > 0 and w2_res[x, y] <= 0) or (w1_res[x, y] == 0 and w2_res[x, y] != 0):
>
if not one_side:
judge[x + 1, y + 1] = 1
result = compute_conv(input, kernel_i(), judge=judge)
for x in range(h):
for y in range(w):
if result[x, y] != 0:
result[x, y] = 128
else:
result[x, y] = 0
arr[:, :, i] = result
arr01 = np.array(arr, dtype=np.uint8)
image = Image.fromarray(arr01, 'RGB')
image.save(r'')
img = mpimg.imread(r'')
temp = np.asarray(img)
[h, w, c] = temp.shape
arr = np.zeros((h, w, c), int)
main()
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/69946337/viewspace-2766727/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- 判斷網路是否連線
- 手指滑動方向判斷
- Python 基礎 - if else流程判斷Python
- Python 實現斷網自動重連Python
- python if判斷的使用格式Python
- SQL Server如何判斷哪些會話/連線是長連線?SQLServer會話
- Python基礎:條件判斷 & 迴圈Python
- Python判斷閏年Python
- Spring Security 基於URL的許可權判斷Spring
- 函式實現閏年判斷函式
- 判斷是否能連線網際網路
- js基礎-12-判斷陣列和判斷物件的方法JS陣列物件
- Envoy熔斷限流實踐(一)基於Rainbond外掛實現熔斷AI
- Nginx實現IF語句裡的AND,OR多重判斷Nginx
- 使用Python實現一個棧, 判斷括號是否平衡Python
- 判斷迴文連結串列
- JS的判斷語句:判斷、迴圈JS
- python 判斷是否為中文Python
- python中字串格式判斷Python字串
- python之判斷語句Python
- Python中None如何判斷PythonNone
- python如何判斷字串相等Python字串
- python判斷是否為listPython
- python如何判斷迴文Python
- 判斷點是否在多邊形內的Python實現及小應用(射線法)斷點Python
- 判斷單連結串列是否關於中心對陣
- ssm框架實現介面基礎上再加上token判斷SSM框架
- 使用帶型別判斷的比較判斷型別
- js函式中的if判斷和a==b判斷JS函式
- 基於機率判斷矩陣A*B是否等於C矩陣
- H5觸控事件判斷滑動方向H5事件
- 寫一個方法判斷頁面滾動方向
- WebSocket斷線重連Web
- Python中型別最佳判斷方法Python型別
- 如何使用Python判斷奇偶數?Python
- Python入門 - 判斷語句Python
- 11.9 python之判斷語句Python
- python怎麼判斷星期幾Python