or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

Files

docs

core-data-structures.mdimage-processing.mdindex.mdio-operations.mdmathematical-operations.mdmesh-processing.mdregistration.mdsegmentation.mdspatial-objects.md

io-operations.mddocs/

0

# Input/Output Operations

1

2

ITK provides comprehensive support for reading and writing medical and standard image formats through a flexible IO framework. The system uses format-specific ImageIO classes behind generic reader/writer interfaces, supporting formats from DICOM to standard image types.

3

4

## File Readers

5

6

### ImageFileReader

7

8

Generic image file reader that automatically detects file format.

9

10

```cpp { .api }

11

template<typename TOutputImage>

12

class ImageFileReader : public ImageSource<TOutputImage>

13

{

14

public:

15

typedef ImageFileReader Self;

16

typedef ImageSource<TOutputImage> Superclass;

17

typedef SmartPointer<Self> Pointer;

18

typedef SmartPointer<const Self> ConstPointer;

19

20

// Object creation

21

static Pointer New();

22

23

// File specification

24

itkSetStringMacro(FileName);

25

itkGetStringMacro(FileName);

26

27

// Format control

28

void SetImageIO(ImageIOBase* imageIO);

29

itkGetObjectMacro(ImageIO, ImageIOBase);

30

31

// Pipeline execution

32

virtual void GenerateOutputInformation() ITK_OVERRIDE;

33

virtual void GenerateData() ITK_OVERRIDE;

34

35

// Convenience methods

36

virtual void Update() ITK_OVERRIDE;

37

const TOutputImage * GetOutput() const;

38

TOutputImage * GetOutput();

39

};

40

```

41

42

### ImageSeriesReader

43

44

Reads a series of 2D images as a single 3D volume.

45

46

```cpp { .api }

47

template<typename TOutputImage>

48

class ImageSeriesReader : public ImageSource<TOutputImage>

49

{

50

public:

51

typedef ImageSeriesReader Self;

52

typedef ImageSource<TOutputImage> Superclass;

53

typedef SmartPointer<Self> Pointer;

54

typedef SmartPointer<const Self> ConstPointer;

55

56

// File series specification

57

void SetFileNames(const FileNamesContainer & names);

58

const FileNamesContainer & GetFileNames() const;

59

void AddFileName(const std::string & name);

60

61

// DICOM-specific methods

62

void SetImageIO(ImageIOBase* imageIO);

63

void UseStreamingOn();

64

void UseStreamingOff();

65

66

// Metadata access

67

void ForceOrthogonalDirectionOff();

68

void ForceOrthogonalDirectionOn();

69

};

70

```

71

72

## File Writers

73

74

### ImageFileWriter

75

76

Generic image file writer with automatic format detection.

77

78

```cpp { .api }

79

template<typename TInputImage>

80

class ImageFileWriter : public ProcessObject

81

{

82

public:

83

typedef ImageFileWriter Self;

84

typedef ProcessObject Superclass;

85

typedef SmartPointer<Self> Pointer;

86

typedef SmartPointer<const Self> ConstPointer;

87

88

// Object creation

89

static Pointer New();

90

91

// Input specification

92

using Superclass::SetInput;

93

void SetInput(const TInputImage * image);

94

const TInputImage * GetInput();

95

const TInputImage * GetInput(unsigned int idx);

96

97

// File specification

98

itkSetStringMacro(FileName);

99

itkGetStringMacro(FileName);

100

101

// Format control

102

void SetImageIO(ImageIOBase* io);

103

itkGetObjectMacro(ImageIO, ImageIOBase);

104

105

// Compression control

106

void SetUseCompression(bool UseCompression);

107

void UseCompressionOn();

108

void UseCompressionOff();

109

itkGetConstMacro(UseCompression, bool);

110

111

// Writing operations

112

virtual void Write();

113

virtual void Update() ITK_OVERRIDE;

114

115

protected:

116

virtual void GenerateData() ITK_OVERRIDE;

117

};

118

```

119

120

### ImageSeriesWriter

121

122

Writes a 3D image as a series of 2D slices.

123

124

