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 maskduplicated, 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 (afterbefore sorting):
$$b=[1,4,4,4,3]$$
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.