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

Roger Bivand Roger.Bivand at nhh.no
Thu Mar 9 18:47:57 CET 2006

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":

x <- read.pnm(system.file("pictures/logo.ppm", package="pixmap")[1])
z <- as(x, "pixmapGrey")
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((c(greydata))) # rather a lot of different values, let's round:
rgreydata <- round(greydata*10)
length(unique(c(rgreydata))) # looks good

I assume that you converted from tiff or jpeg to pnm outside pixmap.

> Thanks for any hint, Christian.

Roger Bivand
Economic Geography Section, Department of Economics, Norwegian School of
Economics and Business Administration, Helleveien 30, N-5045 Bergen,
Norway. voice: +47 55 95 93 55; fax +47 55 95 95 43
e-mail: Roger.Bivand at nhh.no

More information about the R-help mailing list