Thread: Python Help - Image Denoising

1. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Feb 2013
Posts
4
Rep Power
0

Python Help - Image Denoising

Can someone help me with this please?
2. This might be correct, I didn't carefully test. You can figure out why the numpy dot product might be considerably better than whatever you were trying to express.
Code:
def applyFilter(data, kernel):
ret = np.zeros_like(data)
KI = kernel.shape[0]//2
KJ = kernel.shape[1]//2
for i in range(KI, data.shape[0]-KI):
for j in range(KJ, data.shape[1]-KJ):
ret[i,j] = kernel.ravel().dot(data[i-KI:i+KI+1,j-KJ:j+KJ+1].flat)
return ret
3. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Feb 2013
Posts
4
Rep Power
0
Originally Posted by b49P23TIvg
This might be correct, I didn't carefully test. You can figure out why the numpy dot product might be considerably better than whatever you were trying to express.
Code:
def applyFilter(data, kernel):
ret = np.zeros_like(data)
KI = kernel.shape[0]//2
KJ = kernel.shape[1]//2
for i in range(KI, data.shape[0]-KI):
for j in range(KJ, data.shape[1]-KJ):
ret[i,j] = kernel.ravel().dot(data[i-KI:i+KI+1,j-KJ:j+KJ+1].flat)
return ret
Thank you for your help, but I'm taking an introductory programming class and don't quite understand what this is doing (.ravel, .flat). Would you please explain what this is doing in basic terms please?
4. It's your mathematics level that counts here.

I figured that you are familiar with numpy since you used
ret = np.zeros_like(data)

I observed that the convolution of the kernel with the picture amounts to a dot product of vectors. The numpy.ravel function returns the objects in order as a rank 1 vector. numpyArrayObject.flat is an iterator returning the values from the array in order. It's easier to write array operations in j, www.jsoftware.com
Code:
NB. Here's a sample 5 by 5 kernel.
NB. read "1r2" as one half.

KERNEL
0  1r300  1r150  1r100   1r75
1r60   1r50  7r300   2r75  3r100
1r30 11r300   1r25 13r300  7r150
1r20   4r75 17r300   3r50 19r300
1r15  7r100 11r150 23r300   2r25

NB. raveled kernel
, KERNEL
0 1r300 1r150 1r100 1r75 1r60 1r50 7r300 2r75 3r100 1r30 11r300 1r25 13r300 7r150 1r20 4r75 17r300 3r50 19r300 1r15 7r100 11r150 23r300 2r25

NB. sum of raveled kernel is 1
+/ , KERNEL
1
5. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Feb 2013
Posts
4
Rep Power
0
Originally Posted by b49P23TIvg
It's your mathematics level that counts here.

I figured that you are familiar with numpy since you used
ret = np.zeros_like(data)

I observed that the convolution of the kernel with the picture amounts to a dot product of vectors. The numpy.ravel function returns the objects in order as a rank 1 vector. numpyArrayObject.flat is an iterator returning the values from the array in order. It's easier to write array operations in j, www.jsoftware.com
Code:
NB. Here's a sample 5 by 5 kernel.
NB. read "1r2" as one half.

KERNEL
0  1r300  1r150  1r100   1r75
1r60   1r50  7r300   2r75  3r100
1r30 11r300   1r25 13r300  7r150
1r20   4r75 17r300   3r50 19r300
1r15  7r100 11r150 23r300   2r25

NB. raveled kernel
, KERNEL
0 1r300 1r150 1r100 1r75 1r60 1r50 7r300 2r75 3r100 1r30 11r300 1r25 13r300 7r150 1r20 4r75 17r300 3r50 19r300 1r15 7r100 11r150 23r300 2r25

NB. sum of raveled kernel is 1
+/ , KERNEL
1
Okay, thank you so much for your help!