Gb Hari Gb Hari - 1 year ago 121
R Question

How to convert grey scale image to respective pixel values (0-1) in the form of matrix or data frame?

In R Programming I want to convert grey scale image to respective pixel values in the form of matrix or data frame and if needed, I also wish to store the data in excel file.This process of conversion will help me to implement and analyze clustering methods on image in the form of intensity of each pixel. Many suggested biOps package for this grey scale image to pixel matrix conversion, But unfortunately this package in no more supported by R (link : I also tried with EBImage package when I was extracting pixel values of an Grey Scale image from summary using "imageData()" but values are stored as 3D array of values, but I want to make matrix or data frame where each pixel corresponds to a respective value 0 to 1. Any method in R can be suggested. for reference i share excel file of MRI scan image.MRI scan Image:this is how I want my image to be converted

you can download excel file here (

Answer Source

The reason why your greyscale image file loads into R as 3D array rather than a 2D matrix is that its pixel intensities are replicated across RGB color channels. The solution to this is to use the function channel to convert the data to Greyscale representation. This will give you a 2D matrix of pixel intensities in the [0:1] range. You can then use the regular write.csv function to save the image data into a .csv file. You might want to do the transposition to convert from column-major order representation used by R to row-major order.


img <- readImage("sample.jpg")    
img <- channel(img, "grey")
write.csv(t(img), "sample.csv", row.names = FALSE)
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download