{"action":"created","issue":{"url":"https://api.github.com/repos/stnava/ANTsR/issues/126","repository_url":"https://api.github.com/repos/stnava/ANTsR","labels_url":"https://api.github.com/repos/stnava/ANTsR/issues/126/labels{/name}","comments_url":"https://api.github.com/repos/stnava/ANTsR/issues/126/comments","events_url":"https://api.github.com/repos/stnava/ANTsR/issues/126/events","html_url":"https://github.com/stnava/ANTsR/issues/126","id":197310656,"number":126,"title":"4D to 3D image conversion?","user":{"login":"dorianps","id":9083517,"avatar_url":"https://avatars.githubusercontent.com/u/9083517?v=3","gravatar_id":"","url":"https://api.github.com/users/dorianps","html_url":"https://github.com/dorianps","followers_url":"https://api.github.com/users/dorianps/followers","following_url":"https://api.github.com/users/dorianps/following{/other_user}","gists_url":"https://api.github.com/users/dorianps/gists{/gist_id}","starred_url":"https://api.github.com/users/dorianps/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/dorianps/subscriptions","organizations_url":"https://api.github.com/users/dorianps/orgs","repos_url":"https://api.github.com/users/dorianps/repos","events_url":"https://api.github.com/users/dorianps/events{/privacy}","received_events_url":"https://api.github.com/users/dorianps/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":3,"created_at":"2016-12-23T04:13:09Z","updated_at":"2016-12-23T18:11:12Z","closed_at":null,"body":"Is there a way to \"unpack\" a 4D image into a series of 3D images within ANTsR?\r\nI have managed to do it with the following code but it doesn't look the best way to do it:\r\n\r\n```seg = antsImageRead(segfile)\r\n> seg\r\nantsImage\r\n  Pixel Type          : float \r\n  Components Per Pixel: 1 \r\n  Dimensions          : 196x237x194x5 \r\n  Voxel Spacing       : 0.699999988079071x0.699999988079071x0.699999988079071x1 \r\n  Origin              : 68.2 97.3 -65.7 0 \r\n  Direction           : -1 0 0 0 0 -1 0 0 0 0 1 0 0 0 0 1 \r\n\r\nmask <- as.antsImage(array( 1 , dim(seg)[1:3] ))\r\nmat = timeseries2matrix(seg, mask)\r\nnewseg = matrixToImages(mat,mask)\r\n\r\n> newseg[[1]]\r\nantsImage\r\n  Pixel Type          : float \r\n  Components Per Pixel: 1 \r\n  Dimensions          : 196x237x194 \r\n  Voxel Spacing       : 1x1x1 \r\n  Origin              : 0 0 0 \r\n  Direction           : 1 0 0 0 1 0 0 0 1 \r\n\r\n```"},"comment":{"url":"https://api.github.com/repos/stnava/ANTsR/issues/comments/269026608","html_url":"https://github.com/stnava/ANTsR/issues/126#issuecomment-269026608","issue_url":"https://api.github.com/repos/stnava/ANTsR/issues/126","id":269026608,"user":{"login":"dorianps","id":9083517,"avatar_url":"https://avatars.githubusercontent.com/u/9083517?v=3","gravatar_id":"","url":"https://api.github.com/users/dorianps","html_url":"https://github.com/dorianps","followers_url":"https://api.github.com/users/dorianps/followers","following_url":"https://api.github.com/users/dorianps/following{/other_user}","gists_url":"https://api.github.com/users/dorianps/gists{/gist_id}","starred_url":"https://api.github.com/users/dorianps/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/dorianps/subscriptions","organizations_url":"https://api.github.com/users/dorianps/orgs","repos_url":"https://api.github.com/users/dorianps/repos","events_url":"https://api.github.com/users/dorianps/events{/privacy}","received_events_url":"https://api.github.com/users/dorianps/received_events","type":"User","site_admin":false},"created_at":"2016-12-23T18:11:12Z","updated_at":"2016-12-23T18:11:12Z","body":"Hi @muschellij2 \r\n\r\nYour solution may be more memory efficient than the function I created:\r\n\r\n```\r\nantsImage4Dto3D = function(img) {\r\n  # check input is good\r\n  if ( class(img) != 'antsImage' ) stop('Input is not antsImage.')\r\n  if ( length(dim(img)) != 4 ) stop(paste('Input image is not 4D.'))\r\n  \r\n  # put everything to a matrix\r\n  mask <- as.antsImage(array( 1 , dim(img)[1:3] ))\r\n  mat = timeseries2matrix(img, mask)\r\n  \r\n  # put matrix to a list\r\n  newimg = matrixToImages(mat,mask)\r\n  rm(mat)\r\n  invisible(gc)\r\n  \r\n  # copy headers to new images\r\n  direction = antsGetDirection(img)[-4,-4]\r\n  spacing = antsGetSpacing(img)[-4]\r\n  origin = antsGetOrigin(img)[-4]\r\n  for (im in newimg) {\r\n    antsSetDirection(im, direction)\r\n    antsSetSpacing(im, spacing)\r\n    antsSetOrigin(im, origin)\r\n  }\r\n  return(newimg)\r\n}\r\n```\r\n\r\nI guess it needs compiling though, I cannot run it simply by sourcing your code:\r\n```\r\nsource('/data/jag/dpustina/Code/HCP/subset_4d.R')\r\nCreating a generic function from function subset_4d in the global environment\r\nError in rematchDefinition(definition, fdef, mnames, fnames, signature) : \r\n  methods can add arguments to the generic subset_4d only if '...' is an argument to the generic\r\n```\r\n\r\n@stnava do you have thoughts what is good to go in ANTsR? My function uses an intermediate matrix which may require a lot of memory for large 4D image, but gives a list of images. John's method gives only a single image (if my I am understanding the code correctly)."}}
{"actor":{"display_login":"dorianps"}}