```cpp { .api }

125

template<typename TInputImage, typename TOutputImage>

126

class ImageSeriesWriter : public ProcessObject

127

{

128

public:

129

typedef ImageSeriesWriter Self;

130

typedef ProcessObject Superclass;

131

typedef SmartPointer<Self> Pointer;

132

typedef SmartPointer<const Self> ConstPointer;

133

134

// Input specification

135

void SetInput(const TInputImage * input);

136

const TInputImage * GetInput();

137

138

// Output file names

139

void SetFileNames(const FileNamesContainer & names);

140

const FileNamesContainer & GetFileNames() const;

141

142

// Format control

143

void SetImageIO(ImageIOBase* io);

144

void SetMetaDataDictionaryArray(const DictionaryArrayType * dictArray);

145

146

// Writing operations

147

virtual void Write();

148

virtual void Update() ITK_OVERRIDE;

149

150

// Compression

151

void SetUseCompression(bool UseCompression);

152

void UseCompressionOn();

153

void UseCompressionOff();

154

};

155

```

156

157

## ImageIO Base Classes

158

159

### ImageIOBase

160

161

Abstract base class for format-specific IO implementations.

162

163

```cpp { .api }

164

class ImageIOBase : public LightProcessObject

165

{

166

public:

167

typedef ImageIOBase Self;

168

typedef LightProcessObject Superclass;

169

typedef SmartPointer<Self> Pointer;

170

typedef SmartPointer<const Self> ConstPointer;

171

172

// Format detection

173

virtual bool CanReadFile(const char* filename) = 0;

174

virtual bool CanWriteFile(const char* filename) = 0;

175

176

// File information

177

virtual void ReadImageInformation() = 0;

178

virtual void WriteImageInformation() = 0;

179

180

// Data reading/writing

181

virtual void Read(void* buffer) = 0;

182

virtual void Write(const void* buffer) = 0;

183

184

// Image properties

185

void SetNumberOfDimensions(unsigned int dimensions);

186

unsigned int GetNumberOfDimensions() const;

187

188

void SetDimensions(unsigned int i, unsigned int dim);

189

unsigned int GetDimensions(unsigned int i) const;

190

191

void SetOrigin(unsigned int i, double origin);

192

double GetOrigin(unsigned int i) const;

193

194

void SetSpacing(unsigned int i, double spacing);

195

double GetSpacing(unsigned int i) const;

196

197

// Pixel type information

198

void SetPixelType(const IOPixelType pixelType);

199

IOPixelType GetPixelType() const;

200

201

void SetComponentType(const IOComponentType componentType);

202

IOComponentType GetComponentType() const;

203

204

void SetNumberOfComponents(unsigned int components);

205

unsigned int GetNumberOfComponents() const;

206

207

// File name handling

208

itkSetStringMacro(FileName);

209

itkGetStringMacro(FileName);

210

};

211

```

212

213

## Format-Specific IO Classes

214

215

### DICOM Support

216

217

```cpp { .api }

218

// Modern DICOM image IO (preferred)

219

class GDCMImageIO : public ImageIOBase

220

{

221

public:

222

typedef GDCMImageIO Self;

223

typedef ImageIOBase Superclass;

224

typedef SmartPointer<Self> Pointer;

225

226

static Pointer New();

227

228

// DICOM-specific methods

229

bool CanReadFile(const char* filename) override;

230

void ReadImageInformation() override;

231

void Read(void* buffer) override;

232

bool CanWriteFile(const char* filename) override;

233

void WriteImageInformation() override;

234

void Write(const void* buffer) override;

235

};

236

237

// Legacy DICOM image IO (deprecated - use GDCMImageIO instead)

238

class DICOMImageIO2 : public ImageIOBase

239

{

240

public:

241

typedef DICOMImageIO2 Self;

242

typedef ImageIOBase Superclass;

243

typedef SmartPointer<Self> Pointer;

244

245

static Pointer New();

246

247

// DICOM-specific methods

248

virtual bool CanReadFile(const char* filename) ITK_OVERRIDE;

249

virtual void ReadImageInformation() ITK_OVERRIDE;

250

virtual void Read(void* buffer) ITK_OVERRIDE;

251

252

// Metadata access

253

void GetPatientName(std::string & name);

254

void GetPatientID(std::string & id);

255

void GetStudyID(std::string & id);

256

void GetSeriesID(std::string & id);

257

};

258

259

// DICOM series file names generator

260

class DICOMSeriesFileNames : public Object

261

{

262

public:

263

typedef DICOMSeriesFileNames Self;

264

typedef Object Superclass;

265

typedef SmartPointer<Self> Pointer;

266

267

static Pointer New();

268

269

// Directory scanning

270

void SetDirectory(const std::string & directory);

271

const std::string & GetDirectory() const;

272

273

// Series discovery

274

const SeriesUIDContainer & GetSeriesUIDs();

275

const FileNamesContainer & GetFileNames(const std::string & seriesUID);

276

277

// Ordering control

278

void SetUseSeriesDetails(bool useSeriesDetails);

279

void AddSeriesRestriction(const std::string & tag);

280

};

281

```

