Reputation: 13
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)
Upvotes: 0
Views: 2276
Reputation: 1545
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)
Upvotes: 1