Re: [R] Asking Favor For the Script of Median Filter
Here is one I wrote for the raster package. It searches a raster layer for NA's and takes the median of the number of non NA adjacent cells determined by neighbor count. You could turn your matrix into a raster to make it work or change the code. Hope you find it useful, Robert neighbor.filter - function(raster.layer,neighbor.count = 3) { require(raster) base.rast - raster.layer count - 1 NA.ind - which(is.na(base.rast[])) median.vals - matrix(NA,length(NA.ind),3) for (j in 1:length(NA.ind)) { row.ind.NA - rowFromCell(base.rast, NA.ind[j]) col.ind.NA - colFromCell(base.rast, NA.ind[j]) row.ind - c(row.ind.NA-1,row.ind.NA,row.ind.NA+1) col.ind - c(col.ind.NA-1,col.ind.NA,col.ind.NA+1) row.ind.check - expand.grid(row.ind,col.ind)[,1] col.ind.check - expand.grid(row.ind,col.ind)[,2] ind.del.1 - c(which(row.ind.check dim(base.rast)[1]),which(row.ind.check 1)) if (length(ind.del.1) 0) { row.ind.check - row.ind.check[-ind.del.1] col.ind.check - col.ind.check[-ind.del.1] } ind.del.2 - c(which(col.ind.check 1),which(col.ind.check dim(base.rast)[2])) if (length(ind.del.2) 0) { row.ind.check - row.ind.check[-ind.del.2] col.ind.check - col.ind.check[-ind.del.2] } if (length(which(base.rast[cellFromRowCol(base.rast, row.ind.check, col.ind.check)] 0)) = neighbor.count) { median.vals[count,c(1:3)] - c(NA.ind[j], median(base.rast[cellFromRowCol(base.rast, row.ind.check, col.ind.check)], na.rm = T), length(which(base.rast[cellFromRowCol(base.rast, row.ind.check, col.ind.check)] 0))) count - count + 1 } } median.vals - median.vals[which(median.vals[,1] 0),] base.rast[median.vals[,1]] - median.vals[,2] return(base.rast) } Robert Leaf, PhD NOAA Narragansett Laboratory -- View this message in context: http://r.789695.n4.nabble.com/Asking-Favor-For-the-Script-of-Median-Filter-tp3409462p3517365.html Sent from the R help mailing list archive at Nabble.com. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
[R] Asking Favor For the Script of Median Filter
Hello,everybody. My name is Chuan Zun Liang. I come from Malaysia. I am just a beginner for R. Kindly to ask favor about median filter. The problem I facing as below: x-matrix(sample(1:30,25),5,5) x [,1] [,2] [,3] [,4] [,5] [1,]78 30 29 13 [2,]46 1259 [3,] 253 22 14 24 [4,]2 15 26 23 19 [5,] 28 18 10 11 20 This is example original matrices of an image. I want apply with median filter with window size 3X# to remove salt and pepper noise in my matric. Here are the script I attend to writing.The script and output shown as below: MedFilter-function(mat,sz) + {out-matrix(0,nrow(mat),ncol(mat)) + for(p in 1:(nrow(mat)-(sz-1))) + {for(q in 1:(ncol(mat)-(sz-1))) + {outrow-median(as.vector(mat[p:(p+(sz-1)),q:(q+(sz-1))])) + out[(p+p+(sz-1))/2,(q+q+(sz-1))/2]-outrow}} + out} MedFilter(x,3) [,1] [,2] [,3] [,4] [,5] [1,]00000 [2,]08 12 140 [3,]0 12 14 190 [4,]0 18 15 200 [5,]00000 Example to getting value 8 and 12 as below: 7 8 30 8 30 29 4 6 12 (median=8) 6 12 5 (median=12) 25 3 22 3 22 14 Even the script can give output. However, it is too slow. My image size is 364*364. It is time consumption. Is it get other ways to improving it? Best Wishes Chuan -- View this message in context: http://r.789695.n4.nabble.com/Asking-Favor-For-the-Script-of-Median-Filter-tp3409462p3409462.html Sent from the R help mailing list archive at Nabble.com. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Asking Favor For the Script of Median Filter
( sorry if this is a duplicate, I am not sure if hotmail is dropping some of my posts. Thanks ) You obviously want to delegate inner loops to R packages that execute as native, hopefully optimized, code. Generally a google search that starts with R CRAN will help. In this case it looks like a few packages available, http://www.google.com/search?sclient=psyhl=enq=R+cran+median+filter Date: Sun, 27 Mar 2011 07:56:11 -0700 From: chuan...@hotmail.com To: r-help@r-project.org Subject: [R] Asking Favor For the Script of Median Filter Hello,everybody. My name is Chuan Zun Liang. I come from Malaysia. I am just a beginner for R. Kindly to ask favor about median filter. The problem I facing as below: x-matrix(sample(1:30,25),5,5) x [,1] [,2] [,3] [,4] [,5] [1,] 7 8 30 29 13 [2,] 4 6 12 5 9 [3,] 25 3 22 14 24 [4,] 2 15 26 23 19 [5,] 28 18 10 11 20 This is example original matrices of an image. I want apply with median filter with window size 3X# to remove salt and pepper noise in my matric. Here are the script I attend to writing.The script and output shown as below: MedFilter-function(mat,sz) + {out-matrix(0,nrow(mat),ncol(mat)) + for(p in 1:(nrow(mat)-(sz-1))) + {for(q in 1:(ncol(mat)-(sz-1))) + {outrow-median(as.vector(mat[p:(p+(sz-1)),q:(q+(sz-1))])) + out[(p+p+(sz-1))/2,(q+q+(sz-1))/2]-outrow}} + out} MedFilter(x,3) [,1] [,2] [,3] [,4] [,5] [1,] 0 0 0 0 0 [2,] 0 8 12 14 0 [3,] 0 12 14 19 0 [4,] 0 18 15 20 0 [5,] 0 0 0 0 0 Example to getting value 8 and 12 as below: 7 8 30 8 30 29 4 6 12 (median=8) 6 12 5 (median=12) 25 3 22 3 22 14 Even the script can give output. However, it is too slow. My image size is 364*364. It is time consumption. Is it get other ways to improving it? Best Wishes Chuan __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Asking Favor For the Script of Median Filter
On Mar 27, 2011, at 10:56 AM, chuan_zl wrote: Hello,everybody. My name is Chuan Zun Liang. I come from Malaysia. I am just a beginner for R. Kindly to ask favor about median filter. The problem I facing as below: x-matrix(sample(1:30,25),5,5) x [,1] [,2] [,3] [,4] [,5] [1,]78 30 29 13 [2,]46 1259 [3,] 253 22 14 24 [4,]2 15 26 23 19 [5,] 28 18 10 11 20 This is example original matrices of an image. I want apply with median filter with window size 3X# to remove salt and pepper noise in my matric. Here are the script I attend to writing.The script and output shown as below: MedFilter-function(mat,sz) + {out-matrix(0,nrow(mat),ncol(mat)) + for(p in 1:(nrow(mat)-(sz-1))) + {for(q in 1:(ncol(mat)-(sz-1))) + {outrow-median(as.vector(mat[p:(p+(sz-1)),q:(q+(sz-1))])) + out[(p+p+(sz-1))/2,(q+q+(sz-1))/2]-outrow}} + out} Noting that median is probably the rate-limiting factor, I looked for another way to get the middle of 9 items. Using order seem faster: system.time( replicate(10, x2s - sort(x2))) user system elapsed 9.829 0.212 10.029 system.time( replicate(10, x2m - median(x2))) user system elapsed 7.169 0.126 7.272 system.time( replicate(10, x2s -x2[order(x2)[5] ])) user system elapsed 1.907 0.051 1.960 So see if this is any faster. On my system it's about three times faster: x - matrix(sample(364*364), 364,364) out - matrix(0, 364,364) for(xi in 1:(nrow(x)-2)) { for(yi in 1:(ncol(x)-2) ) { xm - x[xi+0:2, yi+0:2] d[xi+1, yi+1] -xm[order(xm)[5] ]}} #-tests -- system.time(for(xi in 1:(nrow(x)-2)) { + for(yi in 1:(ncol(x)-2) ) { + xm - x[xi+0:2, yi+0:2] + d[xi+1, yi+1] -xm[order(xm)[5] ]}} ) user system elapsed 3.806 0.083 3.887 system.time(MedFilter(x,3) ) user system elapsed 11.242 0.202 11.427 MedFilter(x,3) [,1] [,2] [,3] [,4] [,5] [1,]00000 [2,]08 12 140 [3,]0 12 14 190 [4,]0 18 15 200 [5,]00000 Example to getting value 8 and 12 as below: 7 8 30 8 30 29 4 6 12 (median=8) 6 12 5 (median=12) 25 3 22 3 22 14 Even the script can give output. However, it is too slow. My image size is 364*364. It is time consumption. Is it get other ways to improving it? David Winsemius, MD West Hartford, CT __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Asking Favor For the Script of Median Filter
On Mar 27, 2011, at 1:07 PM, Mike Marchywka wrote: You obviously want to delegate inner loops to R packages that execute as native, hopefully optimized, code. Generally a google search that starts with R CRAN will help. In this case it looks like a few packages available, http://www.google.com/search?sclient=psyhl=enq=R+cran+median+filter Did you find any that include a 2D median filter? All the ones I looked at were for univariate data. -- David. Date: Sun, 27 Mar 2011 07:56:11 -0700 From: chuan...@hotmail.com To: r-help@r-project.org Subject: [R] Asking Favor For the Script of Median Filter Hello,everybody. My name is Chuan Zun Liang. I come from Malaysia. I am just a beginner for R. Kindly to ask favor about median filter. The problem I facing as below: x-matrix(sample(1:30,25),5,5) x [,1] [,2] [,3] [,4] [,5] [1,] 7 8 30 29 13 [2,] 4 6 12 5 9 [3,] 25 3 22 14 24 [4,] 2 15 26 23 19 [5,] 28 18 10 11 20 This is example original matrices of an image. I want apply with median filter with window size 3X# to remove salt and pepper noise in my matric. Here are the script I attend to writing.The script and output shown as below: MedFilter-function(mat,sz) + {out-matrix(0,nrow(mat),ncol(mat)) + for(p in 1:(nrow(mat)-(sz-1))) + {for(q in 1:(ncol(mat)-(sz-1))) + {outrow-median(as.vector(mat[p:(p+(sz-1)),q:(q+(sz-1))])) + out[(p+p+(sz-1))/2,(q+q+(sz-1))/2]-outrow}} + out} MedFilter(x,3) [,1] [,2] [,3] [,4] [,5] [1,] 0 0 0 0 0 [2,] 0 8 12 14 0 [3,] 0 12 14 19 0 [4,] 0 18 15 20 0 [5,] 0 0 0 0 0 Example to getting value 8 and 12 as below: 7 8 30 8 30 29 4 6 12 (median=8) 6 12 5 (median=12) 25 3 22 3 22 14 Even the script can give output. However, it is too slow. My image size is 364*364. It is time consumption. Is it get other ways to improving it? Best Wishes Chuan -- David Winsemius, MD West Hartford, CT __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Asking Favor For the Script of Median Filter
?runmed -- Bert On Sun, Mar 27, 2011 at 2:44 PM, David Winsemius dwinsem...@comcast.net wrote: On Mar 27, 2011, at 10:56 AM, chuan_zl wrote: Hello,everybody. My name is Chuan Zun Liang. I come from Malaysia. I am just a beginner for R. Kindly to ask favor about median filter. The problem I facing as below: x-matrix(sample(1:30,25),5,5) x [,1] [,2] [,3] [,4] [,5] [1,] 7 8 30 29 13 [2,] 4 6 12 5 9 [3,] 25 3 22 14 24 [4,] 2 15 26 23 19 [5,] 28 18 10 11 20 This is example original matrices of an image. I want apply with median filter with window size 3X# to remove salt and pepper noise in my matric. Here are the script I attend to writing.The script and output shown as below: MedFilter-function(mat,sz) + {out-matrix(0,nrow(mat),ncol(mat)) + for(p in 1:(nrow(mat)-(sz-1))) + {for(q in 1:(ncol(mat)-(sz-1))) + {outrow-median(as.vector(mat[p:(p+(sz-1)),q:(q+(sz-1))])) + out[(p+p+(sz-1))/2,(q+q+(sz-1))/2]-outrow}} + out} Noting that median is probably the rate-limiting factor, I looked for another way to get the middle of 9 items. Using order seem faster: system.time( replicate(10, x2s - sort(x2))) user system elapsed 9.829 0.212 10.029 system.time( replicate(10, x2m - median(x2))) user system elapsed 7.169 0.126 7.272 system.time( replicate(10, x2s -x2[order(x2)[5] ])) user system elapsed 1.907 0.051 1.960 So see if this is any faster. On my system it's about three times faster: x - matrix(sample(364*364), 364,364) out - matrix(0, 364,364) for(xi in 1:(nrow(x)-2)) { for(yi in 1:(ncol(x)-2) ) { xm - x[xi+0:2, yi+0:2] d[xi+1, yi+1] -xm[order(xm)[5] ]}} #-tests -- system.time(for(xi in 1:(nrow(x)-2)) { + for(yi in 1:(ncol(x)-2) ) { + xm - x[xi+0:2, yi+0:2] + d[xi+1, yi+1] -xm[order(xm)[5] ]}} ) user system elapsed 3.806 0.083 3.887 system.time(MedFilter(x,3) ) user system elapsed 11.242 0.202 11.427 MedFilter(x,3) [,1] [,2] [,3] [,4] [,5] [1,] 0 0 0 0 0 [2,] 0 8 12 14 0 [3,] 0 12 14 19 0 [4,] 0 18 15 20 0 [5,] 0 0 0 0 0 Example to getting value 8 and 12 as below: 7 8 30 8 30 29 4 6 12 (median=8) 6 12 5 (median=12) 25 3 22 3 22 14 Even the script can give output. However, it is too slow. My image size is 364*364. It is time consumption. Is it get other ways to improving it? David Winsemius, MD West Hartford, CT __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. -- Bert Gunter Genentech Nonclinical Biostatistics __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Asking Favor For the Script of Median Filter
Oops. My error! You wanted a 2D filter. I suspect a search will find some implementation, but you may wish to consider ?loess instead (there are numerous others, no doubt). -- Bert On Sun, Mar 27, 2011 at 3:30 PM, David Winsemius dwinsem...@comcast.net wrote: On Mar 27, 2011, at 1:07 PM, Mike Marchywka wrote: You obviously want to delegate inner loops to R packages that execute as native, hopefully optimized, code. Generally a google search that starts with R CRAN will help. In this case it looks like a few packages available, http://www.google.com/search?sclient=psyhl=enq=R+cran+median+filter Did you find any that include a 2D median filter? All the ones I looked at were for univariate data. -- David. Date: Sun, 27 Mar 2011 07:56:11 -0700 From: chuan...@hotmail.com To: r-help@r-project.org Subject: [R] Asking Favor For the Script of Median Filter Hello,everybody. My name is Chuan Zun Liang. I come from Malaysia. I am just a beginner for R. Kindly to ask favor about median filter. The problem I facing as below: x-matrix(sample(1:30,25),5,5) x [,1] [,2] [,3] [,4] [,5] [1,] 7 8 30 29 13 [2,] 4 6 12 5 9 [3,] 25 3 22 14 24 [4,] 2 15 26 23 19 [5,] 28 18 10 11 20 This is example original matrices of an image. I want apply with median filter with window size 3X# to remove salt and pepper noise in my matric. Here are the script I attend to writing.The script and output shown as below: MedFilter-function(mat,sz) + {out-matrix(0,nrow(mat),ncol(mat)) + for(p in 1:(nrow(mat)-(sz-1))) + {for(q in 1:(ncol(mat)-(sz-1))) + {outrow-median(as.vector(mat[p:(p+(sz-1)),q:(q+(sz-1))])) + out[(p+p+(sz-1))/2,(q+q+(sz-1))/2]-outrow}} + out} MedFilter(x,3) [,1] [,2] [,3] [,4] [,5] [1,] 0 0 0 0 0 [2,] 0 8 12 14 0 [3,] 0 12 14 19 0 [4,] 0 18 15 20 0 [5,] 0 0 0 0 0 Example to getting value 8 and 12 as below: 7 8 30 8 30 29 4 6 12 (median=8) 6 12 5 (median=12) 25 3 22 3 22 14 Even the script can give output. However, it is too slow. My image size is 364*364. It is time consumption. Is it get other ways to improving it? Best Wishes Chuan -- David Winsemius, MD West Hartford, CT __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. -- Bert Gunter Genentech Nonclinical Biostatistics __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Asking Favor For the Script of Median Filter
CC: chuan...@hotmail.com; r-help@r-project.org From: dwinsem...@comcast.net To: marchy...@hotmail.com Subject: Re: [R] Asking Favor For the Script of Median Filter Date: Sun, 27 Mar 2011 18:30:48 -0400 On Mar 27, 2011, at 1:07 PM, Mike Marchywka wrote: You obviously want to delegate inner loops to R packages that execute as native, hopefully optimized, code. Generally a google search that starts with R CRAN will help. In this case it looks like a few packages available, http://www.google.com/search?sclient=psyhl=enq=R+cran+median+filter Did you find any that include a 2D median filter? All the ones I looked at were for univariate data. I put almost zero thought or effort into that but an interested party could modify the words a bit and and, for example image and one of the first interesting hits is this, http://cran.r-project.org/web/packages/biOps/biOps.pdf x - readJpeg(system.file(samples, violet.jpg, package=biOps)) y - imgBlockMedianFilter(x, 5) -- David. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.