We start with the imports:
import dicom
import os
import numpy
The
pydicom
package can be installed throughpip
and can be found in https://pypi.python.org/pypi/pydicom/
We start with the imports:
import dicom
import os
import numpy
The
pydicom
package can be installed throughpip
and can be found in https://pypi.python.org/pypi/pydicom/
We start by importing vtk
and SimpleITK
:
import vtk
import SimpleITK
We then create two dictionaries containing the matching built-in types:
The following Perl command, which should work directly in a UNIX/OSX bash, checks a file and prints any line with a non-ascii character
perl -ne 'print "$. $_" if m/[\x80-\xFF]/' file.txt
Here’s a little code snippet for running class methods as background threads in Python. The run() method does some work forever and in this use case you want it to do that in the background (until the main application dies), while the rest of the application continues it’s work.
import threading
import time
class ThreadingExample(object):
""" Threading example class
Using the platform
package one can retrieve a lot of information on the running system.
In order to retrieve a detailed string on the platform one can use:
import platform
platform.platform()
and will get a response like this:
Using the following terminal command one can retrieve the uptime in seconds:
sysctl kern.boottime
getting a response like this:
kern.boottime: { sec = 1362633455, usec = 0 } Wed Mar 6 21:17:35 2013
#source: http://stackoverflow.com/questions/20023209/python-function-for-rotating-2d-objects | |
import math | |
def rotatePolygon(polygon,theta): | |
"""Rotates the given polygon which consists of corners represented as (x,y), | |
around the ORIGIN, clock-wise, theta degrees""" | |
theta = math.radians(theta) | |
rotatedPolygon = [] | |
for corner in polygon : |
# source: | |
# http://stackoverflow.com/questions/5783517/downloading-progress-bar-urllib2-python | |
import urllib2, sys | |
def chunk_report(bytes_so_far, chunk_size, total_size): | |
percent = float(bytes_so_far) / total_size | |
percent = round(percent*100, 2) | |
sys.stdout.write("Downloaded %d of %d bytes (%0.2f%%)\r" % | |
(bytes_so_far, total_size, percent)) |
Assuming we have an .stl
file under filenameSTL
we can load the contents of that file using the vtkSTLReader
class as such:
readerSTL = vtk.vtkSTLReader()
readerSTL.SetFileName(filenameSTL)
# 'update' the reader i.e. read the .stl file
readerSTL.Update()
polydata = readerSTL.GetOutput()
Assuming we have a vtkImageData
object under the name of image
this is how we'd go about resampling that image data with a factor of 2
. Here we assume that image
has a spacing of 1.0
mm across all axes
resliceFilter = vtk.vtkImageReslice()
resliceFilter.SetInput(image)
resliceFilter.SetOutputSpacing(0.5, 0.5, 0.5)
resliceFilter.SetInterpolationModeToCubic()
resliceFilter.Update()
imageResampled = resliceFilter.GetOutput()