Spaces:
Sleeping
Sleeping
File size: 5,018 Bytes
fcd7965 5dfe4a1 fcd7965 e69525e fcd7965 ea7140d 3684fcf fcd7965 ea7140d 3e69fcb ea7140d 46985eb e69525e ea7140d 46985eb e69525e 46985eb ea7140d 46985eb ea7140d 46985eb ea7140d 3e69fcb ea7140d 46985eb e69525e fcd7965 46985eb fcd7965 94a397a 86c2c27 fb8bb17 46985eb 5dfe4a1 3e69fcb 5dfe4a1 3e69fcb 5dfe4a1 46985eb ea7140d 46985eb |
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 |
import streamlit as st
import pydicom
import matplotlib.pyplot as plt
import os
import time
# Set up the Streamlit app title
st.title("DICOM Viewer for CBCT Scans")
# Directory paths where DICOM files are stored
single_image_directory = "00002067/"
multiple_images_directory = "00007FA6/"
# Function to get the slice location or image position for sorting
def get_slice_location(dicom_file):
ds = pydicom.dcmread(dicom_file)
# Try to get the 'Slice Location'; if not available, use 'Image Position Patient'
if 'SliceLocation' in ds:
return float(ds.SliceLocation)
elif 'ImagePositionPatient' in ds:
# 'ImagePositionPatient' is a list; use the first element (z-coordinate)
return float(ds.ImagePositionPatient[2])
else:
return 0 # Default to 0 if neither tag is available
# Display the single DICOM image from the first folder
st.header("Single DICOM Image from folder 00002067")
# Check if the directory exists
if os.path.exists(single_image_directory):
# Get all files in the single_image_directory (no extension filtering)
single_image_files = [os.path.join(single_image_directory, f) for f in os.listdir(single_image_directory) if os.path.isfile(os.path.join(single_image_directory, f))]
if single_image_files:
# Read and display the first DICOM image found
ds = pydicom.dcmread(single_image_files[0])
single_image = ds.pixel_array
# Display the image
fig, ax = plt.subplots()
ax.imshow(single_image, cmap='gray')
ax.axis('off')
st.pyplot(fig)
else:
st.write("No DICOM files found in the directory 00002067.")
else:
st.write("Directory 00002067 does not exist.")
# Display the multiple DICOM images from the second folder using a slider
st.header("Browse Multiple DICOM Images from 00007FA6")
# Check if the directory exists
if os.path.exists(multiple_images_directory):
# Get all files in the multiple_images_directory (no extension filtering)
multiple_image_files = [os.path.join(multiple_images_directory, f) for f in os.listdir(multiple_images_directory) if os.path.isfile(os.path.join(multiple_images_directory, f))]
if multiple_image_files:
# Sort DICOM files by slice location or image position
multiple_image_files.sort(key=lambda x: get_slice_location(x))
# Read all DICOM images (sorted)
dicom_images = [pydicom.dcmread(file).pixel_array for file in multiple_image_files]
# Display the selected DICOM image dynamically
st.header(f"Display with Image Slider:")
# Interactive slider to select image index (below the image)
image_index = st.slider("Select Image Index", 0, len(dicom_images) - 1, 0)
# Display the image based on the slider position
fig, ax = plt.subplots()
ax.imshow(dicom_images[image_index], cmap='gray')
ax.axis('off')
st.pyplot(fig)
# Automatic image display section
st.header("Automatic Image Display (click start)")
placeholder = st.empty() # Placeholder for the images
# Initialize session state for animation control
if 'animation_running' not in st.session_state:
st.session_state.animation_running = False
if 'current_index' not in st.session_state:
st.session_state.current_index = 0
# Button to start the animation
if st.button("Start Animation"):
st.session_state.animation_running = True
# Button to pause the animation
if st.button("Pause Animation"):
st.session_state.animation_running = False
# Button to resume the animation
if st.button("Resume Animation"):
st.session_state.animation_running = True
# Display the current image based on the state
with placeholder:
fig, ax = plt.subplots()
ax.imshow(dicom_images[st.session_state.current_index], cmap='gray')
ax.axis('off')
st.pyplot(fig)
# Run the animation loop if running
if st.session_state.animation_running:
while st.session_state.animation_running and st.session_state.current_index < len(dicom_images):
with placeholder:
# Display the current image
fig, ax = plt.subplots()
ax.imshow(dicom_images[st.session_state.current_index], cmap='gray')
ax.axis('off')
st.pyplot(fig)
time.sleep(0.5) # Display each image for 0.5 seconds
st.session_state.current_index += 1
# Reset to loop from the start if the end is reached
if st.session_state.current_index >= len(dicom_images):
st.session_state.current_index = 0
else:
st.write("No DICOM files found in the directory 00007FA6.")
else:
st.write("Directory 00007FA6 does not exist.")
|