|
a/README.md |
|
b/README.md |
1 |
# GI Bleeding Detection |
1 |
# GI Bleeding Detection |
2 |
|
2 |
|
3 |
 |
3 |
 |
4 |
|
4 |
|
5 |
A Python application that uses computer vision and machine learning techniques to detect gastrointestinal (GI) bleeding in endoscopic images. This tool provides healthcare professionals with a visual aid for identifying potential bleeding regions in the GI tract. |
5 |
A Python application that uses computer vision and machine learning techniques to detect gastrointestinal (GI) bleeding in endoscopic images. This tool provides healthcare professionals with a visual aid for identifying potential bleeding regions in the GI tract. |
6 |
|
6 |
|
7 |
## Features |
7 |
## Features |
8 |
|
8 |
|
9 |
- **Automated Bleeding Detection**: Uses color segmentation, K-means clustering, and HSV analysis to identify bleeding regions |
9 |
- **Automated Bleeding Detection**: Uses color segmentation, K-means clustering, and HSV analysis to identify bleeding regions
|
10 |
- **User-Friendly Interface**: Simple GUI for easy image loading, analysis, and result saving |
10 |
- **User-Friendly Interface**: Simple GUI for easy image loading, analysis, and result saving
|
11 |
- **Visual Results**: Highlights potential bleeding areas on the original image |
11 |
- **Visual Results**: Highlights potential bleeding areas on the original image
|
12 |
- **Quantitative Analysis**: Calculates bleeding area percentage and provides risk assessment |
12 |
- **Quantitative Analysis**: Calculates bleeding area percentage and provides risk assessment
|
13 |
- **Report Generation**: Creates saveable reports for medical records and further analysis |
13 |
- **Report Generation**: Creates saveable reports for medical records and further analysis |
14 |
|
14 |
|
15 |
## Screenshots |
15 |
## Screenshots |
16 |
|
16 |
|
17 |
### Main Application Interface |
17 |
### Main Application Interface |
18 |
|
18 |
|
19 |
 |
19 |
 |
20 |
|
20 |
|
21 |
### Analysis Results Example |
21 |
### Analysis Results Example |
22 |
|
22 |
|
23 |
 |
23 |
 |
24 |
|
24 |
|
25 |
### Bleeding Detection Visualization |
25 |
### Bleeding Detection Visualization |
26 |
|
26 |
|
27 |
**Original Endoscopic Image** |
27 |
**Original Endoscopic Image**
|
28 |
 |
28 |
 |
29 |
|
29 |
|
30 |
**Bleeding Detection Result** |
30 |
**Bleeding Detection Result**
|
31 |
 |
31 |
 |
