[R] Questions on Random Forest

Fucang Jia jiafucang at hotmail.com
Mon Nov 24 16:31:20 CET 2003

Hi, everyone,

I am a newbie on R. Now I want to do image pixel classification by random 
forest. But I has not a clear understanding on random forest. Here is some 

As for an image, for example its size is 512x512 and has only one variable 
-- gray level. The histogram of the image looks like mixture Gaussian Model, 
say Gauss distribution (u1,sigma1), (u2,sigma2),(u3,sigma3). And a image 
classified by K-means or EM algorithm, so the class label image is also 
512x512 and has 0, 1, 2 value.

I read the binary image data as follows:

datafile <- file("bone.img","rb")
img <- readBin(datafile,size=2,what="integer",n=512*512,signed=FALSE)
img <- as.matrix(img)

labelfile <- file(label.img","rb")
label <- readBin(labelfile,size=2,what="integer",n=512*512,signed=FALSE)
label <- as.matrix(label)

img_and_label <- c(img,label)  // binds the image data and class label
img_and_label <- as.matrix(img_and_label)
img_and_label <- array(img_and_label, dim=c(262144,2))

Random Forest need a class label like "Species" in the  iris. I do not know 
to set a class label like "Species" to the img.  So I run the command as 

rf <- randomForest(img_and_label[,2],data=image_and_label,importance=TRUE,

which outputs:

Error in if (n == 0) stop("data (x) has 0 rows") :
        argument is of length zero

Could anyone tell what is wrong and how can do the RF?

Secondly, if there is an new image , say img3 (dimension is 512x512,too), 
how can I
use the former result to classifify the new image?

Thirdly, whether or not random forest be used well if there is only one 
variable, say pixel
gray level, or three variables, such as red, green, blue color component to 
an true color

Thank you very much!



Fucang Jia, Ph.D student
Institute of Computing Technology, Chinese Academy of Sciences
Post.Box 2704
Beijing, 100080
E-mail:fcjia at ict.ac.cn

More information about the R-help mailing list