6

A very long time ago I used Mathematica for a few months and found it very intuitive. Now for my work I use MATLAB. I was just trying some stuff out with Mathematica again and there is something I cannot quite get the hang of.

I import a stack of images. Then I want to know the dimensions of my stack, but when I use Length or Dimensions, it only lists the number of images. Also when I use ? it not only shows me information, but it also plots every single "image" in it, which takes very long.

My question here is how to get the image size(s) for the imported stack of images?

J. M.'s missing motivation
  • 124,525
  • 11
  • 401
  • 574
Leo
  • 257
  • 1
  • 4
  • 9
  • An example (test file) illustrative of your set of images would be helpful. Also see ref/format/DICOM – rm -rf Sep 21 '13 at 18:34
  • 1
    Hmm first I didnt understand what you meant by ref/format/DICOM and googling it even took me to MatLab for some reason. But now that I found this link its really useful. You want me to show some images here? – Leo Sep 21 '13 at 19:14
  • @rm-rf Thanks for your help. I added examples to the question. I cant upload actual files as its sensitive data. – Leo Sep 21 '13 at 19:35
  • Yes, the link that you found is the one that I was referring to. If you just pasted what I wrote in Mathematica's documentation, it would've given you the same page. Thanks for the example :) – rm -rf Sep 21 '13 at 19:54
  • Your question really is broad. Could you possibly narrow it down to fit the title? – Yves Klett Sep 21 '13 at 20:22
  • @YvesKlett oh yeah sorry, got carried away when adding examples, Ill narrow it down again – Leo Sep 21 '13 at 22:28
  • You should look at the help for CompundExpression ( ;) to get an idea where and how to use it esp. in contrast with Matlab. Also, ? is perhaps not what you want. Look up ImageDimensions and related. – Yves Klett Sep 22 '13 at 04:55
  • @YvesKlett Thats good advice, thank you. ImageDimensions did the trick! That gave me exactly what I wanted ( and what size() would have given me in matlab ). The related functions are also helping me with the transformations. So my mistake was not to realise that in Mathematica images are not just arrays. Thank you for answering my question. You dont want to do that formally so I can accept it? – Leo Sep 22 '13 at 08:36
  • I will post an answer, and would propose an edit to your question to remove all clutter. Do you have a multi-image DICOM sample image (other than the ExampleData one)? – Yves Klett Sep 22 '13 at 09:27
  • I found a zip here I can extract an example from that. How can I post a file in my question? – Leo Sep 22 '13 at 09:49
  • I edited your question rather drastically to reflect the actual question. Hopefully this is o.k. for you, otherwise you could roll back. – Yves Klett Sep 22 '13 at 10:17
  • No thats no prolem. It is exactly the question you answered, and the one that helped me. – Leo Sep 22 '13 at 11:17

2 Answers2

6

You can use ImageDimensions for this purpose (in this case for a multi-frame DICOM):

imagestack = 

  Import["http://www.barre.nom.fr/medical/samples/files/MR-MONO2-8-\16x-heart.gz"];

Length[imagestack]

ImageDimensions /@ imagestack

(* 16 *)

(* {{256, 256}, {256, 256}, {256, 256}, {256, 256}, {256, 256}, {256, 

  256}, {256, 256}, {256, 256}, {256, 256}, {256, 256}, {256, 

  256}, {256, 256}, {256, 256}, {256, 256}, {256, 256}, {256, 256}} *)

ListAnimate[img]

Mathematica graphics

Yves Klett
  • 15,383
  • 5
  • 57
  • 124
  • While the above is probably the most practical approach (+1), it might be worth noting that the dimensions are available in the "MetaInformation", at least for the example file: {"FrameCount", "Rows", "Columns"} /. Import["http://www.barre.nom.fr/medical/samples/files/MR-MONO2-8-16x-heart.gz", "MetaInformation"] --> {16, 256, 256}. – Michael E2 Sep 22 '13 at 18:15
  • @MichaelE2 please do post another answer, that is very useful information. – Yves Klett Sep 22 '13 at 19:41
5

One can get information about the images in a file from the metadata, provided it is there in the first place.

For example, in Yves Klett's example file, the "MetaInformation" contains information about "FrameCount", "Rows", and "Columns". These can be obtained as follows:

{"FrameCount", "Rows", "Columns"} /.
  Import["http://www.barre.nom.fr/medical/samples/files/MR-MONO2-8-16x-heart.gz",
         "MetaInformation"]
(* {16, 256, 256} *)

One can get the image dimensions of the frames from the element "ImageSize", too:

Import["http://www.barre.nom.fr/medical/samples/files/MR-MONO2-8-16x-heart.gz",
       "ImageSize"]
(* {256, 256} *)
Michael E2
  • 235,386
  • 17
  • 334
  • 747