[R] count pixels of same color in pixmap object?

Philippe Grosjean phgrosjean at sciviews.org
Fri Mar 10 08:55:17 CET 2006

Another solution is to use external tools. It really depends on what you 
have to do: if it is to count pixels of one, or a few gray levels, it 
could be fine to do it in R,... but if you want to count *all* pixels, 
this could be more efficient using a C program, especially if you work 
with 16bit images! You can find something in ImageMagick or in Netpbm. 
Here is a solution using "pgmhist" from Netpbm 
(http://netpbm.sourceforge.net/). This implies that you first converted 
your image in PGM format (the Netpbm library provides all tools required 
for that conversion).
Here is the R code, assuming "pgmhist" or "pgmhist.exe" (Windows) is 
installed and accessible from the path:

# Get statistics about pixel in graylevel pgm images
pixelStats <- function(imagePgm, pgmhist =
     if (.Platform$OS.type == "windows") "pgmhist.exe" else "pgmhist") {
     # Get pixel count for each gray level
     pix <- system(paste(pgmhist, imagePgm),
         intern = TRUE, invisible = TRUE)
     if (pix[1] != "value\tcount\tb%\tw%")
         stop("Error when running 'pgmhist' on the image ",
             imagePgm, "\n\n", pix)
     pix <- pix[-(1:2)]
     getPixCount <- function(str)
         as.numeric(strsplit(str, "\t")[[1]][1:2])
     pix2 <- t(sapply(pix, getPixCount, USE.NAMES = FALSE))
     colnames(pix2) <- c("pixel.value", "count")


Depending on the treatment you have to do, it is sometimes better to 
delegate it to specialized programs (ImageMagick, Netpbm, ImageJ, etc.) 
instead of using pixmap. The best is to try both and to determine which 
solution is faster for your particular application. I am working with 
very large images (hundreds of megabytes) for the ZooImage application 
(http://www.sciviews.org/zooimage), and I delegate 100% of the work done 
on these images to external programs, because R is not designed to 
handle them efficiently.

Philippe Grosjean

Roger Bivand wrote:
> On Thu, 9 Mar 2006, Christian Jost wrote:
>>Dear all,
>>I try to figure out how to use R to count the number of pixels of the 
>>same color in some gray-level picture. I managed to read it in either 
>>tiff or jpeg format, but the returned pixmap object keeps its 
>>information out of (my) reach. Is there an easy way to tabulate the 
>>different color/graylevel pixels and their numbers? Or should I use a 
>>completely different (free) software?
> The objects are new-style class objects, so they are documented internally 
> - query by getSlots() or from ?"pixmap-class":
> library(pixmap)
> x <- read.pnm(system.file("pictures/logo.ppm", package="pixmap")[1])
> z <- as(x, "pixmapGrey")
> class(z)
> getSlots(class(z)) # lets you know what the slots are
> greydata <- slot(z, "grey") # extract the slot object
> summary(c(greydata)) # c() to flatten a matrix
> length(unique(c(greydata)))
> length((c(greydata))) # rather a lot of different values, let's round:
> rgreydata <- round(greydata*10)
> length(unique(c(rgreydata))) # looks good
> table(c(rgreydata))
> I assume that you converted from tiff or jpeg to pnm outside pixmap.
>>Thanks for any hint, Christian.

More information about the R-help mailing list