282

283

### Medical Image Formats

284

285

```cpp { .api }

286

// NIFTI format support

287

class NiftiImageIO : public ImageIOBase

288

{

289

public:

290

typedef NiftiImageIO Self;

291

typedef ImageIOBase Superclass;

292

typedef SmartPointer<Self> Pointer;

293

294

static Pointer New();

295

296

virtual bool CanReadFile(const char* filename) ITK_OVERRIDE;

297

virtual bool CanWriteFile(const char* filename) ITK_OVERRIDE;

298

virtual void ReadImageInformation() ITK_OVERRIDE;

299

virtual void WriteImageInformation() ITK_OVERRIDE;

300

virtual void Read(void* buffer) ITK_OVERRIDE;

301

virtual void Write(const void* buffer) ITK_OVERRIDE;

302

};

303

304

// Analyze format support

305

class AnalyzeImageIO : public ImageIOBase

306

{

307

public:

308

typedef AnalyzeImageIO Self;

309

typedef ImageIOBase Superclass;

310

typedef SmartPointer<Self> Pointer;

311

312

static Pointer New();

313

314

virtual bool CanReadFile(const char* filename) ITK_OVERRIDE;

315

virtual bool CanWriteFile(const char* filename) ITK_OVERRIDE;

316

virtual void ReadImageInformation() ITK_OVERRIDE;

317

virtual void WriteImageInformation() ITK_OVERRIDE;

318

virtual void Read(void* buffer) ITK_OVERRIDE;

319

virtual void Write(const void* buffer) ITK_OVERRIDE;

320

};

321

322

// MetaImage format support

323

class MetaImageIO : public ImageIOBase

324

{

325

public:

326

typedef MetaImageIO Self;

327

typedef ImageIOBase Superclass;

328

typedef SmartPointer<Self> Pointer;

329

330

static Pointer New();

331

332

virtual bool CanReadFile(const char* filename) ITK_OVERRIDE;

333

virtual bool CanWriteFile(const char* filename) ITK_OVERRIDE;

334

virtual void ReadImageInformation() ITK_OVERRIDE;

335

virtual void WriteImageInformation() ITK_OVERRIDE;

336

virtual void Read(void* buffer) ITK_OVERRIDE;

337

virtual void Write(const void* buffer) ITK_OVERRIDE;

338

339

// MetaImage-specific

340

void SetDataByteOrderToBigEndian();

341

void SetDataByteOrderToLittleEndian();

342

};

343

```

344

345

### Standard Image Formats

346

347

```cpp { .api }

348

// JPEG support

349

class JPEGImageIO : public ImageIOBase

350

{

351

public:

352

typedef JPEGImageIO Self;

353

typedef ImageIOBase Superclass;

354

typedef SmartPointer<Self> Pointer;

355

356

static Pointer New();

357

358

// JPEG quality control

359

void SetQuality(int quality);

360

int GetQuality() const;

361

362

// Progressive JPEG

363

void SetProgressive(bool progressive);

364

bool GetProgressive() const;

365

};

366

367

// PNG support

368

class PNGImageIO : public ImageIOBase

369

{

370

public:

371

typedef PNGImageIO Self;

372

typedef ImageIOBase Superclass;

373

typedef SmartPointer<Self> Pointer;

374

375

static Pointer New();

376

377

// PNG-specific options

378

void SetCompressionLevel(int level);

379

int GetCompressionLevel() const;

380

};

381

382

// TIFF support

383

class TIFFImageIO : public ImageIOBase

384

{

385

public:

386

typedef TIFFImageIO Self;

387

typedef ImageIOBase Superclass;

388

typedef SmartPointer<Self> Pointer;

389

390

static Pointer New();

391

392

// TIFF compression

393

void SetCompressionToNoCompression();

394

void SetCompressionToPackBits();

395

void SetCompressionToJPEG();

396

void SetCompressionToDeflate();

397

};

398

```

399

400

## Factory System

401

402

### ImageIOFactory

403

404

Manages automatic format detection and IO object creation.

405

406

