Robert Meglen
Robert Meglen

Reputation: 21

Script to read yaml matrices for hyperspectral images in R-statistic

I have not used yaml file input with R-stat previously. I have a xxxx.yaml file containing 480 matrices in following format. Each matrix is a slice in a hyperspectral image that is 190 rows X 291 cols. I have looked at R package 'yaml' but can't figure out how to write an R script to read these data into an unfolded matrix with rows=139680, cols=180.

%YAML:1.0
ImFrame0: !!opencv-matrix
   rows: 180
   cols: 291
   dt: f
   data: [ 1.53931296e+000, 5.05877912e-001, 2.08477139e+000, 1.61429548e+000, 1.71412516e+000, 1.29397893e+000,.....]
   ...   
   ...
   ...

ImFrame480: !!opencv-matrix
   rows: 180
   cols: 291
   dt: f
   data: [ 1.32225396e+000, 4.05347912e-001, 3.11177139e+000, 1.87459548e+000, 1.34523456e+000, .....]

Upvotes: 2

Views: 275

Answers (1)

Justin
Justin

Reputation: 43265

Assuming that the data key is a 1-D vector of all the 180x291 maxtrix of data and it is to be filled row by row, the code below should be a start... I'm not totally sure how you go from 480 180x291 matricies to a single 139680x180 matrix... but once you have a list of matricies in a known shape, the rest should follow.

Yaml

ImFrame0: !!opencv-matrix
   rows: 3
   cols: 2
   dt: f
   data: [ 1.53931296e+000, 5.05877912e-001, 2.08477139e+000, 1.61429548e+000, 1.71412516e+000, 1.29397893e+000]
ImFrame480: !!opencv-matrix
   rows: 3
   cols: 2
   dt: f
   data: [ 1.32225396e+000, 4.05347912e-001, 3.11177139e+000, 1.87459548e+000, 1.34523456e+000, 4.05347912e-001]

R Code

library(yaml)

ll <- yaml.load_file('path/to/your.yaml')

from_yaml <- function(l) {
    matrix(l$data, nrow=l$rows, ncol=l$rows, byrow=TRUE)
}

list_of_mats <- lapply(ll, from_yaml)

Upvotes: 1

Related Questions