2

I have tried to implement the following algorithm but the resulting image looks the same.

Step 1: Read Noisy Image.

Step 2: Select 2D window of size 3x3 with centre element as processing pixel. Assume that the pixel being processed is P ij .

Step 3: If P ij is an uncorrupted pixel (that is, 0< P ij <255), then its value is left unchanged.

Step 4: If P ij = 0 or P ij = 255, then P ij is a corrupted pixel.

Step 5: If 3/4 th or more pixels in selected window are noisy then increase window size to 5x5. Step 6: If all the elements in the selected window are 0‟s and 255‟s, then replace P ij with the mean of the elements in the window else go to step 7.

Step 7: Eliminate 0‟s and 255‟s from the selected window and find the median value of the remaining elements. Replace Pij with the median value.

Step 8: Repeat steps 2 to 6 until all the pixels in the entire image are processed.

Here is my code. Please suggest improvements.

import Image

im=Image.open("no.jpg")
im = im.convert('L')

for i in range(2,im.size[0]-2):
    for j in range(2,im.size[1]-2):
        b=[]
        if im.getpixel((i,j))>0 and im.getpixel((i,j))<255:
            pass
        elif im.getpixel((i,j))==0 or im.getpixel((i,j))==255:
            c=0
            for p in range(i-1,i+2):
                for q in range(j-1,j+2):
                    if im.getpixel((p,q))==0 or im.getpixel((p,q))==255: 
                        c=c+1
            if c>6:
                c=0
                for p in range(i-2,i+3):
                    for q in range(j-2,j+3):
                        b.append(im.getpixel((p,q)))
                        if im.getpixel((p,q))==0 or im.getpixel((p,q))==255:
                            c=c+1
                if c==25:
                    a=sum(b)/25
                    print a
                    im.putpixel((i,j),a)
                else:
                    p=[]
                    for t in b:
                        if t not in (0,255):
                            p.append(t)
                    p.sort()
                    im.putpixel((i,j),p[len(p)/2])
            else:
                b1=[]
                for p in range(i-1,i+2):
                    for q in range(j-1,j+2):
                        b1.append(im.getpixel((p,q)))
                im.putpixel((i,j),sum(b1)/9)

im.save("nonoise.jpg")   
4

3 に答える 3

6

median filterを使用する必要があります。これは実装が簡単で、ごま塩ノイズに対して非常にうまく機能します。

于 2014-03-08T17:36:47.753 に答える
0

入力画像はどのように見えますか? アルゴリズムは、ピクセル値 0 と 255 のみがノイズであると想定しています。ノイズの多いピクセルが実際にそれ以外の値を持っている場合、アルゴリズムは何も実行せず、出力が入力と同じに見えることがあります。

于 2014-03-08T14:44:45.500 に答える