File size: 5,039 Bytes
8312ddd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e9a9c89
 
 
 
 
 
 
 
 
 
 
 
 
 
8312ddd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e9a9c89
8312ddd
 
e9a9c89
8312ddd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import cv2
import numpy as np
import pandas as pd
import gradio as gr
from skimage import measure, morphology
from skimage.segmentation import watershed
import matplotlib.pyplot as plt

def process_image(image):
    """Process uploaded image and extract cell features"""
    # Convert to BGR if image is RGB
    if len(image.shape) == 3:
        image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
    
    # Basic preprocessing
    gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
    clahe = cv2.createCLAHE(clipLimit=3.0, tileGridSize=(8,8))
    enhanced = clahe.apply(gray)
    blurred = cv2.medianBlur(enhanced, 5)
    
    # Segmentation
    thresh = cv2.adaptiveThreshold(
        blurred, 255,
        cv2.ADAPTIVE_THRESH_GAUSSIAN_C,
        cv2.THRESH_BINARY_INV, 21, 4
    )
    
    # Clean small noise
    cleaned = morphology.opening(thresh, morphology.disk(2))
    
    # Watershed segmentation
    sure_bg = cv2.dilate(cleaned, morphology.disk(3), iterations=3)
    dist = cv2.distanceTransform(cleaned, cv2.DIST_L2, 5)
    ret, sure_fg = cv2.threshold(dist, 0.5*dist.max(), 255, 0)
    sure_fg = np.uint8(sure_fg)
    unknown = cv2.subtract(sure_bg, sure_fg)
    
    # Marker labelling
    ret, markers = cv2.connectedComponents(sure_fg)
    markers += 1
    markers[unknown == 255] = 0
    markers = watershed(-dist, markers, mask=cleaned)
    
    # Extract features
    features = []
    props = measure.regionprops(markers, intensity_image=gray)
    
    for i, prop in enumerate(props):
        if prop.area < 50:  # Filter small regions
            continue
        
        features.append({
            'cell_id': i+1,
            'area': prop.area,
            'perimeter': prop.perimeter,
            'circularity': (4 * np.pi * prop.area) / (prop.perimeter**2 + 1e-6),
            'mean_intensity': prop.mean_intensity,
            'centroid_x': prop.centroid[1],
            'centroid_y': prop.centroid[0]
        })
    
    # Create visualization
    vis_img = image.copy()
    contours = measure.find_contours(markers, 0.5)
    
    # Draw contours and cell IDs
    for contour in contours:
        coords = contour.astype(int)
        cv2.drawContours(vis_img, [coords], -1, (0,255,0), 1)
    
    for region in measure.regionprops(markers):
        if region.area >= 50:
            y, x = region.centroid
            cv2.putText(vis_img, str(region.label), 
                       (int(x), int(y)),
                       cv2.FONT_HERSHEY_SIMPLEX, 
                       0.4, (255,0,0), 1)
    
    # Create summary plots
    fig, axes = plt.subplots(1, 2, figsize=(12, 6))
    
    # Cell size distribution
    df = pd.DataFrame(features)
    if not df.empty:
        df['area'].hist(ax=axes[0], bins=20)
        axes[0].set_title('Cell Size Distribution')
        axes[0].set_xlabel('Area')
        axes[0].set_ylabel('Count')
        
        # Circularity vs Intensity
        axes[1].scatter(df['circularity'], df['mean_intensity'])
        axes[1].set_title('Circularity vs Intensity')
        axes[1].set_xlabel('Circularity')
        axes[1].set_ylabel('Mean Intensity')
    else:
        axes[0].text(0.5, 0.5, 'No cells detected', ha='center')
        axes[1].text(0.5, 0.5, 'No cells detected', ha='center')
    
    plt.tight_layout()
    
    return (
        cv2.cvtColor(vis_img, cv2.COLOR_BGR2RGB),
        fig,
        df
    )

# Create Gradio interface
with gr.Blocks(title="Cell Analysis Tool") as demo:
    gr.Markdown("""
    # 🔬 Bioengineering Cell Analysis Tool
    
    Upload microscopy images to analyze cell properties:
    - Automated cell detection
    - Feature extraction (size, shape, intensity)
    - Statistical analysis
    
    **Instructions:**
    1. Upload an image containing cells
    2. Wait for analysis to complete
    3. Review results in three tabs:
       - Detected cells visualization
       - Statistical plots
       - Detailed measurements table
    """)
    
    with gr.Row():
        with gr.Column():
            # Fixed the Image component configuration
            input_image = gr.Image(
                label="Upload Image",
                type="numpy"
            )
            analyze_btn = gr.Button(
                "Analyze Image", 
                variant="primary"
            )
        
        with gr.Column():
            with gr.Tabs():
                with gr.Tab("Detection Results"):
                    output_image = gr.Image(
                        label="Detected Cells"
                    )
                with gr.Tab("Statistics"):
                    output_plot = gr.Plot(
                        label="Statistical Analysis"
                    )
                with gr.Tab("Measurements"):
                    output_table = gr.DataFrame(
                        label="Cell Features"
                    )
    
    analyze_btn.click(
        fn=process_image,
        inputs=input_image,
        outputs=[output_image, output_plot, output_table]
    )

# Launch the demo
if __name__ == "__main__":
    demo.launch()