2017-09-12 6 views
1

私は、次のループを使って特定の真理値の距離内にあるすべてのピクセルを平均化して画像をノイズ除去しようとしています。現在のところ、このループには65秒かかり、おそらく何千回も実行する必要があります。禁止的な実行時間なしにPythonでこれを達成する方法はありますか?どんな助けでも大歓迎です。画像内のピクセルをテストする

for row in range(0, width): 
    for column in range(0, height): 
     if euclid_dist(point, pix[row,column]) <= threshold: 
      to_average[(row,column)] = pix[row, column] 

euclid_distは、次のように定義されます。

def euclid_dist(tuple1, tuple2): 

    tot_sq = 0 
    for num1, num2 in zip(tuple1, tuple2): 
    tot_sq += (num1 + num2)**2 
    return math.sqrt(tot_sq) 
+0

イメージを円形マスクで畳み込みます。 –

+0

もう少し情報を提供できますか?私はあなたが何を得ているのか分かっていると思うが、どうすればコード化するのか分からない。 – asheets

+0

NickTがあなたをカバーしているようだ。だから私はちょうどコメントします。 1)ジップは過度の可能性があります。インデックスベースのループを使ってみてください。2)計算する数値が多いときは、Numpyが普通の答えです。 –

答えて

1

あなたは(同じように、というよりもガウスで)円でちょうど平均すべてしたい場合は、ハードサークルカーネルを作ることができ、その後、以下のようにあなたのイメージを畳んでください。

# make a noisy circle thing 
img = np.random.randint(100, 200, (200, 200), dtype=np.uint8) 
xx, yy = np.meshgrid(np.arange(-100, 100), np.arange(-100, 100)) 
img = img + 10 * (xx**2 + yy**2 < 50**2) 
plt.imshow(img) 

enter image description here

# make a non-standard kernel 
radius = 10 
kernel_size = 2 * int(radius) + 1 # odd 
xy = np.arange(-kernel_size//2 + 1, kernel_size//2 + 1) 
xx, yy = np.meshgrid(xy, xy) 

kernel = np.zeros((kernel_size,) * 2) 
kernel[xx**2 + yy**2 <= radius**2] = 1 
plt.imshow(kernel) 

enter image description here

# convolve the two, depending on the mode, it will change the dimensions of the output 
plt.imshow(sig.convolve2d(img, kernel, mode='valid')) 

enter image description here

あなたは、ノイズを解除したい場合は、もう少し一般的であるガウスカーネルを使用することができます。より単純に「ガウスぼかし」と呼ばれています。

関連する問題