2017-03-21 12 views
0

pythonで画像処理を少し行い、50x50正方形のnumpy配列の値を変更しようとしています。各正方形は200ピクセルごとに変化します。これまでのところ、これは私が持っているものです:50x50正方形の2d numpy配列のピクセル値を変更する

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でやや錆びていますが、その解決策はおそらく些細なことですが、私が得ることができる助けに感謝します。

答えて

1

あなたはそのようにそれを行うことができます。

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 

ラインをimage_data=np.zeros([1000,1000])ちょうどあなたがあなたの代わりに行列を使用することができ、上記の例のための行列を初期化します。 index_listは、すべての関連する行と列のインデックスのリストです。

image_data[np.ix_(index_list,index_list)]=999は、マトリックスの関連部分に999を割り当てます。 places=np.arange(-25,26)を設定することに注意してください.50から50までの範囲(つまり、エンドポイントなどを含む175行から225行まで)を必要としていたようです。そうでない場合は、26から25に変更するか、好きなように設定することができます。

また、このコードは正方行列image_dataです。それは長方形である場合は、そのように、個別に行と列のインデックスを定義する必要があります。

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 
+0

お返事ありがとうございます。必要に応じてどのようにアプローチすれば、200ピクセルごとに50ピクセルが変化したと言えますか?行と列の両方でおよそ175-225のように999の明るさである – cvirus96

+0

image_dataが長方形の場合もカバーするようにコードを更新しました。 –

+0

ガウス関数に基づいて各四角形カーネルの明るさを調整したい場合は、これをすばやく修正できますか?私が999の代わりにガウスを付けると、それは各正方形の中心ではなく、画像の中央に1つずつ作成されます。 – cvirus96

0

私は関係なく、画像の形状の、今arr.flatten()np.reshape()

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)) 

を使用して単純なアプローチを提案します(四角形、四角形)では、このアプローチが有効です。

関連する問題