In order to reduce noise in my training dataset,I attempted a WMF whose weights are shown in a 2-D array as follows(values finally get normalized by division by 15:

[1 , 2 , 1
 2 , 3 , 2
 1 , 2 , 1]

My input image is an RGB image size (128,128,3). The pixel values are integers in the range of 0-255.

Isolation of each of my three channels is done as:


Where mask_start=2

Convolution for a single channel is shown as:

for x in range(mask_cent,row_pad-mask_start):#The upper limit thing is not included. So Highest value is row_pad-mask_start-1
    for y in range(mask_cent,col_pad-mask_start):#These loops seem to be fine
      patch_selected_R = R_padded[x - mask_start-1: x + mask_start, y - mask_start-1: y + mask_start]
      a1_R= W*patch_selected_R

My images, which are shown below look like a colour correction rather than a denoising:

the original image

The resulting image

I am unsure of what is going wrong right now.

Edit: Something went wrong, this is my imgur link https://imgur.com/gallery/1d4V5vr

Edit 2: new code, its resulting output is available through this link

https://imgur.com/Dr9K1bU :

      for i in range(1,2):
      for i in range(1,3):
  • $\begingroup$ Want to validate and upvote answers? $\endgroup$ – Laurent Duval Dec 27 '20 at 14:49

Median filters are non-linear systems which do not posses impulse responses, hence the weights that you mention are not related with the median operation.

Those coefficients you presented define just a basic LTI lowpass filter. And it will do its best to remove some high frequency noise. That's it.

  • $\begingroup$ So what would be the best way for me to fix that?I ripped similar code off of Matlab. Then I made another change, shown in edits for n-plication. But my output image still looks like colour correction. Just less noisy $\endgroup$ – Mr. Johnny Doe Jul 30 '20 at 9:20
  • $\begingroup$ do you know what a median filter is ? $\endgroup$ – Fat32 Jul 30 '20 at 10:53
  • $\begingroup$ From what I understand. It essentially exists because impulse noise shows up as an outlier. So it replaces pixels with the median value within a Kernel space. So this way, they get replaced with pixels representative of the rest of the kernel to overcome noise $\endgroup$ – Mr. Johnny Doe Jul 30 '20 at 23:28
  • $\begingroup$ @Mr.JohnnyDoe Yes, a median filter essentially replaces its center pixel with the median of the block. Hence it's actually a sorter and nolinear; it does not have an impulse response nor a set of weights. So your weights cannot belong to a median filter. $\endgroup$ – Fat32 Jul 31 '20 at 0:20
  • $\begingroup$ I had changed the code to not have it be just a convolutional filter. But I'm trying to implement a weighted median filter that I found online. So just the same sorter along with weights $\endgroup$ – Mr. Johnny Doe Jul 31 '20 at 0:26

Apparently, you intend to weight the patch pixel per pixel in:

a1_R= W*patch_selected_R

and then apply a median on the piece-wise product, and divide by 15. By the way, beware of the * product and the input type of the array, you don't want a matrix product.

This is not the weighted median I know of. To me, the elements in patch_selected_R should be duplicated, triplicated, $n$-plicated with respect to the corresponding integer weight in the mask. On a smallest example: if $w=[1,3,1]$ and $m=[1 4 3]$, the intermediate (longer, of length $1+3+1=5$) buffer is (before sorting):


thus $[1,3,4,4,4]$ after sorting, and the weighted median is $4$ (instead of $3$ with the classical median).

The resulting array here is indeed 15 times larger, and you then compute the median, and replace the central value, with no need to further divide by 15. I am thinking of an interpretation of your result. Meanwhile, the theoretical origin of the weighted median is describe here: What is the advantage of weighted median filter over median filter?

Note: there are combinations of linear and nonlinear filters, like the mean-medians filters.

  • $\begingroup$ I think I realised my mistake. I got carried away and for a second thought I needed to do a convolution(I was adapted code written for grayscale, written in Matlab). I will try out np.multiply(W,Patch_selected_R) and see what happens. That should give me the effect of duplication and a proper answer $\endgroup$ – Mr. Johnny Doe Jul 29 '20 at 9:32
  • $\begingroup$ Not sure in Python, but multplying a value by 2 is not the same as duplicating the value $\endgroup$ – Laurent Duval Jul 29 '20 at 9:35
  • 1
    $\begingroup$ Ah, I just realised that issue. I will figure out the n-plication and get back to it in a bit. Thanks for your help $\endgroup$ – Mr. Johnny Doe Jul 29 '20 at 9:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.