using System;
using System.Collections.Generic;
using System.IO;
namespace itk.simple.examples
{
public class DicomSeriesFromArray
{
static void WriteSlices(List<Tuple<string, string>> seriesTag,
Image inImage,
string outDir,
int i)
{
Image imageSlice = inImage.Slice(0, (
long)i, 2);
foreach (var tagValue in seriesTag)
{
imageSlice.SetMetaData(tagValue.Item1, tagValue.Item2);
}
imageSlice.
SetMetaData(
"0008|0012", DateTime.Now.ToString(
"yyyyMMdd"));
imageSlice.SetMetaData("0008|0013", DateTime.Now.ToString("HHmmss"));
imageSlice.SetMetaData("0008|0060", "CT");
imageSlice.SetMetaData("0020|0032", string.Join("\\", position));
imageSlice.SetMetaData("0020|0013", i.ToString());
}
public static void Main(string[] args)
{
if (args.Length < 1)
{
Console.WriteLine("Usage: DicomSeriesFromArray <output_directory>");
return;
}
Random rand = new Random();
uint[] imageSize = { 512, 512, 256 };
int totalPixels = (int)(imageSize[0] * imageSize[1] * imageSize[2]);
{
short[] randomData = new short[totalPixels];
for (int i = 0; i < totalPixels; i++)
{
randomData[i] = (short)(rand.Next(-1000, 1001));
}
newImg = SimpleITK.ImportAsInt16(randomData, new VectorUInt32(imageSize));
}
{
double[] randomData = new double[totalPixels];
for (int i = 0; i < totalPixels; i++)
{
randomData[i] = rand.NextDouble() * 2000.0 - 1000.0;
}
newImg = SimpleITK.ImportAsFloat64(randomData, new VectorUInt32(imageSize));
}
else
{
throw new ArgumentException("Unsupported pixel type");
}
newImg.
SetSpacing(
new VectorDouble(
new double[] { 2.5, 3.5, 4.5 }));
string modificationTime = DateTime.Now.ToString("HHmmss");
string modificationDate = DateTime.Now.ToString("yyyyMMdd");
List<Tuple<string, string>> seriesTagValues = new List<Tuple<string, string>>
{
new Tuple<string, string>("0008|0031", modificationTime),
new Tuple<string, string>("0008|0021", modificationDate),
new Tuple<string, string>("0008|0008", "DERIVED\\SECONDARY"),
new Tuple<string, string>("0020|000e", "1.2.826.0.1.3680043.2.1125." + modificationDate + ".1" + modificationTime),
new Tuple<string, string>("0020|0037", string.Format("{0}\\{1}\\{2}\\{3}\\{4}\\{5}",
direction[0], direction[3], direction[6], direction[1], direction[4], direction[7])),
new Tuple<string, string>("0008|103e", "Created-SimpleITK")
};
{
double rescaleSlope = 0.001;
seriesTagValues.AddRange(new List<Tuple<string, string>>
{
new Tuple<string, string>("0028|1053", rescaleSlope.ToString()),
new Tuple<string, string>("0028|1052", "0"),
new Tuple<string, string>("0028|0100", "16"),
new Tuple<string, string>("0028|0101", "16"),
new Tuple<string, string>("0028|0102", "15"),
new Tuple<string, string>("0028|0103", "1")
});
}
for (
int i = 0; i < (int)newImg.
GetDepth(); i++)
{
WriteSlices(seriesTagValues, newImg, args[0], i);
}
string dataDirectory = args[0];
if (seriesIDs.Count == 0)
{
Console.WriteLine("ERROR: given directory \"" + dataDirectory + "\" does not contain a DICOM series.");
return;
}
VectorDouble originalSpacing = newImg.
GetSpacing();
Console.WriteLine("[{0}, {1}, {2}] vs [{3}, {4}, {5}]",
readSpacing[0], readSpacing[1], readSpacing[2],
originalSpacing[0], originalSpacing[1], originalSpacing[2]);
}
}
}
Write out a SimpleITK image to the specified file location.
void KeepOriginalImageUIDOn()
Set/Get name of ImageIO to use.
void Execute(const Image &)
void SetFileName(const PathType &fileName)
virtual void LoadPrivateTagsOn()
Set/Get loading private DICOM tags into Image's MetaData.
Read series of image files into a SimpleITK image.
static std::vector< std::string > GetGDCMSeriesIDs(const PathType &directory, bool useSeriesDetails=false)
Get all the seriesIDs from a DICOM data set.
void SetFileNames(const std::vector< PathType > &fileNames)
static std::vector< PathType > GetGDCMSeriesFileNames(const PathType &directory, const std::string &seriesID="", bool useSeriesDetails=false, bool recursive=false, bool loadSequences=false)
Generate a sequence of filenames from a directory with a DICOM data set and a series ID.
The Image class for SimpleITK.
unsigned int GetDepth() const
void SetMetaData(const std::string &key, const std::string &value)
Set an entry in the meta-data dictionary.
void SetSpacing(const std::vector< double > &spacing)
std::vector< double > GetSpacing() const
std::vector< double > GetDirection() const
Set/Get the Direction.
std::vector< double > TransformIndexToPhysicalPoint(const std::vector< int64_t > &index) const
PixelIDValueEnum
Enumerated values of pixelIDs.