Re: [R] Asking Favor For Remove element with Particular Value In Vector
On 08/30/2011 12:06 AM, Bert Gunter wrote: Jim et. al: This is the second time I've seen this advice recently. Use logical indexing: which(), though not wrong, is superfluous: x[ !x %in% c(0,255)] will do, rather than: By golly, you're right, and it works even if x is a logical vector. I should have checked the operator precedence. Jim __ 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 Remove element with Particular Value In Vector
But that is a good reason to always use parentheses: x[ !(x %in% c(0,255))] since some of the 'precendences' vary between languages. On Tue, Aug 30, 2011 at 4:47 AM, Jim Lemon j...@bitwrit.com.au wrote: On 08/30/2011 12:06 AM, Bert Gunter wrote: Jim et. al: This is the second time I've seen this advice recently. Use logical indexing: which(), though not wrong, is superfluous: x[ !x %in% c(0,255)] will do, rather than: By golly, you're right, and it works even if x is a logical vector. I should have checked the operator precedence. Jim __ 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. -- Jim Holtman Data Munger Guru What is the problem that you are trying to solve? __ 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 Remove element with Particular Value In Vector
chuan_zl wrote: Dear All. I am Chuan. I am beginner for R.I facing some problem in remove element from vector.I have a vector with size 238 element as follow(a part) [1] 0 18 24 33 44..[238] 255 Let the vector label as x,I want remove element 0 and 255.I try use such function: x[x0 x255] Hi Chuan, If you want to remove the specific values 0 and 255 from your vector, try: x-x[-which(x %in% c(0,255))] Jim __ 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 Remove element with Particular Value In Vector
Jim et. al: This is the second time I've seen this advice recently. Use logical indexing: which(), though not wrong, is superfluous: x[ !x %in% c(0,255)] will do, rather than: If you want to remove the specific values 0 and 255 from your vector, try: x-x[-which(x %in% c(0,255))] Jim -- Bert __ 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. -- Men by nature long to get on to the ultimate truths, and will often be impatient with elementary studies or fight shy of them. If it were possible to reach the ultimate truths without the elementary studies usually prefixed to them, these would not be preparatory studies but superfluous diversions. -- Maimonides (1135-1204) 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 Remove element with Particular Value In Vector
Thank you friend for suggestion. -- View this message in context: http://r.789695.n4.nabble.com/Asking-Favor-For-Remove-element-with-Particular-Value-In-Vector-tp3772779p3776432.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 Remove element with Particular Value In Vector
Thank you very much,friend. -- View this message in context: http://r.789695.n4.nabble.com/Asking-Favor-For-Remove-element-with-Particular-Value-In-Vector-tp3772779p3776427.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 Remove element with Particular Value In Vector
Thank you very much,friend. -- View this message in context: http://r.789695.n4.nabble.com/Asking-Favor-For-Remove-element-with-Particular-Value-In-Vector-tp3772779p3776430.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 Remove element with Particular Value In Vector
Thank you very much,friend. -- View this message in context: http://r.789695.n4.nabble.com/Asking-Favor-For-Remove-element-with-Particular-Value-In-Vector-tp3772779p3776435.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 Remove element with Particular Value In Vector
-Original Message- From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org] On Behalf Of Bert Gunter Sent: Monday, August 29, 2011 7:07 AM To: Jim Lemon Cc: r-help@r-project.org Subject: Re: [R] Asking Favor For Remove element with Particular Value In Vector Jim et. al: This is the second time I've seen this advice recently. Use logical indexing: which(), though not wrong, is superfluous: which() will give the wrong answer if x does not contain any elements of the set which you want to omit. E.g., x - 1:3 x[-which(x %in% c(0,255))] # bad integer(0) x[!is.element(x, c(0,255))] # good [1] 1 2 3 Bill Dunlap Spotfire, TIBCO Software wdunlap tibco.com x[ !x %in% c(0,255)] will do, rather than: If you want to remove the specific values 0 and 255 from your vector, try: x-x[-which(x %in% c(0,255))] Jim -- Bert __ 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. -- Men by nature long to get on to the ultimate truths, and will often be impatient with elementary studies or fight shy of them. If it were possible to reach the ultimate truths without the elementary studies usually prefixed to them, these would not be preparatory studies but superfluous diversions. -- Maimonides (1135-1204) 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. __ 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 Remove element with Particular Value In Vector
You can use 'which' and negative subscripts to remove elements from a vector. y-x[-(which(x==0|x==255))] chuan_zl wrote: Dear All. I am Chuan. I am beginner for R.I facing some problem in remove element from vector.I have a vector with size 238 element as follow(a part) [1] 0 18 24 33 44..[238] 255 Let the vector label as x,I want remove element 0 and 255.I try use such function: x[x0 x255] However, I am fail since same results are give even try it for many times.I also try with shorter vector with 10 element. It is successfully resulted. So,want can I do for it. Kindly asking favor for expert here. Thank you very much. Chuan -- View this message in context: http://r.789695.n4.nabble.com/Asking-Favor-For-Remove-element-with-Particular-Value-In-Vector-tp3772779p3774271.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 Remove element with Particular Value In Vector
Be careful about negating the 'which' in case there are no matches: x - 1:10 x[-which(x == 11)] integer(0) Notice it deletes the whole vector. Safer to use logical vectors: x[!(x==3 | x == 7)] [1] 1 2 4 5 6 8 9 10 x[!(x == 11)] # notice this works [1] 1 2 3 4 5 6 7 8 9 10 On Sun, Aug 28, 2011 at 7:20 AM, eyildiz engin.yildizt...@gmail.com wrote: You can use 'which' and negative subscripts to remove elements from a vector. y-x[-(which(x==0|x==255))] chuan_zl wrote: Dear All. I am Chuan. I am beginner for R.I facing some problem in remove element from vector.I have a vector with size 238 element as follow(a part) [1] 0 18 24 33 44..[238] 255 Let the vector label as x,I want remove element 0 and 255.I try use such function: x[x0 x255] However, I am fail since same results are give even try it for many times.I also try with shorter vector with 10 element. It is successfully resulted. So,want can I do for it. Kindly asking favor for expert here. Thank you very much. Chuan -- View this message in context: http://r.789695.n4.nabble.com/Asking-Favor-For-Remove-element-with-Particular-Value-In-Vector-tp3772779p3774271.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. -- Jim Holtman Data Munger Guru What is the problem that you are trying to solve? __ 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 Remove element with Particular Value In Vector
Dear All. I am Chuan. I am beginner for R.I facing some problem in remove element from vector.I have a vector with size 238 element as follow(a part) [1] 0 18 24 33 44..[238] 255 Let the vector label as x,I want remove element 0 and 255.I try use such function: x[x0 x255] However, I am fail since same results are give even try it for many times.I also try with shorter vector with 10 element. It is successfully resulted. So,want can I do for it. Kindly asking favor for expert here. Thank you very much. Chuan -- View this message in context: http://r.789695.n4.nabble.com/Asking-Favor-For-Remove-element-with-Particular-Value-In-Vector-tp3772779p3772779.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 Remove element with Particular Value In Vector
Not sure whether I understand your question right but here is what I would do: # Sample data x - seq( 1, 100, by=6) x [1] 1 7 13 19 25 31 37 43 49 55 61 67 73 79 85 91 97 # remove element with value 19 x - x[ x != 19 ] x [1] 1 7 13 25 31 37 43 49 55 61 67 73 79 85 91 97 If you want to remove values smaller / larger than a certain threshold, your way should work well: # Sample data x - seq( 1, 100, by=6) x[9] - 155 x [1] 1 7 13 19 25 31 37 43 155 55 61 67 73 79 85 91 97 # Remove elements smaller than 20 or larger than 80: x - x[ x 20 x 80 ] x [1] 25 31 37 43 55 61 67 73 79 So there is probably an issue with your data vector - why don't you dput() it? Rgds, Rainer On Saturday 27 August 2011 02:31:29 chuan_zl wrote: Dear All. I am Chuan. I am beginner for R.I facing some problem in remove element from vector.I have a vector with size 238 element as follow(a part) [1] 0 18 24 33 44..[238] 255 Let the vector label as x,I want remove element 0 and 255.I try use such function: x[x0 x255] However, I am fail since same results are give even try it for many times.I also try with shorter vector with 10 element. It is successfully resulted. So,want can I do for it. Kindly asking favor for expert here. Thank you very much. Chuan -- View this message in context: http://r.789695.n4.nabble.com/Asking-Favor-For-Remove-element-with-Particul ar-Value-In-Vector-tp3772779p3772779.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-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 Remove element with Particular Value In Vector
On Aug 27, 2011, at 5:31 AM, chuan_zl wrote: Dear All. I am Chuan. I am beginner for R.I facing some problem in remove element from vector.I have a vector with size 238 element as follow(a part) [1] 0 18 24 33 44..[238] 255 Let the vector label as x,I want remove element 0 and 255.I try use such function: x[x0 x255] I am not completely clear but it appears that you want to remove the first and last elements. You can use negative indexing vectors. x[ -c(1, length(x) ) ] However, I am fail Perhaps your vector is a factor? Try this and see what you get: str(x) since same results are give even try it for many times.I also try with shorter vector with 10 element. It is successfully resulted. So,want can I do for it. Kindly asking favor for expert here. Thank you very much. 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
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.
Re: [R] Asking Favor
Hi what exactly do you want. You say you can read your data x - read.jpeg(system.file(data, cat.jpg, package=rimage)) plot(x) What do you mean by pixel image? By reading a picture you get an object imagematrix str(x) imagematrix [1:420, 1:418, 1:3] 0.255 0.251 0.247 0.247 0.255 ... - attr(*, type)= chr rgb - attr(*, class)= chr [1:2] imagematrix array Do you want to extract part of this object? Here it is. plot(imagematrix(x[30:150, 30:150,])) Go through help pages and try examples from them. Regards Petr r-help-boun...@r-project.org napsal dne 21.09.2010 14:52:35: Hi Chuan, I'm forwarding your question to the list because I haven't used the rimage package... It's best if you post questions to the list anyway because you are more likely to get a fast and useful answer. On 20 September 2010 23:03, chuan zun liang wrote: Dear Michael: I am so sorry,disturb again.I can plot jpeg image in R under package rimage But How I can it into pixel image?I want exact some part of image.I found this function in package spatstat...Is it possible I do it?Thank a lot 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. __ 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
Hi Chuan, I'm forwarding your question to the list because I haven't used the rimage package... It's best if you post questions to the list anyway because you are more likely to get a fast and useful answer. On 20 September 2010 23:03, chuan zun liang wrote: Dear Michael: I am so sorry,disturb again.I can plot jpeg image in R under package rimage But How I can it into pixel image?I want exact some part of image.I found this function in package spatstat...Is it possible I do it?Thank a lot 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
Here's a way with less code... # assume original matrix is X u - unique( sort(X) ) ucdf - cumsum( tabulate(X)[u] ) Xcdf - matrix(ucdf[ match(X, u) ], 8) Michael On 16 September 2010 14:58, chuan zun liang chuan...@yahoo.com.my wrote: Hello Friend: Thanks a lot. I kindly told that Micheal already misunderstand what i want.But never mind.I also can share with you what I want. I got a set of matrix(image) as below: x-t(matrix(y,8)) x [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [1,] 52 55 61 66 70 61 64 73 [2,] 63 59 55 90 109 85 69 72 [3,] 62 59 68 113 144 104 66 73 [4,] 63 58 71 122 154 106 70 69 [5,] 67 61 68 104 126 88 68 70 [6,] 79 65 60 70 77 68 58 75 [7,] 85 71 64 59 55 61 65 83 [8,] 87 79 69 68 65 76 78 94 y just the matrix change to vector. y-c(52,55,61,66,70,61,64,73,63,59,55,90,109,85,69,72,62,59,68,113,144,104,66,73,63,58,71,122,154,106,70,69,67,61,68,104,126,88,68,70,79,65,60,70,77,68,58,75,85,71,64,59,55,61,65,83,87,79,69,68,65,76,78,94) Then, I want doing for histogram equalization for my image.What I want is I want to replace 52 by value of the cumulative distribution function. For example,frequency 52 is 1 and frequency for 55 is 3, so cumulative frequency for 55 is 4.So, I want replace 52 by 1 and 55 by 4 and so on until 94 replace 94. This is the script I get(just a part for histogram equalization): cdfv-function(p) {a-cumsum(unlist(lapply(split(p,f=p),length))) b-sort(unique(p)) for(i in c(1:37)) {x-replace(x,x==b[i],a[i])} c(x)} matrix(cdfv(y),8) Thank you. Chuan From: Michael Bedward michael.bedw...@gmail.com To: chuan zun liang chuan...@yahoo.com.my; Rhelp r-help@r-project.org Sent: Wednesday, 15 September 2010 16:08:20 Subject: Re: [R] Asking Favor Hello Chuan, If you just want a matrix with the numbers 1 to 64 arranged by row... m - matrix(1:64, ncol=8, byrow=TRUE) But perhaps I don't understand your question properly ? Michael On 16 September 2010 12:46, chuan zun liang chuan...@yahoo.com.my wrote: Dear Prof: My name is Chuan.I from Malaysia.I am a beginner for R.I need favor for Prof. This is my data: y-c(52,55,61,66,70,61,64,73,63,59,55,90,109,85,69,72,62,59,68,113,144,104,66,73,63,58,71,122,154,106,70,69,67,61,68,104,126,88,68,70,79,65,60,70,77,68,58,75,85,71,64,59,55,61,65,83,87,79,69,68,65,76,78,94) and I form it into matrix as below: [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [1,] 52 63 62 63 67 79 85 87 [2,] 55 59 59 58 61 65 71 79 [3,] 61 55 68 71 68 60 64 69 [4,] 66 90 113 122 104 70 59 68 [5,] 70 109 144 154 126 77 55 65 [6,] 61 85 104 106 88 68 61 76 [7,] 64 69 66 70 68 58 65 78 [8,] 73 72 73 69 70 75 83 94 But i want replace 52 by 1, 63 by 2, 63 by 5, 94 by 64. Is it possible I do it in R.Thank a lot. Chuan [[alternative HTML version deleted]] __ 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-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
Dear Prof: My name is Chuan.I from Malaysia.I am a beginner for R.I need favor for Prof. This is my data: y-c(52,55,61,66,70,61,64,73,63,59,55,90,109,85,69,72,62,59,68,113,144,104,66,73,63,58,71,122,154,106,70,69,67,61,68,104,126,88,68,70,79,65,60,70,77,68,58,75,85,71,64,59,55,61,65,83,87,79,69,68,65,76,78,94) and I form it into matrix as below: [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [1,] 52 63 626367 79 85 87 [2,] 55 59 595861 65 71 79 [3,] 61 55 687168 60 64 69 [4,] 66 90 113 122 104 70 59 68 [5,] 70 109 144 154 126 77 55 65 [6,] 61 85 104 10688 68 61 76 [7,] 64 69 667068 58 65 78 [8,] 73 72 736970 75 83 94 But i want replace 52 by 1, 63 by 2, 63 by 5, 94 by 64. Is it possible I do it in R.Thank a lot. Chuan [[alternative HTML version deleted]] __ 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
Hello Chuan, If you just want a matrix with the numbers 1 to 64 arranged by row... m - matrix(1:64, ncol=8, byrow=TRUE) But perhaps I don't understand your question properly ? Michael On 16 September 2010 12:46, chuan zun liang chuan...@yahoo.com.my wrote: Dear Prof: My name is Chuan.I from Malaysia.I am a beginner for R.I need favor for Prof. This is my data: y-c(52,55,61,66,70,61,64,73,63,59,55,90,109,85,69,72,62,59,68,113,144,104,66,73,63,58,71,122,154,106,70,69,67,61,68,104,126,88,68,70,79,65,60,70,77,68,58,75,85,71,64,59,55,61,65,83,87,79,69,68,65,76,78,94) and I form it into matrix as below: [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [1,] 52 63 62 63 67 79 85 87 [2,] 55 59 59 58 61 65 71 79 [3,] 61 55 68 71 68 60 64 69 [4,] 66 90 113 122 104 70 59 68 [5,] 70 109 144 154 126 77 55 65 [6,] 61 85 104 106 88 68 61 76 [7,] 64 69 66 70 68 58 65 78 [8,] 73 72 73 69 70 75 83 94 But i want replace 52 by 1, 63 by 2, 63 by 5, 94 by 64. Is it possible I do it in R.Thank a lot. Chuan [[alternative HTML version deleted]] __ 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-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.