File size: 10,316 Bytes
1e8a7b1
 
 
92e84e5
1e8a7b1
92e84e5
 
 
 
a69a5af
1e8a7b1
 
92e84e5
1e8a7b1
92e84e5
 
 
 
 
 
 
 
 
1e8a7b1
 
 
 
92e84e5
 
1e8a7b1
 
92e84e5
 
1e8a7b1
 
 
92e84e5
 
 
 
1e8a7b1
 
92e84e5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1e8a7b1
92e84e5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1e8a7b1
92e84e5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1e8a7b1
92e84e5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1e8a7b1
 
 
92e84e5
1e8a7b1
92e84e5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a69a5af
1e8a7b1
 
92e84e5
1e8a7b1
 
92e84e5
1e8a7b1
92e84e5
 
 
 
 
 
 
 
 
 
1e8a7b1
92e84e5
 
 
 
 
 
1e8a7b1
92e84e5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a69a5af
92e84e5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a69a5af
1e8a7b1
 
 
 
 
92e84e5
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
import gradio as gr
import cv2
import numpy as np
import pandas as pd
import pydicom
import io
import os
from PIL import Image
import tempfile

class DicomAnalyzer:
    def __init__(self):
        self.results = []
        self.circle_diameter = 9
        self.zoom_factor = 1.0
        self.current_image1 = None
        self.current_image2 = None
        self.dicom_data1 = None
        self.dicom_data2 = None
        self.image_display1 = None
        self.image_display2 = None
        self.marks1 = []
        self.marks2 = []

    def load_dicom(self, file):
        try:
            if file is None:
                return None, None, None
                
            dicom_data = pydicom.dcmread(file.name)
            image = dicom_data.pixel_array.astype(np.float32)
            
            # Apply rescale slope and intercept
            rescale_slope = getattr(dicom_data, 'RescaleSlope', 1)
            rescale_intercept = getattr(dicom_data, 'RescaleIntercept', 0)
            image = (image * rescale_slope) + rescale_intercept
            
            # Store original image for analysis
            original_image = image.copy()
            
            # Normalize for display
            image_display = cv2.normalize(image, None, 0, 255, cv2.NORM_MINMAX).astype(np.uint8)
            
            # Convert to BGR for visualization
            if len(image_display.shape) == 2:
                image_display = cv2.cvtColor(image_display, cv2.COLOR_GRAY2BGR)
            
            return original_image, image_display, dicom_data
        except Exception as e:
            print(f"Error loading DICOM file: {str(e)}")
            return None, None, None

    def analyze_point(self, image, dicom_data, x, y):
        try:
            # Create a circular mask
            mask = np.zeros_like(image, dtype=np.uint8)
            y_indices, x_indices = np.ogrid[:image.shape[0], :image.shape[1]]
            distance_from_center = np.sqrt((x_indices - x)**2 + (y_indices - y)**2)
            mask[distance_from_center <= self.circle_diameter / 2] = 1

            # Extract pixel values within the circle
            pixels = image[mask == 1]

            # Calculate metrics
            area_pixels = np.sum(mask)
            pixel_spacing = float(dicom_data.PixelSpacing[0])
            area_mm2 = area_pixels * (pixel_spacing**2)
            mean = np.mean(pixels)
            stddev = np.std(pixels)
            min_val = np.min(pixels)
            max_val = np.max(pixels)

            return {
                'Area (mm²)': f"{area_mm2:.3f}",
                'Mean': f"{mean:.3f}",
                'StdDev': f"{stddev:.3f}",
                'Min': f"{min_val:.3f}",
                'Max': f"{max_val:.3f}"
            }
        except Exception as e:
            print(f"Error analyzing point: {str(e)}")
            return None

    def draw_circle(self, image, x, y, is_image1=True):
        try:
            image_copy = image.copy()
            
            # Draw all previous marks
            marks = self.marks1 if is_image1 else self.marks2
            for mark_x, mark_y in marks:
                cv2.circle(image_copy, 
                          (int(mark_x), int(mark_y)), 
                          int(self.circle_diameter/2), 
                          (0, 255, 255), 
                          1, 
                          lineType=cv2.LINE_AA)
            
            # Draw new mark
            cv2.circle(image_copy, 
                      (int(x), int(y)), 
                      int(self.circle_diameter/2), 
                      (0, 255, 255), 
                      1, 
                      lineType=cv2.LINE_AA)
            
            # Store new mark
            if is_image1:
                self.marks1.append((x, y))
            else:
                self.marks2.append((x, y))
                
            return image_copy
        except Exception as e:
            print(f"Error drawing circle: {str(e)}")
            return image

    def process_image1(self, file):
        image, image_display, dicom_data = self.load_dicom(file)
        self.current_image1 = image
        self.image_display1 = image_display
        self.dicom_data1 = dicom_data
        return image_display

    def process_image2(self, file):
        image, image_display, dicom_data = self.load_dicom(file)
        self.current_image2 = image
        self.image_display2 = image_display
        self.dicom_data2 = dicom_data
        return image_display

    def handle_click1(self, evt: gr.SelectData):
        if self.current_image1 is None:
            return self.image_display1, "Please load Image 1 first"
        
        try:
            x, y = evt.index
            marked_image = self.draw_circle(self.image_display1, x, y, is_image1=True)
            self.image_display1 = marked_image
            
            results = self.analyze_point(self.current_image1, self.dicom_data1, x, y)
            if results:
                results['Image'] = "Image 1"
                results['Point'] = f"({x}, {y})"
                self.results.append(results)
                
            return self.image_display1, self.format_results()
        except Exception as e:
            print(f"Error in handle_click1: {str(e)}")
            return self.image_display1, f"Error: {str(e)}"

    def handle_click2(self, evt: gr.SelectData):
        if self.current_image2 is None:
            return self.image_display2, "Please load Image 2 first"
        
        try:
            x, y = evt.index
            marked_image = self.draw_circle(self.image_display2, x, y, is_image1=False)
            self.image_display2 = marked_image
            
            results = self.analyze_point(self.current_image2, self.dicom_data2, x, y)
            if results:
                results['Image'] = "Image 2"
                results['Point'] = f"({x}, {y})"
                self.results.append(results)
                
            return self.image_display2, self.format_results()
        except Exception as e:
            print(f"Error in handle_click2: {str(e)}")
            return self.image_display2, f"Error: {str(e)}"

    def format_results(self):
        if not self.results:
            return "No results yet"
        df = pd.DataFrame(self.results)
        return df.to_string()

    def clear_results(self):
        self.results = []
        self.marks1 = []
        self.marks2 = []
        if self.current_image1 is not None:
            self.image_display1 = cv2.cvtColor(
                cv2.normalize(self.current_image1, None, 0, 255, cv2.NORM_MINMAX).astype(np.uint8),
                cv2.COLOR_GRAY2BGR
            )
        if self.current_image2 is not None:
            self.image_display2 = cv2.cvtColor(
                cv2.normalize(self.current_image2, None, 0, 255, cv2.NORM_MINMAX).astype(np.uint8),
                cv2.COLOR_GRAY2BGR
            )
        return "Results cleared", self.image_display1, self.image_display2

    def add_blank_row(self):
        self.results.append({
            'Image': '',
            'Point': '',
            'Area (mm²)': '',
            'Mean': '',
            'StdDev': '',
            'Min': '',
            'Max': ''
        })
        return self.format_results()

    def update_circle_diameter(self, value):
        self.circle_diameter = value
        return f"Circle diameter set to {value}"

    def save_results(self):
        try:
            if not self.results:
                return None, "No results to save"
                
            df = pd.DataFrame(self.results)
            
            # Create temporary file
            temp_dir = tempfile.gettempdir()
            temp_file = os.path.join(temp_dir, "analysis_results.xlsx")
            
            # Save to Excel
            df.to_excel(temp_file, index=False, engine='openpyxl')
            
            return temp_file, "Results saved successfully. Click to download."
        except Exception as e:
            print(f"Error saving results: {str(e)}")
            return None, f"Error saving results: {str(e)}"

