Spaces:
Sleeping
Sleeping
File size: 3,963 Bytes
60a7004 628502a c807f2c 628502a c807f2c 60a7004 628502a 60a7004 3685682 c807f2c 3685682 60a7004 c807f2c 3685682 60a7004 3685682 60a7004 3685682 60a7004 |
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 |
import os
import tempfile
import pydicom
import nibabel as nib
import numpy as np
import matplotlib.pyplot as plt
import gradio as gr
def dicom_to_nifti_and_display(dicom_file):
"""
Converte um arquivo DICOM para NIfTI e exibe as imagens antes e depois da conversão.
:param dicom_file: Arquivo DICOM carregado pelo usuário.
:return: Tupla contendo a imagem DICOM original e a imagem NIfTI convertida.
"""
try:
# Ler o arquivo DICOM
dicom_data = pydicom.dcmread(dicom_file.name)
dicom_image = dicom_data.pixel_array
# Extrair o slice central da imagem DICOM
if len(dicom_image.shape) == 3:
largest_axis = np.argmax(dicom_image.shape)
slice_index = dicom_image.shape[largest_axis] // 2
if largest_axis == 0:
dicom_slice = dicom_image[slice_index, :, :]
elif largest_axis == 1:
dicom_slice = dicom_image[:, slice_index, :]
else:
dicom_slice = dicom_image[:, :, slice_index]
else:
dicom_slice = dicom_image # Se for 2D, use a imagem diretamente
# Criar um objeto NIfTI
nifti_image = nib.Nifti1Image(dicom_image, affine=np.eye(4))
# Salvar temporariamente o arquivo NIfTI
with tempfile.NamedTemporaryFile(suffix=".nii", delete=False) as temp_nifti:
nib.save(nifti_image, temp_nifti.name)
nifti_path = temp_nifti.name
# Carregar o arquivo NIfTI para exibição
nifti_loaded = nib.load(nifti_path).get_fdata()
# Extrair o slice central da imagem NIfTI
if len(nifti_loaded.shape) == 3:
largest_axis = np.argmax(nifti_loaded.shape)
slice_index = nifti_loaded.shape[largest_axis] // 2
if largest_axis == 0:
nifti_slice = nifti_loaded[slice_index, :, :]
elif largest_axis == 1:
nifti_slice = nifti_loaded[:, slice_index, :]
else:
nifti_slice = nifti_loaded[:, :, slice_index]
else:
nifti_slice = nifti_loaded # Se for 2D, use a imagem diretamente
# Exibir as imagens usando Matplotlib
fig, axes = plt.subplots(1, 2, figsize=(12, 6), constrained_layout=True)
# Imagem DICOM (slice central)
axes[0].imshow(dicom_slice, cmap="gray", aspect="auto")
axes[0].set_title("Imagem DICOM Original", fontsize=14, color="blue")
axes[0].axis("off")
# Imagem NIfTI (slice central)
axes[1].imshow(nifti_slice, cmap="gray", aspect="auto")
axes[1].set_title("Imagem NIfTI Convertida", fontsize=14, color="green")
axes[1].axis("off")
# Salvar a figura em um buffer temporário
with tempfile.NamedTemporaryFile(suffix=".png", delete=False) as temp_image:
plt.savefig(temp_image.name, bbox_inches="tight", dpi=300) # Alta resolução
plt.close(fig)
return temp_image.name, nifti_path
except Exception as e:
raise gr.Error(f"Erro ao processar o arquivo: {e}")
# Interface Gradio
with gr.Blocks() as demo:
gr.Markdown("# Conversor DICOM para NIfTI com Visualização")
gr.Markdown("Faça upload de um arquivo DICOM, visualize a imagem original, converta para NIfTI e veja o resultado.")
with gr.Row():
input_dicom = gr.File(label="Upload de Arquivo DICOM")
output_images = gr.Image(label="Visualização das Imagens", interactive=False)
output_nifti = gr.File(label="Download do Arquivo NIfTI")
convert_button = gr.Button("Converter e Visualizar")
def process_and_display(dicom_file):
image_path, nifti_path = dicom_to_nifti_and_display(dicom_file)
return image_path, nifti_path
convert_button.click(
process_and_display,
inputs=input_dicom,
outputs=[output_images, output_nifti]
)
# Executar a interface
demo.launch() |