ITPub博客

首页 > IT职业 > IT生活 > python实现基于八方向判断的断裂连接

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

IT生活 作者:专注的阿熊 时间:2021-04-06 17:17:14 0 删除 编辑

'''

  八方向连接算法

'''

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/,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2019-08-23

  • 博文量
    133
  • 访问量
    89991