def create_interface():
    analyzer = DicomAnalyzer()
    
    with gr.Blocks() as interface:
        gr.Markdown("# CT DICOM Image Analyzer")
        
        with gr.Row():
            with gr.Column():
                file1 = gr.File(label="Upload first DICOM file")
                image1 = gr.Image(label="Image 1", interactive=True, type="numpy")
                file1.change(fn=analyzer.process_image1, inputs=file1, outputs=image1)
            
            with gr.Column():
                file2 = gr.File(label="Upload second DICOM file")
                image2 = gr.Image(label="Image 2", interactive=True, type="numpy")
                file2.change(fn=analyzer.process_image2, inputs=file2, outputs=image2)
        
        with gr.Row():
            circle_diameter = gr.Slider(
                minimum=1, 
                maximum=20, 
                value=9, 
                step=1, 
                label="Circle Diameter"
            )
        
        with gr.Row():
            clear_btn = gr.Button("Clear Results")
            blank_row_btn = gr.Button("Add Blank Row")
            save_btn = gr.Button("Save Results")
        
        results = gr.Textbox(label="Results", interactive=False)
        file_output = gr.File(label="Download Results")
        status = gr.Textbox(label="Status")
        
        # Connect events
        circle_diameter.change(
            fn=analyzer.update_circle_diameter, 
            inputs=circle_diameter, 
            outputs=status
        )
        
        image1.select(
            fn=analyzer.handle_click1, 
            outputs=[image1, results]
        )
        
        image2.select(
            fn=analyzer.handle_click2, 
            outputs=[image2, results]
        )
        
        clear_btn.click(
            fn=analyzer.clear_results, 
            outputs=[status, image1, image2]
        )
        
        blank_row_btn.click(
            fn=analyzer.add_blank_row, 
            outputs=results
        )
        
        save_btn.click(
            fn=analyzer.save_results, 
            outputs=[file_output, status]
        )

    return interface

if __name__ == "__main__":
    interface = create_interface()
    interface.launch()