python實現基於八方向判斷的斷裂連線

專注的阿熊發表於2021-04-06

'''

  八方向連線演算法

'''

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/,如需轉載,請註明出處,否則將追究法律責任。

相關文章