```cpp { .api }

407

class ImageIOFactory : public Object

408

{

409

public:

410

typedef ImageIOFactory Self;

411

typedef Object Superclass;

412

typedef SmartPointer<Self> Pointer;

413

414

// Factory registration

415

static void RegisterBuiltInFactories();

416

417

// IO object creation

418

static ImageIOBase::Pointer CreateImageIO(const char* path, FileModeType mode);

419

420

// Factory management

421

static void RegisterFactory(ObjectFactoryBase* factory);

422

static void UnRegisterFactory(ObjectFactoryBase* factory);

423

};

424

```

425

426

## Common Usage Patterns

427

428

### Basic File Reading

429

430

```cpp

431

// Define image type

432

typedef itk::Image<short, 3> ImageType;

433

434

// Create reader

435

typedef itk::ImageFileReader<ImageType> ReaderType;

436

ReaderType::Pointer reader = ReaderType::New();

437

438

// Set filename and read

439

reader->SetFileName("brain.nii.gz");

440

try {

441

reader->Update();

442

ImageType::Pointer image = reader->GetOutput();

443

} catch (itk::ExceptionObject & error) {

444

std::cerr << "Reading failed: " << error << std::endl;

445

}

446

```

447

448

### DICOM Series Reading

449

450

```cpp

451

// Setup DICOM names generator

452

typedef itk::DICOMSeriesFileNames NamesGeneratorType;

453

NamesGeneratorType::Pointer nameGenerator = NamesGeneratorType::New();

454

nameGenerator->SetDirectory("/path/to/dicom/series");

455

456

// Get series UIDs

457

const SeriesIdContainer & seriesUID = nameGenerator->GetSeriesUIDs();

458

std::string seriesIdentifier = seriesUID.begin()->c_str();

459

460

// Get file names for series

461

typedef std::vector<std::string> FileNamesContainer;

462

FileNamesContainer fileNames = nameGenerator->GetFileNames(seriesIdentifier);

463

464

// Create series reader

465

typedef itk::Image<short, 3> ImageType;

466

typedef itk::ImageSeriesReader<ImageType> ReaderType;

467

ReaderType::Pointer reader = ReaderType::New();

468

469

// Configure DICOM IO (using preferred GDCMImageIO)

470

typedef itk::GDCMImageIO ImageIOType;

471

ImageIOType::Pointer dicomIO = ImageIOType::New();

472

reader->SetImageIO(dicomIO);

473

reader->SetFileNames(fileNames);

474

475

// Read series

476

reader->Update();

477

ImageType::Pointer image = reader->GetOutput();

478

```

479

480

### File Writing with Compression

481

482

```cpp

483

// Create writer

484

typedef itk::Image<short, 3> ImageType;

485

typedef itk::ImageFileWriter<ImageType> WriterType;

486

WriterType::Pointer writer = WriterType::New();

487

488

// Configure output

489

writer->SetFileName("output.nii.gz");

490

writer->SetInput(processedImage);

491

writer->UseCompressionOn();

492

493

// Write file

494

try {

495

writer->Update();

496

} catch (itk::ExceptionObject & error) {

497

std::cerr << "Writing failed: " << error << std::endl;

498

}

499

```

500

501

## Type Definitions

502

503

```cpp { .api }

504

// IO enums

505

enum IOPixelType {

506

UNKNOWNPIXELTYPE,

507

SCALAR,

508

RGB,

509

RGBA,

510

OFFSET,

511

VECTOR,

512

POINT,

513

COVARIANTVECTOR,

514

SYMMETRICSECONDRANKTENSOR,

515

DIFFUSIONTENSOR3D,

516

COMPLEX,

517

FIXEDARRAY,

518

MATRIX

519

};

520

521

enum IOComponentType {

522

UNKNOWNCOMPONENTTYPE,

523

UCHAR,

524

CHAR,

525

USHORT,

526

SHORT,

527

UINT,

528

INT,

529

ULONG,

530

LONG,

531

FLOAT,

532

DOUBLE

533

};

534

535

enum FileModeType {

536

ReadMode,

537

WriteMode

538

};

539

540

// Container types

541

typedef std::vector<std::string> FileNamesContainer;

542

typedef std::vector<std::string> SeriesIdContainer;

543

typedef std::vector<MetaDataDictionary *> DictionaryArrayType;

544

```

545

546

ITK's IO framework provides robust, extensible support for medical and standard image formats, enabling seamless integration with medical imaging workflows and research pipelines.