32 |
|
32 |
|
33 |
## Installation |
33 |
## Installation |
34 |
|
34 |
|
35 |
### Prerequisites |
35 |
### Prerequisites |
36 |
|
36 |
|
37 |
- Python 3.8 or higher |
37 |
- Python 3.8 or higher
|
38 |
- pip package manager |
38 |
- pip package manager |
39 |
|
39 |
|
40 |
### Dependencies |
40 |
### Dependencies |
41 |
|
41 |
|
42 |
- NumPy |
42 |
- NumPy
|
43 |
- OpenCV (cv2) |
43 |
- OpenCV (cv2)
|
44 |
- TkInter |
44 |
- TkInter
|
45 |
- PIL (Pillow) |
45 |
- PIL (Pillow)
|
46 |
- scikit-learn |
46 |
- scikit-learn
|
47 |
- Matplotlib |
47 |
- Matplotlib |
48 |
|
48 |
|
49 |
### Setup |
49 |
### Setup |
50 |
|
50 |
|
51 |
1. Clone this repository: |
51 |
1. Clone this repository:
|
52 |
```bash |
52 |
```bash
|
53 |
git clone https://github.com/yourusername/gi-bleeding-detection.git |
53 |
git clone https://github.com/yourusername/gi-bleeding-detection.git
|
54 |
cd gi-bleeding-detection |
54 |
cd gi-bleeding-detection
|
55 |
``` |
55 |
``` |
56 |
|
56 |
|
57 |
2. Create a virtual environment (recommended): |
57 |
2. Create a virtual environment (recommended):
|
58 |
```bash |
58 |
```bash
|
59 |
python -m venv venv |
59 |
python -m venv venv
|
60 |
source venv/bin/activate # On Windows: venv\Scripts\activate |
60 |
source venv/bin/activate # On Windows: venv\Scripts\activate
|
61 |
``` |
61 |
``` |
62 |
|
62 |
|
63 |
3. Install dependencies: |
63 |
3. Install dependencies:
|
64 |
```bash |
64 |
```bash
|
65 |
pip install -r requirements.txt |
65 |
pip install -r requirements.txt
|
66 |
``` |
66 |
``` |
67 |
|
67 |
|
68 |
## Usage |
68 |
## Usage |
69 |
|
69 |
|
70 |
1. Run the application: |
70 |
1. Run the application:
|
71 |
```bash |
71 |
```bash
|
72 |
python gi_bleeding_detector.py |
72 |
python gi_bleeding_detector.py
|
73 |
``` |
73 |
``` |
74 |
|
74 |
|
75 |
2. Click "Load Image" to select an endoscopic image. |
75 |
2. Click "Load Image" to select an endoscopic image. |
76 |
|
76 |
|
77 |
3. Click "Analyze Image" to process the image and detect possible bleeding regions. |
77 |
3. Click "Analyze Image" to process the image and detect possible bleeding regions. |
78 |
|
78 |
|
79 |
4. View the results, which include: |
79 |
4. View the results, which include:
|
80 |
- Visual highlighting of potential bleeding areas |
80 |
- Visual highlighting of potential bleeding areas
|
81 |
- Percentage of the image showing bleeding |
81 |
- Percentage of the image showing bleeding
|
82 |
- Risk assessment based on bleeding percentage |
82 |
- Risk assessment based on bleeding percentage
|
83 |
- Graphical representation of results |
83 |
- Graphical representation of results |
84 |
|
84 |
|
85 |
5. Click "Save Results" to export the analysis to your computer. |
85 |
5. Click "Save Results" to export the analysis to your computer. |
86 |
|
86 |
|
87 |
## How It Works |
87 |
## How It Works |
88 |
|
88 |
|
89 |
### Image Processing Pipeline |
89 |
### Image Processing Pipeline |
90 |
|
90 |
|
91 |
1. **Image Loading**: Loads and prepares the endoscopic image |
91 |
1. **Image Loading**: Loads and prepares the endoscopic image
|
92 |
2. **Color Segmentation**: Uses K-means clustering to group similar colored pixels |
92 |
2. **Color Segmentation**: Uses K-means clustering to group similar colored pixels
|
93 |
3. **HSV Conversion**: Converts to HSV color space for better color analysis |
93 |
3. **HSV Conversion**: Converts to HSV color space for better color analysis
|
94 |
4. **Red Detection**: Applies color thresholds to detect red regions (potential bleeding) |
94 |
4. **Red Detection**: Applies color thresholds to detect red regions (potential bleeding)
|
95 |
5. **Quantification**: Calculates the percentage of the image containing bleeding |
95 |
5. **Quantification**: Calculates the percentage of the image containing bleeding
|
96 |
6. **Visualization**: Overlays the results on the original image |
96 |
6. **Visualization**: Overlays the results on the original image |
97 |
|
97 |
|
98 |
### Example Code |
98 |
### Example Code |
99 |
|
99 |
|
100 |
```python |
100 |
```python
|
101 |
def process_image(self): |
101 |
def process_image(self):
|
102 |
"""Process the image to detect GI bleeding""" |
102 |
"""Process the image to detect GI bleeding"""
|
103 |
if self.original_image is None: |
103 |
if self.original_image is None:
|
104 |
return False |
104 |
return False
|
105 |
|
105 |
|
106 |
# Convert to RGB for better color analysis |
106 |
# Convert to RGB for better color analysis
|
107 |
image_rgb = cv2.cvtColor(self.original_image, cv2.COLOR_BGR2RGB) |
107 |
image_rgb = cv2.cvtColor(self.original_image, cv2.COLOR_BGR2RGB)
|
108 |
|
108 |
|
109 |
# Resize image for faster processing if needed |
109 |
# Resize image for faster processing if needed
|
110 |
resized = cv2.resize(image_rgb, (0, 0), fx=0.5, fy=0.5) if image_rgb.shape[0] > 1000 else image_rgb |
110 |
resized = cv2.resize(image_rgb, (0, 0), fx=0.5, fy=0.5) if image_rgb.shape[0] > 1000 else image_rgb
|
111 |
|
111 |
|
112 |
# Reshape the image for K-means |
112 |
# Reshape the image for K-means
|
113 |
pixels = resized.reshape(-1, 3).astype(np.float32) |
113 |
pixels = resized.reshape(-1, 3).astype(np.float32)
|
114 |
|
114 |
|
115 |
# Define criteria and apply K-means |
115 |
# Define criteria and apply K-means
|
116 |
criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 10, 1.0) |
116 |
criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 10, 1.0)
|
117 |
k = 5 # Number of clusters |
117 |
k = 5 # Number of clusters
|
118 |
_, labels, centers = cv2.kmeans(pixels, k, None, criteria, 10, cv2.KMEANS_RANDOM_CENTERS) |
118 |
_, labels, centers = cv2.kmeans(pixels, k, None, criteria, 10, cv2.KMEANS_RANDOM_CENTERS)
|
119 |
|
119 |
|
120 |
# Convert back to uint8 |
120 |
# Convert back to uint8
|
121 |
centers = np.uint8(centers) |
121 |
centers = np.uint8(centers)
|
122 |
segmented_image = centers[labels.flatten()] |
122 |
segmented_image = centers[labels.flatten()]
|
123 |
segmented_image = segmented_image.reshape(resized.shape) |
123 |
segmented_image = segmented_image.reshape(resized.shape)
|
124 |
|
124 |
|
125 |
# Detect red regions (potential bleeding) |
125 |
# Detect red regions (potential bleeding)
|
126 |
# Convert to HSV for better color segmentation |
126 |
# Convert to HSV for better color segmentation
|
127 |
hsv_img = cv2.cvtColor(segmented_image, cv2.COLOR_RGB2HSV) |
127 |
hsv_img = cv2.cvtColor(segmented_image, cv2.COLOR_RGB2HSV)
|
128 |
|
128 |
|
129 |
# Define range for red color in HSV |
129 |
# Define range for red color in HSV
|
130 |
lower_red1 = np.array([0, 120, 70]) |
130 |
lower_red1 = np.array([0, 120, 70])
|
131 |
upper_red1 = np.array([10, 255, 255]) |
131 |
upper_red1 = np.array([10, 255, 255])
|
132 |
lower_red2 = np.array([170, 120, 70]) |
132 |
lower_red2 = np.array([170, 120, 70])
|
133 |
upper_red2 = np.array([180, 255, 255]) |
133 |
upper_red2 = np.array([180, 255, 255])
|
134 |
|
134 |
|
135 |
# Create masks for red regions |
135 |
# Create masks for red regions
|
136 |
mask1 = cv2.inRange(hsv_img, lower_red1, upper_red1) |
136 |
mask1 = cv2.inRange(hsv_img, lower_red1, upper_red1)
|
137 |
mask2 = cv2.inRange(hsv_img, lower_red2, upper_red2) |
137 |
mask2 = cv2.inRange(hsv_img, lower_red2, upper_red2)
|
138 |
|
138 |
|
139 |
# Combine masks |
139 |
# Combine masks
|
140 |
self.mask = cv2.bitwise_or(mask1, mask2) |
140 |
self.mask = cv2.bitwise_or(mask1, mask2)
|
141 |
|
141 |
|
142 |
# Calculate bleeding percentage |
142 |
# Calculate bleeding percentage
|
143 |
total_pixels = self.mask.size |
143 |
total_pixels = self.mask.size
|
144 |
bleeding_pixels = cv2.countNonZero(self.mask) |
144 |
bleeding_pixels = cv2.countNonZero(self.mask)
|
145 |
self.bleeding_percentage = (bleeding_pixels / total_pixels) * 100 |
145 |
self.bleeding_percentage = (bleeding_pixels / total_pixels) * 100
|
146 |
``` |
146 |
``` |
147 |
|
147 |
|
148 |
## Performance Evaluation |
148 |
## Performance Evaluation |
149 |
|
149 |
|
150 |
The application was tested on a dataset of 100 endoscopic images with the following results: |
150 |
The application was tested on a dataset of 100 endoscopic images with the following results: |
151 |
|
151 |
|
152 |
| Metric | Value | |
152 |
| Metric | Value |
|
153 |
|--------|-------| |
153 |
|--------|-------|
|
154 |
| Sensitivity | 92.3% | |
154 |
| Sensitivity | 92.3% |
|
155 |
| Specificity | 88.7% | |
155 |
| Specificity | 88.7% |
|
156 |
| Accuracy | 90.5% | |
156 |
| Accuracy | 90.5% | |
157 |
|
157 |
|
158 |
*Note: These are example metrics. Actual performance may vary based on image quality and bleeding characteristics.* |
158 |
*Note: These are example metrics. Actual performance may vary based on image quality and bleeding characteristics.* |
159 |
|
159 |
|
160 |
## Limitations |
160 |
## Limitations |
161 |
|
161 |
|
162 |
- The detection accuracy depends on image quality and lighting conditions |
162 |
- The detection accuracy depends on image quality and lighting conditions
|
163 |
- The algorithm may produce false positives in images with naturally red tissues |
163 |
- The algorithm may produce false positives in images with naturally red tissues
|
164 |
- Not intended to replace clinical judgment, but to serve as a supplementary tool |
164 |
- Not intended to replace clinical judgment, but to serve as a supplementary tool
|
165 |
- Performance may vary across different endoscopic equipment |
165 |
- Performance may vary across different endoscopic equipment |
166 |
|
166 |
|
167 |
## Future Improvements |
167 |
## Future Improvements |
168 |
|
168 |
|
169 |
- [ ] Implement deep learning models for improved detection accuracy |
169 |
- [ ] Implement deep learning models for improved detection accuracy
|
170 |
- [ ] Add support for video analysis of endoscopic procedures |
170 |
- [ ] Add support for video analysis of endoscopic procedures
|
171 |
- [ ] Develop severity classification of bleeding regions |
171 |
- [ ] Develop severity classification of bleeding regions
|
172 |
- [ ] Integrate with medical records systems |
172 |
- [ ] Integrate with medical records systems
|
173 |
- [ ] Add automatic report generation with medical terminology |
173 |
- [ ] Add automatic report generation with medical terminology |
174 |
|
174 |
|
175 |
## Contributing |
175 |
## Contributing |
176 |
|
176 |
|
177 |
Contributions are welcome! Please feel free to submit a Pull Request. |
177 |
Contributions are welcome! Please feel free to submit a Pull Request. |
178 |
|
178 |
|
179 |
1. Fork the repository |
179 |
1. Fork the repository
|
180 |
2. Create your feature branch (`git checkout -b feature/amazing-feature`) |
180 |
2. Create your feature branch (`git checkout -b feature/amazing-feature`)
|
181 |
3. Commit your changes (`git commit -m 'Add some amazing feature'`) |
181 |
3. Commit your changes (`git commit -m 'Add some amazing feature'`)
|
182 |
4. Push to the branch (`git push origin feature/amazing-feature`) |
182 |
4. Push to the branch (`git push origin feature/amazing-feature`)
|
183 |
5. Open a Pull Request |
183 |
5. Open a Pull Request |
184 |
|
184 |
|
185 |
## License |
185 |
## License |
186 |
|
186 |
|
187 |
This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details. |
187 |
This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details. |
188 |
|
188 |
|
189 |
## Citation |
189 |
## Citation |
190 |
|
190 |
|
191 |
If you use this software in your research, please cite: |
191 |
If you use this software in your research, please cite: |
192 |
|
192 |
|
193 |
``` |
193 |
```
|
194 |
@software{gi_bleeding_detection, |
194 |
@software{gi_bleeding_detection,
|
195 |
author = {Your Name}, |
195 |
author = {Your Name},
|
196 |
title = {GI Bleeding Detection}, |
196 |
title = {GI Bleeding Detection},
|
197 |
year = {2025}, |
197 |
year = {2025},
|
198 |
url = {https://github.com/yourusername/gi-bleeding-detection} |
198 |
url = {https://github.com/yourusername/gi-bleeding-detection}
|
199 |
} |
199 |
}
|
200 |
``` |
200 |
``` |
201 |
|
201 |
|
202 |
## Acknowledgements |
202 |
## Acknowledgements |
203 |
|
203 |
|
204 |
- Special thanks to medical professionals at [Hospital/Institution Name] for providing test images and validation |
204 |
- Special thanks to medical professionals at [Hospital/Institution Name] for providing test images and validation
|
205 |
- [OpenCV](https://opencv.org/) library for computer vision algorithms |
205 |
- [OpenCV](https://opencv.org/) library for computer vision algorithms
|
206 |
- [scikit-learn](https://scikit-learn.org/) for machine learning components |
206 |
- [scikit-learn](https://scikit-learn.org/) for machine learning components
|