March 31st, 2013, 11:53 PM

Python Help  Image Denoising
Can someone help me with this please?
April 1st, 2013, 10:35 AM

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[iKI:i+KI+1,jKJ:j+KJ+1].flat)
return ret
[code]
Code tags[/code] are essential for python code and Makefiles!
April 1st, 2013, 12:10 PM

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[iKI:i+KI+1,jKJ: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?
April 1st, 2013, 12:35 PM

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've seen the zeros_like function used about twice in a decade.
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
[code]
Code tags[/code] are essential for python code and Makefiles!
April 1st, 2013, 12:46 PM

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've seen the zeros_like function used about twice in a decade.
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!