0

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 : cran.r-project.org/web/packages/biOps/index.html). 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 (https://d37djvu3ytnwxt.cloudfront.net/assets/courseware/v1/e4d303da53c93dfaca745ad1236e7d53/asset-v1:MITx+15.071x_3+1T2016+type@asset+block/tumor.csv)

Gb Hari
  • 13
  • 2
  • 1
    I am not able to check this at work but it might lead you in the right direction. http://nielshorn.net/prog/vba/vbaXL_bmp2xls.php or maybe this http://stackoverflow.com/questions/27489947/convert-jpg-to-greyscale-csv-using-r – Captain Grumpy Sep 26 '16 at 04:50
  • This is quite broad and in danger of being closed. What is the criterion for converting a value to 0 or 1? Have you tried package `raster`? – Roman Luštrik Sep 26 '16 at 07:36
  • I have tried raster but no luck. – Gb Hari Sep 26 '16 at 07:54
  • In case if any one know how to do this with raster, share with me how to do it. – Gb Hari Sep 26 '16 at 08:38

1 Answers1

1

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.

library("EBImage")

img <- readImage("sample.jpg")    
img <- channel(img, "grey")
write.csv(t(img), "sample.csv", row.names = FALSE)
aoles
  • 1,525
  • 10
  • 17