Login  Register

Re: Saving DICOM Headers

Posted by Bruno on May 22, 2014; 9:23am
URL: http://imagej.273.s1.nabble.com/Saving-DICOM-Headers-tp5007833p5007849.html

Hi Michael,

I had a similar problem and solved by a work-around:
Split the header and raw image data in separate files, edit and save the raw image and glue the files together again.
File split files via a macro, see below.

This will generate two files (.header and .image) and a log window with information to load the raw image data (width, height and #bits).
For 16 bits images you have to find out if it uses little or big endian byte order (see file>import>raw dialog) and when saving via save_as > raw data you can set this in the options>io>"Save raw in intel byte order".

To glue the files to a new dicom file I use the dos command: "copy /b file.header + file.image file.dcm"

It's a bit tricky but works for me.

Bruno.


Macro:

path=File.openDialog("Open DICOM file")

open(path); // open DICOM file
file_size=File.length(path);
header_size=getNumericTag("7FE0,0010"); // offset to image data
print("file: "+path);
print("rows "+ getNumericTag("0028,0010"));
print("columns "+ getNumericTag("0028,0011"));
print("bits "+ getNumericTag("0028,0100"));
print("samples per pixel "+ getNumericTag("0028,0002"));
close();

run("Raw...", "open=" + path + " image=8-bit width=1 height="+header_size+" offset=0 number=1 gap=0 little-endian");
saveAs("Raw Data", path+".header");
close();
run("Raw...", "open=" + path + " image=8-bit width=1 height=" + file_size-header_size + " offset=header_size number=1 gap=0 little-endian");
saveAs("Raw Data", path+".image");
close();

  // This function returns the numeric value of the
  // specified tag (e.g., "0018,0050"). Returns NaN
  // (not-a-number) if the tag is not found or it
  // does not have a numeric value.
  function getNumericTag(tag) {
    value = getTag(tag);
    if (value=="") return NaN;
    index3 = indexOf(value, "\\");
    if (index3>0)
      value = substring(value, 0, index3);
    value = 0 + value; // convert to number
    return value;
  }
 

  // This function returns the value of the specified
  // tag  (e.g., "0010,0010") as a string. Returns ""
  // if the tag is not found.
  function getTag(tag) {
      info = getImageInfo();
      index1 = indexOf(info, tag);
      if (index1==-1) return "";
      index1 = indexOf(info, ":", index1);
      if (index1==-1) return "";
      index2 = indexOf(info, "\n", index1);
      value = substring(info, index1+1, index2);
      return value;
  }

--
ImageJ mailing list: http://imagej.nih.gov/ij/list.html