关于python:python实现基于八方向判断的断裂连接

42次阅读

共计 2973 个字符,预计需要花费 8 分钟才能阅读完成。

”’
八方向连贯算法
”’
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([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))
    # 构建判断矩阵,[电子钱包](https://www.gendan5.com/wallet.html) 用来判断某个像素是否进行卷积
    judge = np.zeros((h + 2, w + 2))
    for x in range(h):
        for y in range(w):
            one_side = False
            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):
                    one_side = True
            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()

正文完
 0