https://github.com/juanprietob/itk-image-pad-resample
Resample any image 2D, 3D, with 1 or multiple components and a wide variety of formats NIFTI, NRRD, DICOM
https://github.com/juanprietob/itk-image-pad-resample
emscripten image itk javascript medical-imaging nodejs resample
Last synced: about 1 month ago
JSON representation
Resample any image 2D, 3D, with 1 or multiple components and a wide variety of formats NIFTI, NRRD, DICOM
- Host: GitHub
- URL: https://github.com/juanprietob/itk-image-pad-resample
- Owner: juanprietob
- Created: 2019-12-30T05:19:26.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2022-03-25T23:31:45.000Z (about 4 years ago)
- Last Synced: 2025-07-03T08:43:00.077Z (11 months ago)
- Topics: emscripten, image, itk, javascript, medical-imaging, nodejs, resample
- Language: C++
- Size: 3.09 MB
- Stars: 2
- Watchers: 1
- Forks: 0
- Open Issues: 8
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# itk-image-pad-resample
> Resample virtually any type of image 2D, 3D with 1 or multiple components.
[](https://www.npmjs.com/package/itk-image-pad-resample) [](https://standardjs.com)
## Install
Install with the flag '-g' and use it in the command line.
```bash
npm install itk-image-pad-resample -g
```
## Usage in the command line
```bash
img-pad-resample --help
```
```
Help: Resample an image to a specific size.
Required:
--img or --dir
--size
Optional:
--out default: out.nrrd
--out_ext (when using --dir) default: .nrrd
--spacing Input image spacing is used. Otherwise, is set to fit the output size.
--pad pad output at the top
--iso_spacing If this is set, the spacing of the output image will be isometric, i.e., the same for all dimensions which means the max spacing value is selected and set for all dimensions.
--center_image If this is set, the output image is centered in the resampled space.
--linear Linear interpolation, default is nearest neighbor.
```
```bash
img-pad-resample --img /path/to/input.nii --size 250,250,250 --out temp.nrrd
```
To process a whole directory with images
```bash
img-pad-resample --dir /path/to/directory --size 250,250 --out /path/to/output/dir --out_ext .jpg
```
## Usage in your js logic
Use [med-img-reader](https://www.npmjs.com/package/med-img-reader) to read any type of image with one or multiple
components in a variety of formats.
```js
const MedImgReader = require('med-img-reader');
const ImgPadResampleLib = require('itk-image-pad-resample');
```
```js
const medimgreader = new MedImgReader();
medimgreader.SetFilename('/path/to/input{.png,.jpg,.nrrd,.nii.gz,.dcm}');
medimgreader.ReadImage();
const in_img = medimgreader.GetOutput();
const imgpad = new ImgPadResampleLib();
imgpad.SetImage(in_img);
imgpad.SetOutputSize([300, 250]);
imgpad.SetFitSpacingToOutputSizeOn(); //optional
imgpad.SetIsoSpacingOn(); //optional
imgpad.SetCenterImageOn(); //optional
imgpad.SetInterpolationTypeToLinear(); //default is nearest
imgpad.Update();
var img_out = imgpad.GetOutput();
const writer = new MedImgReader();
writer.SetInput(img_out);
writer.SetFilename('/path/to/ouput/{.png,.jpg,.nrrd,.nii.gz,.dcm}');
writer.WriteImage();
```
## Example
Input RGBA image:
")
```bash
img-pad-resample --img brain.png --size 500,250 --out out_brain.png
```
Output RGBA image, the image here might look stretched because typical image viewers will not take spacing information in consideration!:

Use flag '--iso_spacing' to have the same spacing in all dimensions
```bash
img-pad-resample --img brain.png --size 500,250 --iso_spacing --out out_brain_iso.png
```
Output RGBA image with equal spacing:

## Convert image to a tensorflow Tensor [tfjs](https://www.tensorflow.org/js)
---
const tf = require('@tensorflow/tfjs-node');//Or tfjs in browser or tfjs-node-gpu if in linux
tf.tensor(
Float32Array.from(img_out.data),
[1, ...[...img_out.size].reverse(), img_out.imageType.components]
));
---
## License
MIT © [juanprietob](https://github.com/juanprietob)