Dear list members, I need to translate 3 lines of matlab code to R (a loop, to be specific), and I don't know what would be the results in matlab or how to do it in R-- I don't realise if they are doing to the col, vector or what. if the results are a vector or a value or a matrix :-( Anyone with matlab, can run it and give me the result? Any ideias what am I doing wrong? The code is bellow...I have put the matlab code, followed by what I think would be the R code . Any help would be greatly appreciated, Thank you very much in advance, Best wishes, Marta * # from Rubin 2004: The following example opens an image (line 1), displays the image (line 2), # converts the image to grayscale data (line 3), and calculates the autocorrelation curve # for offset distances of one pixel to 99 pixels (lines 7–10). This code can be used to # perform steps (4) and (6) listed above in the Summary of Steps to Calculate Grain # Size of Natural Sediment. ColorData = imread(‘ImageName.jpg’); x <- read.jpeg("sed2.jpg")# opens the image imshow(ColorData); plot(x) # plot image data = double(rgb2gray(ColorData)); x=rgb2grey(x) # transform in grayscale, the image ; # example of a similar data matrix: data double(0.54878431372549,0.468,0.553411764705882,0.663529411764706, 0.440156862745098,0.330313725490196,0.473843137254902,0.690980392156863, 0.483333333333333,0.344862745098039,0.458156862745098,0.647843137254902) ImageWidth = size(data,2); dim(A)[2] # i.e. number of col MaxOffset = 99; # defined variable, =2R ImageWidthToProcess = ImageWidth-MaxOffset; # =2R defined variable for Offset = 1:MaxOffset ; loop to calculate the autocorrelation. basicly, they do cor2, which I think is Spearman correlation, between every column and store it in the vector position??? OffsetPlaquette = data(:,1+Offset:ImageWidthToProcess+Offset); AutoCData(Offset) = corr2(data(:,1:ImageWidthToProcess), OffsetPlaquette) end # IN R would be... but it is not correct! What would be the results in matlab? and how to do it in R? for(Offset in 1:MaxOffset){ OffsetPlaquette = x[,1+Offset:ImageWidthToProcess+Offset] AutoCData[Offset] = cor(c(x[,1:ImageWidthToProcess]), c(OffsetPlaquette)) } * -- LNEG_VS_Poli_CMYK_300dpi Laboratório Nacional de Energia e Geologia, I.P. Estrada da Portela, Zambujal - Alfragide Apartado 7586, 2720-866 Amadora www.lneg.pt [[alternative HTML version deleted]]
Dear list members, I need to translate 3 lines of matlab code to R (a loop, to be specific), and I don't know what would be the results in matlab or how to do it in R-- I don't realise if they are doing to the col, vector or what. if the results are a vector or a value or a matrix :-( Anyone with matlab, can run it and give me the result? Any ideias what am I doing wrong? The code is bellow...I have put the matlab code, followed by what I think would be the R code . Any help would be greatly appreciated, Thank you very much in advance, Best wishes, Marta * # from Rubin 2004: The following example opens an image (line 1), displays the image (line 2), # converts the image to grayscale data (line 3), and calculates the autocorrelation curve # for offset distances of one pixel to 99 pixels (lines 7–10). This code can be used to # perform steps (4) and (6) listed above in the Summary of Steps to Calculate Grain # Size of Natural Sediment. ColorData = imread(‘ImageName.jpg’); x <- read.jpeg("sed2.jpg")# opens the image imshow(ColorData); plot(x) # plot image data = double(rgb2gray(ColorData)); x=rgb2grey(x) # transform in grayscale, the image ; # example of a similar data matrix: data double(0.54878431372549,0.468,0.553411764705882,0.663529411764706, 0.440156862745098,0.330313725490196,0.473843137254902,0.690980392156863, 0.483333333333333,0.344862745098039,0.458156862745098,0.647843137254902) ImageWidth = size(data,2); dim(A)[2] # i.e. number of col MaxOffset = 99; # defined variable, =2R ImageWidthToProcess = ImageWidth-MaxOffset; # =2R defined variable for Offset = 1:MaxOffset ; loop to calculate the autocorrelation. basicly, they do cor2, which I think is Spearman correlation, between every column and store it in the vector position??? OffsetPlaquette = data(:,1+Offset:ImageWidthToProcess+Offset); AutoCData(Offset) = corr2(data(:,1:ImageWidthToProcess), OffsetPlaquette) end # IN R would be... but it is not correct! What would be the results in matlab? and how to do it in R? for(Offset in 1:MaxOffset){ OffsetPlaquette = x[,1+Offset:ImageWidthToProcess+Offset] AutoCData[Offset] = cor(c(x[,1:ImageWidthToProcess]), c(OffsetPlaquette)) } * [[alternative HTML version deleted]]
Dear Marta, I did it in Matlab, and fiddled around with R code until I had *almost* the same result. The "almost" is probably due to R handling the picture values (ranging from 0 to 1) differently than Matlab (ranging from 0 to 255), and simply multiplying the R picture values by 255 did NOT result in exactly the same values as the Matlab values. [what seems white in the picture is 245 in Matlab, although values potentially range to 255, and white is 0.9642549 in R, which multiplied by 255 gives 245.12, e.g.] But maybe the precision of this solution is good enough for you .. The corr2 demand from matlab is a 2D correlation coefficient - the R command cor works elementwise, and is not the solution here. Below I tried to implement the formula given in the following matlab page: http://www.mathworks.com/access/helpdesk/help/toolbox/images/corr2.html Maybe somebody on the list has a nice idea how to make the code more elegant This is the complete code in R setwd("D:/ wherever ") library(ReadImages) x <- read.jpeg(" whichever .jpg") #open image plot(x) #plot image x <- rgb2grey(x) #convert to greyscale plot(x) # check ;-) the image is in grey scale ImageWidth = dim(x)[2] #number of col MaxOffset = 99; #defined variable ImageWidthToProcess = ImageWidth-MaxOffset; #col-defined variable ## this one does NOT work because matrices not square: for(k in 1: MaxOffset) { OffsetPlaquette <- x[ , c((1+ k) : (ImageWidthToProcess + k))] dataToProcess <- x[,c(1:ImageWidthToProcess)] AutoCData[k] <- mantel(OffsetPlaquette, dataToProcess) } AutoCData ## END this one does not work because matrices not square AutoCData <- rep(0, MaxOffset) sumBothM <- rep(0, MaxOffset) sum1stMsq <- rep(0, MaxOffset) sum2ndMsq <- rep(0, MaxOffset) for(k in 1: MaxOffset) { OffsetPlaquette <- x[ ,(1+k) : (ImageWidthToProcess + k)] dataToProcess <- x[,c(1:ImageWidthToProcess)] meanM <- mean(OffsetPlaquette); meanM2 <- mean(dataToProcess) for(j in 1:dim(dataToProcess)[2]){ for(i in 1:dim(OffsetPlaquette)[1]){ sumBothM[k] <- sumBothM[k] + (OffsetPlaquette[i,j]-meanM)*(dataToProcess[i,j]-meanM2) sum1stMsq[k] <- sum1stMsq[k] + (OffsetPlaquette[i,j]-meanM)2 sum2ndMsq[k] <- sum2ndMsq[k] + (dataToProcess[i,j]-meanM2)2 } } AutoCData[k] <- sumBothM[k]/(sqrt(sum1stMsq[k] * sum2ndMsq[k])) } AutoCData Best wishes, Susanne __________________________________________________ Do You Yahoo!? Sie sind Spam leid? Yahoo! Mail verf?gt ?ber einen herausragenden Schutz gegen Massenmails. http://mail.yahoo.com
Dear Susanne, Thank you for your answer :-) and for the other people that helped privately. I have been running the code with a friend, and we reached a similar conclusion. Matlab, apparently automatically transforms the matrices in vector and does the correlation between vectors thus obtaining one value only. This differs also from octave, which as R, do the correlation of the matrices*matrices. I managed to sort this out, transforming the matrices into vectors, simply by adding a c before. We also found this aspect of 0-1 and 1-255 intriguing.In the end, I used a formula from a friend to do the transformation, getting a similar values in both programmes. Here's the code: ImageWidth = dim(x)[2] #number of col MaxOffset = 99; #defined variable ImageWidthToProcess = ImageWidth-MaxOffset; #col-defined variable AutoCData=0 #dif from matlab: in R we should create the matrix/dataframe where we will store the data created by the loop for(Offset in 1:MaxOffset){ #Offset=2 OffsetPlaquette = x[,c(1+Offset):c(ImageWidthToProcess+Offset)] AutoCData[Offset] = cor(c(x[,1:ImageWidthToProcess]), c(OffsetPlaquette)) print(Offset) } AutoCData plot(AutoCData) ### COOL :-) The results were very similar to matlab. I still have many lines to go :-) Using the function you very well produced (thank you so much), the difference between the results of the two are low summary(AutoCData2-AutoCData) Min. 1st Qu. Median Mean 3rd Qu. -2.581e-15 -2.741e-16 -2.082e-17 5.723e-17 2.637e-16 Max. 5.329e-15 which is good! All the best, Marta 2010/3/29 Susanne Schmidt <s.schmidt@bham.ac.uk>> Dear Marta, > > I did it in Matlab, and fiddled around with R code until I had *almost* the > same result. The "almost" is probably due to R handling the picture values > (ranging from 0 to 1) differently than Matlab (ranging from 0 to 255), and > simply multiplying the R picture values by 255 did NOT result in exactly the > same values as the Matlab values. [what seems white in the picture is 245 in > Matlab, although values potentially range to 255, and white is 0.9642549 in > R, which multiplied by 255 gives 245.12, e.g.] > But maybe the precision of this solution is good enough for you .. > > The corr2 demand from matlab is a 2D correlation coefficient - the R > command cor works elementwise, and is not the solution here. > Below I tried to implement the formula given in the following matlab page: > http://www.mathworks.com/access/helpdesk/help/toolbox/images/corr2.html > > Maybe somebody on the list has a nice idea how to make the code more > elegant > > This is the complete code in R > > setwd("D:/ wherever ") > library(ReadImages) > x <- read.jpeg(" whichever .jpg") #open image > > plot(x) #plot image > x <- rgb2grey(x) #convert to greyscale > plot(x) # check ;-) the image is in grey scale > > ImageWidth = dim(x)[2] #number of col > MaxOffset = 99; #defined variable > ImageWidthToProcess = ImageWidth-MaxOffset; #col-defined variable > > ## this one does NOT work because matrices not square: > for(k in 1: MaxOffset) { > OffsetPlaquette <- x[ , c((1+ k) : (ImageWidthToProcess + k))] > dataToProcess <- x[,c(1:ImageWidthToProcess)] > AutoCData[k] <- mantel(OffsetPlaquette, dataToProcess) > } > AutoCData > ## END this one does not work because matrices not square > > > AutoCData <- rep(0, MaxOffset) > sumBothM <- rep(0, MaxOffset) > sum1stMsq <- rep(0, MaxOffset) > sum2ndMsq <- rep(0, MaxOffset) > for(k in 1: MaxOffset) { > OffsetPlaquette <- x[ ,(1+k) : (ImageWidthToProcess + k)] > dataToProcess <- x[,c(1:ImageWidthToProcess)] > meanM <- mean(OffsetPlaquette); meanM2 <- mean(dataToProcess) > for(j in 1:dim(dataToProcess)[2]){ > for(i in 1:dim(OffsetPlaquette)[1]){ > sumBothM[k] <- sumBothM[k] + > (OffsetPlaquette[i,j]-meanM)*(dataToProcess[i,j]-meanM2) > sum1stMsq[k] <- sum1stMsq[k] + (OffsetPlaquette[i,j]-meanM)^2 > sum2ndMsq[k] <- sum2ndMsq[k] + (dataToProcess[i,j]-meanM2)^2 > } > } > AutoCData[k] <- sumBothM[k]/(sqrt(sum1stMsq[k] * sum2ndMsq[k])) > } > > AutoCData > > > Best wishes, > Susanne >[[alternative HTML version deleted]]