npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

med-img-reader

v1.3.0

Published

Reads any type of medical image in the browser.

Downloads

37

Readme

med-img-reader

Read or write a 3D or 2D image with one or multiple components in a variety of formats. DICOM, NIFTI, NRRD, JPG, PNG, MHD Viewer example

Installation


npm install med-img-reader

Usage example

In NODE environment

Read a single image file


const MedImgReader = require('med-img-reader');

//Image with one or multiple components in any format
var inputImage = '/path/to/input{.nrrd,.nii.gz,.jpg,.png,.dcm}'
var outputImage = '/path/to/output{.nrrd,.nii.gz,.jpg,.png,.dcm}'


const medImgReader = new MedImgReader();
medImgReader.SetFilename(inputImage);
medImgReader.ReadImage();
var image = medImgReader.GetOutput();
console.log("Image:", image);


const medImgWriter = new MedImgReader();

medImgWriter.SetInput(image);
medImgWriter.SetFilename(outputImage);
medImgWriter.WriteImage();

This is an example of an object return by the reader, which is compatible with itk.js


image = {
    imageType: {
      dimension: 2,
      componentType: 'uint16_t',
      pixelType: 1,
      components: 1
    },
    name: 'Image',
    origin: [ 0, 0 ],
    spacing: [ 0.148489, 0.148489 ],
    direction: { rows: 2, columns: 2, data: [ 1, 0, 0, 1 ] },
    size: [ 256, 256 ],
    data: Uint16Array [...]
}

Convert the image to a Tensor from tensorflow tfjs


const tf = require('@tensorflow/tfjs-node');//Or tfjs in browser or tfjs-node-gpu if in linux

tf.tensor(
	Float32Array.from(image.data), 
	[...[...image.size].reverse(), image.imageType.components]
));

Read a DICOM series


var inputDirectory = '/path/to/series/directory'
var outputImage = 'out.nrrd';

const medImgReader = new MedImgReader();
medImgReader.SetDirectory(inputDirectory);
medImgReader.ReadDICOMDirectory();

var image = medImgReader.GetOutput();

const medImgWriter = new MedImgReader();
medImgWriter.SetInput(inputImage);
medImgWriter.SetFilename(outputImage);
medImgWriter.WriteImage();

In browser environment

React component

If you are going to use this in the browser, the build time will be long so be patient. This library is compiled using emscripten and it bundles a file system with the FS library.

Here is an example for a React component:


import React, { Component } from 'react'

const axios = require('axios');
const MedImgReader = require('med-img-reader');

export default class ExampleComponent extends Component {

  constructor(){
    super();

    this.state = {
      itkImage: {}
    }

    const self = this;
    var medImgReader = new MedImgReader();

    var filename = '/brain.png';

    axios({
      method: 'get',
      url: filename,
      responseType: 'blob'
    })
    .then(function(brain){
      var blob = brain.data;
      return blob.arrayBuffer()
      .then((arr)=>{
        medImgReader.WriteFile(filename, arr);//We add the file to the FS filesystem
        medImgReader.SetFilename(filename);//Set the file name 
        medImgReader.ReadImage();
        return medImgReader.GetOutput();
      })
      
    })
    .then((itkImage)=>{
      self.setState({...self.state, itkImage})
    })
  }

  render() {

    const {
      itkImage
    } = this.state;

    var copyImg = {...itkImage, data: []};

    return (
      <div className={styles.test}>
        {JSON.stringify(copyImg)}
      </div>
    )
  }
}

Display the image using vtk.js

Example is here react-med-img-viewer