Source code for Py3DFreeHandUS.converters

# -*- coding: utf-8 -*-
"""
.. module:: converters
   :synopsis: helper module for conversion between data formats.

"""

[docs]def vti2arr(fileIn): """Convert voxel-array from VTI to Numpy 3D image array. Parameters ---------- fileIn : str Path for input VTI file. fileOut : str Path for output MAT file. Returns ------- V : np.ndarray Lx x Ly x Lz 3D array, representing the voxel-array. metadata : dict Dict containing metadata about voxel array: - 'spacing': array of voxel dimensions (pixel/mm) """ import numpy as np import vtk from vtk.util import numpy_support as nps reader = vtk.vtkXMLImageDataReader() reader.SetFileName(fileIn) reader.Update() vtkImageData = reader.GetOutput() dim = vtkImageData.GetDimensions() flatV = nps.vtk_to_numpy(vtkImageData.GetPointData().GetScalars()) V = flatV.reshape(dim[::-1]) #V = flatV.reshape(dim) metadata = {} spacing = np.array(vtkImageData.GetSpacing())[::-1] #spacing = np.array(vtkImageData.GetSpacing()) metadata['spacing'] = spacing return V, metadata
[docs]def vti2mat(fileIn, fileOut): """Convert voxel-array from VTI to MAT (MATLAB(R)) format. Parameters ---------- fileIn : str Path for input VTI file. fileOut : str Path for output MAT file. """ import numpy as np import vtk import scipy.io as sio from vtk.util import numpy_support as nps from math_utils import lcmm reader = vtk.vtkXMLImageDataReader() reader.SetFileName(fileIn) reader.Update() vtkImageData = reader.GetOutput() dim = vtkImageData.GetDimensions() flatV = nps.vtk_to_numpy(vtkImageData.GetPointData().GetScalars()) V = flatV.reshape(dim[::-1]) spacing = np.array(vtkImageData.GetSpacing())[::-1] estimatedFactors = lcmm(*spacing) / spacing estimatedVoxelSize = 1. / estimatedFactors sio.savemat(fileOut, {'volume':V, 'spacing': spacing, 'estimated_voxel_size': estimatedVoxelSize})
[docs]def avi2dcm(fileIn, fileOut): """Convert gray-scale AVI file to gray-scale image-sequence DICOM file. Frame rate is *not* added as *CineRate* tag in the DICOM file. Parameters ---------- fileIn : str Path for input AVI file. fileOut : str Path for output DICOM file. """ import numpy as np import cv2 import SimpleITK as sitk cap = cv2.VideoCapture(fileIn) Nf = int(cap.get(7)) w = cap.get(3) h = cap.get(4) fps = cap.get(5) I = np.zeros((Nf,h,w), dtype=np.uint8) #while(cap.isOpened()): for i in xrange(Nf): print 'Converting frame %d ...' % i ret, frame = cap.read() gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) I[i,:,:] = gray cap.release() arr2dcm(I, fileOut)
[docs]def arr2dcm(I, fileOut): """Convert gray-scale Numpy 3D image array to gray-scale image-sequence DICOM file. Parameters ---------- I : np.ndarray F x H x W 3D array, representing a sequence of F images, each H x W. fileOut : str Path for output DICOM file. """ import SimpleITK as sitk image = sitk.GetImageFromArray(I) writer = sitk.ImageFileWriter() writer.SetFileName(fileOut) writer.Execute(image)
[docs]def arr2aviOCV(fileName, M, fps): """Convert gray-scale Numpy 3D image array to AVI file (use OpenCV). Parameters ---------- fileName : str Path for output AVI file. M : np.ndarray(uint8) F x H x W 3D array, representing a sequence of F images, each H x W. fps : int frame rate for the output file. """ import numpy as np import cv2 # Define the codec and create VideoWriter object #fourcc = cv2.cv.CV_FOURCC(*'XVID') #fourcc = cv2.cv.CV_FOURCC(*'DIVX') fourcc = cv2.cv.CV_FOURCC(*'msvc') # it seems the only one working #fourcc = -1 # choose codec manually try: out = cv2.VideoWriter(fileName,fourcc, fps, M.shape[::-1][:-1]) for frame in M: #frame = cv2.flip(frame,0) # write the flipped frame out.write(frame) except: out.release() # Release everything if job is finished out.release()
[docs]def arr2aviMPY(fileName, M, fps): """Convert gray-scale Numpy 3D image array to AVI file (use moviepy). Parameters ---------- fileName : str Path for output AVI file. M : np.ndarray(uint8) F x H x W 3D array, representing a sequence of F images, each H x W. fps : int frame rate for the output file. """ import numpy as np from moviepy.editor import ImageSequenceClip D = [np.dstack([m] * 3) for m in M] clip = ImageSequenceClip(D, fps=fps) clip.write_videofile(fileName, codec='mpeg4', ffmpeg_params=['-vb','1M'])
[docs]def arr2seqFile(fileName, M, fps): """Convert gray-scale Numpy 3D image array to image sequence file. Parameters ---------- fileName : str Path for output file. Possible extensions: - 'avi': ``arr2aviMPY()`` is called. M : np.ndarray(uint8) F x H x W 3D array, representing a sequence of F images, each H x W. fps : int frame rate for the output file. """ from image_utils import getFileExt # Save to file ext = getFileExt(fileName) if ext == 'avi': print 'Saving file %s ...' % fileName arr2aviMPY(fileName, M, fps) print 'File saved'