Hi: Goal: use R to turn a matrix of 1's and 0's into a corresponding image (e.g. png) of black and white pixels. Why R: Yes, I can do this more efficiently and precisely with a perl module like Image::PBM. Been there, done that many times, etc. (Just humor me. I'm trying to do this with R for a number of reasons.) Problem: Difficult to get a perfect rasterization. There can be appended or removed pixel columns or pixel rows depending on plot region dimensions. I witness this with both R version 1.8.1 and R version 2.0. print($out "bitmap('/usr/local/mycrow/tmp/out.png', type = 'png256', height = ".(int($height*68/64)/64).", width = ".(int($width*69/64)/64).", res = 64, pointsize=0)\n"); print($out 'par(mar=c(0,0,0,0))'."\n"); my $width1 = $width-1; my $height1 = $height-1; print($out <<END); plot.new() plot.window(c(0,$width1),c(0,$height1)) rect(m[,1], m[,2], m[,1], m[,2], col="black", border="black") There are alternatives to rect (plot with type="p", pch=".", etc) and I have also tried png() instead of bitmap(). (I do prefer bitmap so this can run without x11.) I am guessing that R's internal region calculations are vector based, which generally makes sense for most statistical plots. However, I do have some ideas for R and the presentation of cellular automata results. Any tips out there? (Is it just a matter of height=50px to overcome the inches default, etc?)..... Regards, Scott
On Thu, 14 Oct 2004, Scott Harrison wrote:> Hi: > > Goal: use R to turn a matrix of 1's and 0's > into a corresponding image (e.g. png) > of black and white pixels.Maybe use the write.pnm() function in the pixmap package:> try1 <- matrix(0, nrow=100, ncol=100) > try1[upper.tri(try1)] <- 1 > write.pnm(pixmapGrey(try1), file="~/tmp/try1.pbm", type="pbm")should do it (but as an ASCII file, bits are not so easy to do one by one). You need to watch which row order is used if the matrix and image row orders are to "look" the same. This is assuming that you don't actually need to use a graphics device, but have an R matrix object you need to represent. Another package you could explore is rimage, which looks relevant. Roger> > Why R: Yes, I can do this more efficiently and precisely > with a perl module like Image::PBM. Been there, > done that many times, etc. (Just humor me. > I'm trying to do this with R for a number of reasons.) > > Problem: Difficult to get a perfect rasterization. There can > be appended or removed pixel columns or pixel rows > depending on plot region dimensions. I witness this > with both R version 1.8.1 and R version 2.0. > > print($out "bitmap('/usr/local/mycrow/tmp/out.png', type = 'png256', > height = ".(int($height*68/64)/64).", width = > ".(int($width*69/64)/64).", res = 64, pointsize=0)\n"); > print($out 'par(mar=c(0,0,0,0))'."\n"); > my $width1 = $width-1; > my $height1 = $height-1; > print($out <<END); > plot.new() > plot.window(c(0,$width1),c(0,$height1)) > rect(m[,1], m[,2], m[,1], m[,2], col="black", border="black") > > There are alternatives to rect (plot with type="p", pch=".", etc) > and I have also tried png() instead of bitmap(). (I do prefer > bitmap so this can run without x11.) > > I am guessing that R's internal region calculations are > vector based, which generally makes sense for most statistical > plots. However, I do have some ideas for R and the presentation > of cellular automata results. > > Any tips out there? (Is it just a matter of height=50px to > overcome the inches default, etc?)..... > > Regards, > Scott > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html >-- Roger Bivand Economic Geography Section, Department of Economics, Norwegian School of Economics and Business Administration, Breiviksveien 40, N-5045 Bergen, Norway. voice: +47 55 95 93 55; fax +47 55 95 93 93 e-mail: Roger.Bivand at nhh.no
Martin Maechler
2004-Oct-14 16:08 UTC
[R] fidelity of generated raster images (R and perl)
>>>>> "Scott" == Scott Harrison <harris41 at msu.edu> >>>>> on Thu, 14 Oct 2004 11:13:16 -0400 writes:Scott> Hi: Goal: use R to turn a matrix of 1's and 0's into Scott> a corresponding image (e.g. png) of black and white Scott> pixels. Scott> Why R: Yes, I can do this more efficiently and Scott> precisely with a perl module like Image::PBM. Been Scott> there, done that many times, etc. (Just humor me. Scott> I'm trying to do this with R for a number of Scott> reasons.) Scott> Problem: Difficult to get a perfect rasterization. Scott> There can be appended or removed pixel columns or Scott> pixel rows depending on plot region dimensions. I Scott> witness this with both R version 1.8.1 and R version Scott> 2.0. <....> Scott> There are alternatives to rect (plot with type="p", Scott> pch=".", etc) and I have also tried png() instead of Scott> bitmap(). (I do prefer bitmap so this can run Scott> without x11.) I think the "most typical" alternative is to use image() which is probably the most efficient currently --- it is used by the plot() method of "pixmap" objects in package 'pixmap' {which you probably should really look at!}. Note however that image() is still very inefficient for high-resolution (already 1000x1000) images since it really fills a rectangular polygon for each pixel. This e.g. leads to horribly large postscript files when plotting such pixmaps. In some way, this is a known "missing feature" in R's graphics engines. However a nice implementation would probably use properties of the graphics device: A smartImage() function would ``see'' that for a given output device, it should only draw pixels instead of rects(); even smarter for even larger pixmaps (where there are less "pixels" on the output device than you have in your image) it would even average ("color-smooth") neighboring pixmap pixels into device pixels. Scott> I am guessing that R's internal region calculations Scott> are vector based, which generally makes sense for Scott> most statistical plots. However, I do have some Scott> ideas for R and the presentation of cellular automata Scott> results. Scott> Any tips out there? (Is it just a matter of Scott> height=50px to overcome the inches default, Scott> etc?)..... Scott> Regards, Scott Regards, Martin Maechler