Re: [R] From two colors to 01 sequences
On Tue, May 12, 2009 at 12:20:56PM +0100, Paul Smith wrote: I have got several pdf files with rows of colored rectangles: red rectangles should be read as 0; green rectangles as 1. No other color exists. Is there some way to have R reading the colored rectangles to a matrix or data frame converting the color of the rectangles to sequences of 01? I would not do it with R, but.. here's the general approach: 1. Convert the PDF to some raster format at high enough resolution (DPI) without any kind of compression or anti-aliasing 2. Use some image manipulation program to replace red/green with black/white 3. Save the resulting picture in ASCII PBM format 4. Parse the resulting PBM and find 0-1 and 1-0 transitions which will give you rectangle boundaries. 5. You did not specify the kind of rectangles, nor whether rows are of uniform height, so I assume uniform grid. Otherwise, position and size might also be relevant[1], the interpretation is completely up to you. [1] As in, for example, http://educ.queensu.ca/~fmc/october2001/GoldenArt3.gif (Imagine that there are only two colors instead of 4 + black lines) __ 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] From two colors to 01 sequences
Depending on the nature of your pdf file, it may be possible to use the grImport package. I've never used it before, but a quick test seems promising, # create a test picture colorStrip - function (colors, draw = T) { x - seq(0, 1 - 1/ncol(colors), length = ncol(colors)) y - rep(0.5, length(colors)) my.grob - grid.rect(x = unit(x, npc), y = unit(y, npc), width = unit(1/ncol(colors), npc), height = unit(1, npc), just = left, hjust = NULL, vjust = NULL, default.units = npc, name = NULL, gp = gpar(fill = rgb(colors[1, ], colors[2, ], colors[3, ]), col = rgb(colors[1, ], colors[2, ], colors[3, ])), draw = draw, vp = NULL) my.grob } colors - rbind(c(1, 0, 1), c(0, 1, 0), c(0, 0, 0)) pdf(testRGB.pdf) colorStrip(colors) dev.off() # import the pdf file into R library(grImport) PostScriptTrace(testRGB.pdf) test - readLines(testRGB.pdf.xml) testRead - readPicture(testRGB.pdf.xml) str(testRead) grid.picture(testRead) # somehow I've lost the fill color in the process?! grep(rgb.+, test, value=T) # this should allow you to find the sequence of red and green rectangles HTH, baptiste On 12 May 2009, at 13:38, Zeljko Vrba wrote: I have got several pdf files with rows of colored rectangles: red rectangles should be read as 0; green rectangles as 1. No other color exists. Is there some way to have R reading the colored rectangles to a matrix or data frame converting the color of the rectangles to sequences of 01? _ Baptiste Auguié School of Physics University of Exeter Stocker Road, Exeter, Devon, EX4 4QL, UK Phone: +44 1392 264187 http://newton.ex.ac.uk/research/emag __ [[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] From two colors to 01 sequences
Thanks, Baptiste and Zeljko. I am attaching here an example of the picture of the rectangles. Paul On Tue, May 12, 2009 at 1:23 PM, baptiste auguie ba...@exeter.ac.uk wrote: Depending on the nature of your pdf file, it may be possible to use the grImport package. I've never used it before, but a quick test seems promising, # create a test picture colorStrip - function (colors, draw = T) { x - seq(0, 1 - 1/ncol(colors), length = ncol(colors)) y - rep(0.5, length(colors)) my.grob - grid.rect(x = unit(x, npc), y = unit(y, npc), width = unit(1/ncol(colors), npc), height = unit(1, npc), just = left, hjust = NULL, vjust = NULL, default.units = npc, name = NULL, gp = gpar(fill = rgb(colors[1, ], colors[2, ], colors[3, ]), col = rgb(colors[1, ], colors[2, ], colors[3, ])), draw = draw, vp = NULL) my.grob } colors - rbind(c(1, 0, 1), c(0, 1, 0), c(0, 0, 0)) pdf(testRGB.pdf) colorStrip(colors) dev.off() # import the pdf file into R library(grImport) PostScriptTrace(testRGB.pdf) test - readLines(testRGB.pdf.xml) testRead - readPicture(testRGB.pdf.xml) str(testRead) grid.picture(testRead) # somehow I've lost the fill color in the process?! grep(rgb.+, test, value=T) # this should allow you to find the sequence of red and green rectangles HTH, baptiste On 12 May 2009, at 13:38, Zeljko Vrba wrote: I have got several pdf files with rows of colored rectangles: red rectangles should be read as 0; green rectangles as 1. No other color exists. Is there some way to have R reading the colored rectangles to a matrix or data frame converting the color of the rectangles to sequences of 01? _ Baptiste Auguié School of Physics University of Exeter Stocker Road, Exeter, Devon, EX4 4QL, UK Phone: +44 1392 264187 http://newton.ex.ac.uk/research/emag __ [[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.