0

行列内の要素の近傍の要素間の差が許容値より大きいかどうかを確認しようとしています。そうであれば、新しい行列の近傍の要素の同じインデックスで 1 の値を与えます. どういうわけか、新しいマトリックスでは常にすべてが 1 になりますが、これは間違っています。これは私のコードです。また、画像を行列に変換して行列を取得します。

from PIL import Image
import numpy as np
from numpy.lib.stride_tricks import as_strided


imo = Image.open("/home/gauss/Pictures/images.jpg")


matrix_pic = np.array(imo.convert('L')).astype(float)
dim = matrix_pic.shape


# start 1 step out of the outer borders of the matrix


def binary_edges(pic_mat , tolerance):  
    dim = pic_mat.shape
    binary_mat = np.zeros((dim[0],dim[1]))
    for i in range(1  , dim[0]-1):
        for j in range(1  ,dim[1]-1):
            center = pic_mat[i,j]
            if (abs(pic_mat[i+1,j] - center ) > tolerance):
                binary_mat[i+1,j] = 1
            if (abs(pic_mat[i,j+1] - center ) > tolerance):
                binary_mat[i,j+1] = 1
            if (abs(pic_mat[i+1,j+1] - center ) > tolerance):
                binary_mat[i+1,j+1] = 1
            if (abs(pic_mat[i-1,j] - center ) > tolerance):
                binary_mat[i-1,j] = 1
            if (abs(pic_mat[i,j-1] - center ) > tolerance):
                binary_mat[i,j-1] = 1
            if (abs(pic_mat[i-1,j-1] - center ) > tolerance):
                binary_mat[i-1,j-1] = 1
    return binary_mat       

myarray = binary_edges(matrix_pic, 60)
im = Image.fromarray(myarray)
im.show()
4

1 に答える 1