Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/337.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 矩阵中元素的邻域_Python_Matrix - Fatal编程技术网

Python 矩阵中元素的邻域

Python 矩阵中元素的邻域,python,matrix,Python,Matrix,我试图检查矩阵中元素邻域的元素之间的差异是否大于某个公差值,如果大于,则在新矩阵中邻域中元素的相同索引处给出值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"

我试图检查矩阵中元素邻域的元素之间的差异是否大于某个公差值,如果大于,则在新矩阵中邻域中元素的相同索引处给出值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()

编辑:不知道我怎么会错过很多年前发布的这篇文章,哦,好吧,为了后面的读者,我将留下这个答案

根据评论,看起来您可能已经解决了一个问题,但在我看来,您的函数binary_edges在另一方面不符合您的预期目的

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
            #If I understand your intention correctly, then I think you're missing these two lines (notice that each element should have 8 neighbours)
            if (abs(pic_mat[i+1,j-1] - center ) > tolerance):
                binary_mat[i+1,j-1] = 1
            if (abs(pic_mat[i-1,j+11] - center ) > tolerance):
                binary_mat[i-1,j+11] = 1
    return binary_mat  
我是否可以建议一种替代方法,对于大小合理的pic_mat矩阵,该方法将大大加快速度,并且在我看来,该方法具有更少的混乱

def binary_edges_np(pic_mat, tolerance):
    dim = pic_mat.shape
    binary_mat = np.zeros((dim[0],dim[1]), dtype=bool)
    for i,j in [0,1],[1,0],[1,1],[1,-1]:
        s = pic_mat.shape
        slice1 = np.s_[max(0, 0+i):s[0]+i, max(0+j,0):s[1]+j]
        slice2 = np.s_[max(0-i,0):s[0]-i, max(0-j,0):s[1]-j]
        bool_slice = np.abs(pic_mat[slice1] - pic_mat[slice2]) > tolerance
        binary_mat[slice1] += bool_slice
        binary_mat[slice2] += bool_slice
    return binary_mat
详情如下:

matrix_pic = np.random.randint(0,255, (1000,1000))
s = time.time()
a = binary_edges(matrix_pic, 60)
print("binary_edges:", time.time() - s)
s = time.time()
b = binary_edges_np(matrix_pic, 60)
print("binary_edges_np:", time.time() - s)
二进制_边:5.694662809371948, 二进制_边_np:0.01562666893005371

此外,请注意,您的原始函数肯定具有边界效果,其中边缘2行/列内的元素将不会正确标记为超过阈值,我假设这不是有意的。新方法也为边提供了正确的值:

print((a == b).all()) # = False
print((a[2:-2,2:-2] == b[2:-2,2:-2]).all()) # True

我觉得不错:试着打印myarray和matrix_pic(查看数字的典型大小)。谢谢,我解决了复制到新矩阵的问题。现在在myarray的新矩阵中,我有1和0的元素,我正在尝试使用PIL模块将其转换为pic,你知道我是否做对了吗?