DHEIVER commited on
Commit
628502a
·
verified ·
1 Parent(s): c807f2c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +23 -7
app.py CHANGED
@@ -18,10 +18,17 @@ def dicom_to_nifti_and_display(dicom_file):
18
  dicom_data = pydicom.dcmread(dicom_file.name)
19
  dicom_image = dicom_data.pixel_array
20
 
21
- # Verificar se a imagem é 3D e extrair o slice central
22
  if len(dicom_image.shape) == 3:
23
- slice_index = dicom_image.shape[0] // 2 # Slice central ao longo do eixo Z
24
- dicom_slice = dicom_image[slice_index, :, :] # Extrair o slice
 
 
 
 
 
 
 
25
  else:
26
  dicom_slice = dicom_image # Se for 2D, use a imagem diretamente
27
 
@@ -36,6 +43,19 @@ def dicom_to_nifti_and_display(dicom_file):
36
  # Carregar o arquivo NIfTI para exibição
37
  nifti_loaded = nib.load(nifti_path).get_fdata()
38
 
 
 
 
 
 
 
 
 
 
 
 
 
 
39
  # Exibir as imagens usando Matplotlib
40
  fig, axes = plt.subplots(1, 2, figsize=(10, 5))
41
 
@@ -45,10 +65,6 @@ def dicom_to_nifti_and_display(dicom_file):
45
  axes[0].axis("off")
46
 
47
  # Imagem NIfTI (slice central)
48
- if len(nifti_loaded.shape) == 3:
49
- nifti_slice = nifti_loaded[nifti_loaded.shape[2] // 2, :, :] # Slice central ao longo do eixo Z
50
- else:
51
- nifti_slice = nifti_loaded
52
  axes[1].imshow(nifti_slice, cmap="gray")
53
  axes[1].set_title("Imagem NIfTI Convertida")
54
  axes[1].axis("off")
 
18
  dicom_data = pydicom.dcmread(dicom_file.name)
19
  dicom_image = dicom_data.pixel_array
20
 
21
+ # Extrair o slice central da imagem DICOM
22
  if len(dicom_image.shape) == 3:
23
+ # Identificar o maior eixo e extrair o slice central
24
+ largest_axis = np.argmax(dicom_image.shape)
25
+ slice_index = dicom_image.shape[largest_axis] // 2
26
+ if largest_axis == 0:
27
+ dicom_slice = dicom_image[slice_index, :, :]
28
+ elif largest_axis == 1:
29
+ dicom_slice = dicom_image[:, slice_index, :]
30
+ else:
31
+ dicom_slice = dicom_image[:, :, slice_index]
32
  else:
33
  dicom_slice = dicom_image # Se for 2D, use a imagem diretamente
34
 
 
43
  # Carregar o arquivo NIfTI para exibição
44
  nifti_loaded = nib.load(nifti_path).get_fdata()
45
 
46
+ # Extrair o slice central da imagem NIfTI
47
+ if len(nifti_loaded.shape) == 3:
48
+ largest_axis = np.argmax(nifti_loaded.shape)
49
+ slice_index = nifti_loaded.shape[largest_axis] // 2
50
+ if largest_axis == 0:
51
+ nifti_slice = nifti_loaded[slice_index, :, :]
52
+ elif largest_axis == 1:
53
+ nifti_slice = nifti_loaded[:, slice_index, :]
54
+ else:
55
+ nifti_slice = nifti_loaded[:, :, slice_index]
56
+ else:
57
+ nifti_slice = nifti_loaded # Se for 2D, use a imagem diretamente
58
+
59
  # Exibir as imagens usando Matplotlib
60
  fig, axes = plt.subplots(1, 2, figsize=(10, 5))
61
 
 
65
  axes[0].axis("off")
66
 
67
  # Imagem NIfTI (slice central)
 
 
 
 
68
  axes[1].imshow(nifti_slice, cmap="gray")
69
  axes[1].set_title("Imagem NIfTI Convertida")
70
  axes[1].axis("off")