def load_itk(filename): """ This function reads a '.mhd' file using SimpleITK and return the image array, origin and spacing of the image. """ # Reads the image using SimpleITK itk_image = ReadImage(filename) # Convert the image to a numpy array first and then # shuffle the dimensions to get axis in the order z,y,x ct_scan = GetArrayFromImage(itk_image) # Read the origin of the ct_scan, will be used to convert # the coordinates from world to voxel and vice versa. origin = np.array(list(reversed(itk_image.GetOrigin()))) # Read the spacing along each dimension spacing = np.array(list(reversed(itk_image.GetSpacing()))) return ct_scan, origin, spacing
def openfiles(self): if self.file_path == "": return if self.name == "": if "T2" in self.file_path.split("/")[-1]: self.name = "T2" if "LGE" in self.file_path.split("/")[-1]: self.name = "LGE" if "C0" in self.file_path.split("/")[-1]: self.name = "C0" if self.name == "": self.name = "C0" itk_img = ReadImage(self.file_path) img = GetArrayFromImage(itk_img) self.spacing = itk_img.GetSpacing() self.direction = itk_img.GetDirection() # print("img:", self.file_path, "direction:", self.direction) self.origin = itk_img.GetOrigin() minDim = list(img.shape).index(min(img.shape)) if minDim == 0: self.img = np.zeros((img.shape[1], img.shape[2], min(img.shape))) for i in range(min(img.shape)): self.img[:, :, i] = self.showRoate(img[i, :, :]) if minDim == 1: self.img = np.zeros((img.shape[0], img.shape[2], min(img.shape))) for i in range(min(img.shape)): self.img[:, :, i] = img[:, i, :] if minDim == 2: self.img = img self.imgDim = self.img.shape[2] if self.imgDim >= 3: self.imgIndex = int(self.imgDim / 2 + 1) else: self.imgIndex = int(self.imgDim / 2) self.adjusted = False self.predicted = False self.isOpen = True
def resample(sel, old_image_path, datatype='series'): """ Usage: resample(sel, old_image_path) Resample a 3D old_image to given new spacing The new voxel spacing will determine the new old_image dimensions. interpolation选项 所用的插值方法 INTER_NEAREST 最近邻插值 INTER_LINEAR 双线性插值(默认设置) INTER_AREA 使用像素区域关系进行重采样。 它可能是图像抽取的首选方法,因为它会产生无云纹理的结果。 但是当图像缩放时,它类似于INTER_NEAREST方法。 INTER_CUBIC 4x4像素邻域的双三次插值 INTER_LANCZOS4 8x8像素邻域的Lanczos插值 """ # read dicom series if datatype == 'series': reader = ImageSeriesReader() dicom_names = reader.GetGDCMSeriesFileNames(old_image_path) reader.SetFileNames(dicom_names) reader.MetaDataDictionaryArrayUpdateOn() reader.LoadPrivateTagsOn() series_ids = reader.GetGDCMSeriesIDs( old_image_path) # get all series id series_file_names = reader.GetGDCMSeriesFileNames( old_image_path, series_ids[0]) # get the first series reader.SetFileNames(series_file_names) old_image = reader.Execute() # type: sitk.Image elif datatype == 'nii': # read nifiti file old_image = ReadImage(old_image_path) else: print(f'Datatype {datatype} is wrong!\n') # get old information and new information old_spacing = old_image.GetSpacing() size = old_image.GetSize() new_size = (np.round( size * (old_spacing / sel._new_spacing))).astype(int).tolist() # EXE # If is orginal data ('series'), use sitk.sitkLinear. # If is binary mask ('nii'), usse sitk.sitkNearestNeighbor. # TODO: other methods; # FIXME: Some cases the 'series' may not indicate the orginal data # FIXME:Some cases the 'nii' may not indicate the binary mask if datatype == 'series': resampled_img = sitk.Resample(old_image, new_size, sitk.Transform(), sitk.sitkLinear, old_image.GetOrigin(), sel._new_spacing, old_image.GetDirection(), 0.0, old_image.GetPixelID()) elif datatype == 'nii': resampled_img = sitk.Resample(old_image, new_size, sitk.Transform(), sitk.sitkNearestNeighbor, old_image.GetOrigin(), sel._new_spacing, old_image.GetDirection(), 0.0, old_image.GetPixelID()) # resampled_img.GetSpacing() # resampled_img.GetSize() return resampled_img
def openfiles(self): try: imgName, imgType = QFileDialog.getOpenFileName( self, "", os.getcwd(), filter= "nii.gz Files (*.nii.gz);;mha Files (*.mha);;nii Files (*.nii);;All Files (*)" ) if imgName == "": return if "T2" in imgName: self.name = "T2" self.classItems.setCurrentIndex(1) if "LGE" in imgName: self.name = "LGE" self.classItems.setCurrentIndex(2) if "C0" in imgName: self.name = "C0" self.classItems.setCurrentIndex(0) self.OpenPath = imgName itk_img = ReadImage(imgName) img = GetArrayFromImage(itk_img) self.spacing = itk_img.GetSpacing() self.direction = itk_img.GetDirection() print("img:", self.openPath, "direction:", self.direction) self.origin = itk_img.GetOrigin() minDim = list(img.shape).index(min(img.shape)) print(minDim) if minDim == 0: self.img = np.zeros( (img.shape[1], img.shape[2], min(img.shape))) for i in range(min(img.shape)): self.img[:, :, i] = self.adjustRoate(img[i, :, :]) if minDim == 1: self.img = np.zeros( (img.shape[0], img.shape[2], min(img.shape))) for i in range(min(img.shape)): self.img[:, :, i] = img[:, i, :] if minDim == 2: self.img = img self.imgDim = self.img.shape[2] if self.imgDim >= 3: self.imgIndex = int(self.imgDim / 2 + 1) else: self.imgIndex = int(self.imgDim / 2) plt.cla() self.fig, self.ax = plt.subplots() self.ax.axis('off') self.ax.imshow(self.showRoate(self.img[:, :, self.imgIndex]), interpolation='nearest', aspect='auto', cmap='gray') cavan = FigureCanvas(self.fig) for i in reversed(range(self.verticalLayout.count())): self.verticalLayout.itemAt(i).widget().setParent(None) self.verticalLayout.addWidget(cavan) self.tableWidget.insertRow(0) item = QTableWidgetItem(str(self.img.shape[0])) item.setTextAlignment(Qt.AlignVCenter | Qt.AlignHCenter) self.tableWidget.setItem(0, 0, item) item = QTableWidgetItem(str(self.img.shape[1])) item.setTextAlignment(Qt.AlignVCenter | Qt.AlignHCenter) self.tableWidget.setItem(0, 1, item) item = QTableWidgetItem(str(self.img.shape[2])) item.setTextAlignment(Qt.AlignVCenter | Qt.AlignHCenter) self.tableWidget.setItem(0, 2, item) if not self.isOpen: self.LPLabel.setText("L/P") self.RALabel.setText("R/A") self.ILabel.setText("I") self.SLabel.setText("S") self.nameLabel.setText(imgName.split("/")[-1]) self.totalCount.setText("/" + str(self.imgDim)) self.lineEdit.setText(str(self.imgIndex + 1)) self.adjusted = False self.predicted = False self.isOpen = True except: pass