Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/301.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/video/2.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 更改50x50正方形2d numpy阵列中的像素值_Python_Arrays_Numpy_Image Processing - Fatal编程技术网

Python 更改50x50正方形2d numpy阵列中的像素值

Python 更改50x50正方形2d numpy阵列中的像素值,python,arrays,numpy,image-processing,Python,Arrays,Numpy,Image Processing,在python中进行一些图像处理,我试图改变一个50x50平方的numpy数组的值,每个平方每改变200像素。到目前为止,这就是我所拥有的: image_data[0::200,0::200] = 999 这将每200个空间种植一个非常明亮的像素。一、 但是,无法确定如何使其周围的像素发生变化。我试过类似的方法: image_data[0::200+1,0::200] = 999 image_data[0::200-1,0::200] = 999 image_data[0::200+2,0:

在python中进行一些图像处理,我试图改变一个50x50平方的numpy数组的值,每个平方每改变200像素。到目前为止,这就是我所拥有的:

 image_data[0::200,0::200] = 999
这将每200个空间种植一个非常明亮的像素。一、 但是,无法确定如何使其周围的像素发生变化。我试过类似的方法:

image_data[0::200+1,0::200] = 999
image_data[0::200-1,0::200] = 999
image_data[0::200+2,0::200] = 999
image_data[0::200-2,0::200] = 999

但这会在迭代过程中分散像素。我对python有些生疏,解决方案可能很琐碎,但我非常感谢能得到的任何帮助。

您可以这样做:

import numpy as np
image_data=np.zeros([1017,1017])
places=np.arange(-25,26)
centers=np.array([[200*i]*len(places) for i in range(1,len(image_data)//200+1)])
index_list=np.concatenate(centers+places)
index_list=index_list[index_list<len(image_data)]
image_data[np.ix_(index_list,index_list)]=999
import numpy as np
image_data=np.zeros([1017,2017])
places=np.arange(-25,26)
centers_rows=np.array([[200*i]*len(places) for i in range(1,len(image_data)//200+1)])
centers_columns=np.array([[200*i]*len(places) for i in range(1,len(image_data[0])//200+1)])
row_index=np.concatenate(centers_rows+places)
col_index=np.concatenate(centers_columns+places)
row_index=row_index[row_index<len(image_data)]
col_index=col_index[col_index<len(image_data[0])]
image_data[np.ix_(row_index,col_index)]=999

我建议使用arr.flatten和np.reforme进行简单的方法


现在,不管图像的形状是正方形还是长方形,这种方法都是可行的。

感谢您的回复。如果需要,您将如何实现这一点?假设每改变200个像素,就有50个像素环绕。像大约175-225行和列都是999亮度一样,我更新了代码以涵盖图像数据也是矩形的情况。如果我想根据高斯函数调整每个方形核的亮度,有没有快速解决方法?如果我在999的位置附加一个高斯分布,它会在图像的中心创建一个高斯分布,而不是在每个正方形的中心。如果你试图替换的不仅仅是一个数字,你需要确保尺寸合适。例如,这将起作用:image\u data[np.ix\u row\u index,col\u index]=np.random.randint5,size=lenrow\u index,lencol\u index。所以你可以试着事先准备好你要替换的矩阵,然后替换它。
In [140]: a = np.zeros((1000, 1000))

# C style flattening for row level operation
In [141]: af = a.flatten()
In [142]: af.shape
Out[142]: (1000000,)

# get every 200th element
In [145]: idxs = [ idx for idx, el in enumerate(af) if idx % 200 == 0]

# generate required indices
In [146]: indices = [ range(el-25, el+25) for el in idxs[1:]]

# update new values to the above indices
In [147]: af[indices] = 999

# now, again reshape to original shape
In [148]: a_modified = af.reshape((1000, 1000))


# Fortran style flattening for column level operation
In [149]: amf = a_modified.flatten('F')

# get every 200th element
In [150]: idxs = [idx for idx, el in enumerate(amf) if idx % 200 == 0]

# generate required indices
In [151]: indices = [ range(el-25, el+25) for el in idxs[1:]]

# update new values to the above indices
In [152]: amf[indices] = 999

# now, again reshape to original shape
In [153]: a_final = amf.reshape((1000, 1000))