Spaces:
Runtime error
Runtime error
File size: 6,731 Bytes
dde56f0 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 |
{
"cells": [
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [],
"source": [
"# David Ouyang 10/2/2019\n",
"\n",
"# Notebook which iterates through a folder, including subfolders, \n",
"# and convert DICOM files to AVI files of a defined size (natively 112 x 112)\n",
"\n",
"import re\n",
"import os, os.path\n",
"from os.path import splitext\n",
"import pydicom as dicom\n",
"import numpy as np\n",
"from pydicom.uid import UID, generate_uid\n",
"import shutil\n",
"from multiprocessing import dummy as multiprocessing\n",
"import time\n",
"import subprocess\n",
"import datetime\n",
"from datetime import date\n",
"import sys\n",
"import cv2\n",
"#from scipy.misc import imread\n",
"import matplotlib.pyplot as plt\n",
"import sys\n",
"from shutil import copy\n",
"import math\n",
"\n",
"destinationFolder = \"Output Folder Name\"\n"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Requirement already satisfied: pillow in c:\\programdata\\anaconda3\\lib\\site-packages (6.2.0)\n",
"Requirement already satisfied: scipy in c:\\programdata\\anaconda3\\lib\\site-packages (1.3.1)\n"
]
}
],
"source": [
"# Dependencies you might need to run code\n",
"# Commonly missing\n",
"\n",
"#!pip install pydicom\n",
"#!pip install opencv-python\n"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"def mask(output):\n",
" dimension = output.shape[0]\n",
" \n",
" # Mask pixels outside of scanning sector\n",
" m1, m2 = np.meshgrid(np.arange(dimension), np.arange(dimension))\n",
" \n",
"\n",
" mask = ((m1+m2)>int(dimension/2) + int(dimension/10)) \n",
" mask *= ((m1-m2)<int(dimension/2) + int(dimension/10))\n",
" mask = np.reshape(mask, (dimension, dimension)).astype(np.int8)\n",
" maskedImage = cv2.bitwise_and(output, output, mask = mask)\n",
" \n",
" #print(maskedImage.shape)\n",
" \n",
" return maskedImage\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"def makeVideo(fileToProcess, destinationFolder):\n",
" try:\n",
" fileName = fileToProcess.split('\\\\')[-1] #\\\\ if windows, / if on mac or sherlock\n",
" #hex(abs(hash(fileToProcess.split('/')[-1]))).upper()\n",
"\n",
" if not os.path.isdir(os.path.join(destinationFolder,fileName)):\n",
"\n",
" dataset = dicom.dcmread(fileToProcess, force=True)\n",
" testarray = dataset.pixel_array\n",
"\n",
" frame0 = testarray[0]\n",
" mean = np.mean(frame0, axis=1)\n",
" mean = np.mean(mean, axis=1)\n",
" yCrop = np.where(mean<1)[0][0]\n",
" testarray = testarray[:, yCrop:, :, :]\n",
"\n",
" bias = int(np.abs(testarray.shape[2] - testarray.shape[1])/2)\n",
" if bias>0:\n",
" if testarray.shape[1] < testarray.shape[2]:\n",
" testarray = testarray[:, :, bias:-bias, :]\n",
" else:\n",
" testarray = testarray[:, bias:-bias, :, :]\n",
"\n",
"\n",
" print(testarray.shape)\n",
" frames,height,width,channels = testarray.shape\n",
"\n",
" fps = 30\n",
"\n",
" try:\n",
" fps = dataset[(0x18, 0x40)].value\n",
" except:\n",
" print(\"couldn't find frame rate, default to 30\")\n",
"\n",
" fourcc = cv2.VideoWriter_fourcc('M','J','P','G')\n",
" video_filename = os.path.join(destinationFolder, fileName + '.avi')\n",
" out = cv2.VideoWriter(video_filename, fourcc, fps, cropSize)\n",
"\n",
"\n",
" for i in range(frames):\n",
"\n",
" outputA = testarray[i,:,:,0]\n",
" smallOutput = outputA[int(height/10):(height - int(height/10)), int(height/10):(height - int(height/10))]\n",
"\n",
" # Resize image\n",
" output = cv2.resize(smallOutput, cropSize, interpolation = cv2.INTER_CUBIC)\n",
"\n",
" finaloutput = mask(output)\n",
"\n",
"\n",
" finaloutput = cv2.merge([finaloutput,finaloutput,finaloutput])\n",
" out.write(finaloutput)\n",
"\n",
" out.release()\n",
"\n",
" else:\n",
" print(fileName,\"hasAlreadyBeenProcessed\")\n",
" except:\n",
" print(\"something filed, not sure what, have to debug\", fileName)\n",
" return 0"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"AllA4cNames = \"Input Folder Name\"\n",
"\n",
"count = 0\n",
" \n",
"cropSize = (112,112)\n",
"subfolders = os.listdir(AllA4cNames)\n",
"\n",
"\n",
"for folder in subfolders:\n",
" print(folder)\n",
"\n",
" for content in os.listdir(os.path.join(AllA4cNames, folder)):\n",
" for subcontent in os.listdir(os.path.join(AllA4cNames, folder, content)):\n",
" count += 1\n",
" \n",
"\n",
" VideoPath = os.path.join(AllA4cNames, folder, content, subcontent)\n",
"\n",
" print(count, folder, content, subcontent)\n",
"\n",
" if not os.path.exists(os.path.join(destinationFolder,subcontent + \".avi\")):\n",
" makeVideo(VideoPath, destinationFolder)\n",
" else:\n",
" print(\"Already did this file\", VideoPath)\n",
"\n",
"\n",
"print(len(AllA4cFilenames))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.4"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
|