def BrowseFolder(self): path, _ = QFileDialog.getOpenFileName( self, "open", "/home/", "All Files (*);; Image Files (*.png *.tif *.jpg *.ser *.dm3)") self.imagePath_content = self.imagePath_content if not path else path if self.imagePath_content: self.imagePath.setText(self.imagePath_content) file_name = os.path.basename(self.imagePath_content) _, suffix = os.path.splitext(file_name) # file_name = self.imagePath_content.split('/')[-1] # suffix = '.' + file_name.split('.')[-1] if suffix == '.ser': from file_readers.ser_lib.serReader import serReader ser_data = serReader(self.imagePath_content) ser_array = np.array(ser_data['imageData'], dtype='float64') self.imarray_original = ser_array ser_array = (map01(ser_array) * 255).astype('uint8') self.ori_image = Image.fromarray(ser_array, 'L') elif suffix == '.dm3': from file_readers import dm3_lib as dm3 data = dm3.DM3(self.imagePath_content).imagedata self.imarray_original = np.array(data) data = np.array(data, dtype='float64') data = (map01(data) * 255).astype('uint8') self.ori_image = Image.fromarray(data, mode='L') elif suffix == '.tif': im = Image.open(self.imagePath_content).convert('L') self.imarray_original = np.array(im, dtype='float64') self.ori_image = Image.fromarray( (map01(self.imarray_original) * 255).astype('uint8'), mode='L') else: self.ori_image = Image.open( self.imagePath_content).convert('L') self.imarray_original = np.array(self.ori_image) self.width, self.height = self.ori_image.size pix_image = PIL2Pixmap(self.ori_image) pix_image.scaled(self.ori.size(), QtCore.Qt.KeepAspectRatio) self.ori.setPixmap(pix_image) self.ori.show() self.ori_content = self.ori_image
def __load_model(self): # if not self.ori_image: # raise Exception("No image is selected.") self.cuda = self.use_cuda.isChecked() model_path = os.path.join(self.__curdir, self.__models[self.model_name]) recon_path = self.recon_name self.ori_content = self.ori_image dirname = os.path.dirname(self.imagePath_content) dataname = self.listData.currentText() result_d, result = load_model(dataname, self.matdata, self.matangles, self.matsize, model_path,\ recon_path, self.cuda, self.set_iter.value()) savename = os.path.join(dirname, dataname) if not os.path.exists(savename): os.mkdir(savename) scio.savemat(savename + "/angles.mat", {"angles":self.matangles}) if self.model_name=="Deepfusion(3d)" or self.model_name=="Deepfusion(3dGAN)": scio.savemat(savename + "/" + "%s_%s.mat" % (dataname,self.model_name), result_d) else: scio.savemat(savename + "/" + "%s_%s.mat" % (dataname,recon_path), result) scio.savemat(savename + "/" + "%s_%s_%s.mat" % (dataname, self.model_name, recon_path), result_d) print("Save mat successfully!") self.model_out = result_d self.recon_out = result dataname = self.listData.currentText() idx = self.img_num.value() print(dataname, idx) model_out = map01(self.model_out[dataname][idx, :, :]) model_out = (model_out * 255 / np.max(model_out)).astype('uint8') model_out = Image.fromarray((model_out), mode='L') model_out = PIL2Pixmap(model_out) model_out.scaled(self.model_output.size(), QtCore.Qt.KeepAspectRatio) self.model_output.setPixmap(model_out) self.model_output.show() recon_out = map01(self.recon_out[dataname][idx, :, :]) recon_out = (recon_out * 255 / np.max(recon_out)).astype('uint8') recon_out = Image.fromarray((recon_out), mode='L') recon_out = PIL2Pixmap(recon_out) recon_out.scaled(self.model_output.size(), QtCore.Qt.KeepAspectRatio) self.recon_output.setPixmap(recon_out) self.recon_output.show()
def ResultShow(self): dataname = self.listData.currentText() idx = self.img_num.value() # print(self.img_num.value()) self.img_num_show.setValue(self.img_num.value()) # print(dataname, idx) model_out = map01(self.model_out[dataname][idx,:,:]) model_out = (model_out * 255 / np.max(model_out)).astype('uint8') model_out = Image.fromarray((model_out), mode='L') model_out = PIL2Pixmap(model_out) model_out.scaled(self.model_output.size(), QtCore.Qt.KeepAspectRatio) self.model_output.setPixmap(model_out) self.model_output.show() recon_out = map01(self.recon_out[dataname][idx,:,:]) recon_out = (recon_out * 255 / np.max(recon_out)).astype('uint8') recon_out = Image.fromarray((recon_out), mode='L') recon_out = PIL2Pixmap(recon_out) recon_out.scaled(self.model_output.size(), QtCore.Qt.KeepAspectRatio) self.recon_output.setPixmap(recon_out) self.recon_output.show()
def __load_model(self): if not self.ori_image: raise Exception("No image is selected.") self.cuda = self.use_cuda.isChecked() model_path = os.path.join(self.__curdir, self.__models[self.model_name]) if self.change_size.currentText() == 'Down sample by 2': self.width, self.height = self.ori_image.size self.ori_content = self.ori_image.resize( (self.width // 2, self.height // 2), Image.BILINEAR) elif self.change_size.currentText() == 'Up sample by 2': self.width, self.height = self.ori_image.size self.ori_content = self.ori_image.resize( (self.width * 2, self.height * 2), Image.BICUBIC) elif self.change_size.currentText() == 'Down sample by 3': self.width, self.height = self.ori_image.size self.ori_content = self.ori_image.resize( (self.width // 3, self.height // 3), Image.BILINEAR) elif self.change_size.currentText() == 'Up sample by 3': self.width, self.height = self.ori_image.size self.ori_content = self.ori_image.resize( (self.width * 3, self.height * 3), Image.BICUBIC) elif self.change_size.currentText() == 'Down sample by 4': self.width, self.height = self.ori_image.size self.ori_content = self.ori_image.resize( (self.width // 4, self.height // 4), Image.BILINEAR) elif self.change_size.currentText() == 'Up sample by 4': self.width, self.height = self.ori_image.size self.ori_content = self.ori_image.resize( (self.width * 4, self.height * 4), Image.BICUBIC) else: self.ori_content = self.ori_image pix_image = PIL2Pixmap(self.ori_content) pix_image.scaled(self.ori.size(), QtCore.Qt.KeepAspectRatio) self.ori.setPixmap(pix_image) self.ori.show() self.width, self.height = self.ori_content.size if self.split.isChecked(): if self.height > 1024 and self.height < 2000: blk_row = 2 else: if self.height > 2000: blk_row = 4 else: blk_row = 1 if self.width > 1024 and self.width < 2000: blk_col = 2 else: if self.width > 2000: blk_col = 4 else: blk_col = 1 else: blk_col = 1 blk_row = 1 self.result = np.zeros((self.height, self.width)) - 100 for r in range(0, blk_row): for c in range(0, blk_col): inner_blk, outer_blk = GetIndexRangeOfBlk( self.height, self.width, blk_row, blk_col, r, c, over_lap=int(self.width * 0.01)) temp_image = self.ori_content.crop( (outer_blk[0], outer_blk[1], outer_blk[2], outer_blk[3])) temp_result = load_model(model_path, temp_image, self.cuda, self.set_iter.value()) # temp_result = map01(temp_result) self.result[outer_blk[1]:outer_blk[3], outer_blk[0]:outer_blk[2]] = np.maximum( temp_result, self.result[outer_blk[1]:outer_blk[3], outer_blk[0]:outer_blk[2]]) self.model_output_content = map01(self.result) self.model_output_content = ( self.model_output_content * 255 / np.max(self.model_output_content)).astype('uint8') self.output_image = Image.fromarray((self.model_output_content), mode='L') pix_image = PIL2Pixmap(self.output_image) pix_image.scaled(self.model_output.size(), QtCore.Qt.KeepAspectRatio) self.model_output.setPixmap(pix_image) self.model_output.show() del temp_image del temp_result