--- a/README.md +++ b/README.md @@ -1,206 +1,206 @@ -# GI Bleeding Detection - - - -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. - -## Features - -- **Automated Bleeding Detection**: Uses color segmentation, K-means clustering, and HSV analysis to identify bleeding regions -- **User-Friendly Interface**: Simple GUI for easy image loading, analysis, and result saving -- **Visual Results**: Highlights potential bleeding areas on the original image -- **Quantitative Analysis**: Calculates bleeding area percentage and provides risk assessment -- **Report Generation**: Creates saveable reports for medical records and further analysis - -## Screenshots - -### Main Application Interface - - - -### Analysis Results Example - - - -### Bleeding Detection Visualization - -**Original Endoscopic Image** - - -**Bleeding Detection Result** - - -## Installation - -### Prerequisites - -- Python 3.8 or higher -- pip package manager - -### Dependencies - -- NumPy -- OpenCV (cv2) -- TkInter -- PIL (Pillow) -- scikit-learn -- Matplotlib - -### Setup - -1. Clone this repository: - ```bash - git clone https://github.com/yourusername/gi-bleeding-detection.git - cd gi-bleeding-detection - ``` - -2. Create a virtual environment (recommended): - ```bash - python -m venv venv - source venv/bin/activate # On Windows: venv\Scripts\activate - ``` - -3. Install dependencies: - ```bash - pip install -r requirements.txt - ``` - -## Usage - -1. Run the application: - ```bash - python gi_bleeding_detector.py - ``` - -2. Click "Load Image" to select an endoscopic image. - -3. Click "Analyze Image" to process the image and detect possible bleeding regions. - -4. View the results, which include: - - Visual highlighting of potential bleeding areas - - Percentage of the image showing bleeding - - Risk assessment based on bleeding percentage - - Graphical representation of results - -5. Click "Save Results" to export the analysis to your computer. - -## How It Works - -### Image Processing Pipeline - -1. **Image Loading**: Loads and prepares the endoscopic image -2. **Color Segmentation**: Uses K-means clustering to group similar colored pixels -3. **HSV Conversion**: Converts to HSV color space for better color analysis -4. **Red Detection**: Applies color thresholds to detect red regions (potential bleeding) -5. **Quantification**: Calculates the percentage of the image containing bleeding -6. **Visualization**: Overlays the results on the original image - -### Example Code - -```python -def process_image(self): - """Process the image to detect GI bleeding""" - if self.original_image is None: - return False - - # Convert to RGB for better color analysis - image_rgb = cv2.cvtColor(self.original_image, cv2.COLOR_BGR2RGB) - - # Resize image for faster processing if needed - resized = cv2.resize(image_rgb, (0, 0), fx=0.5, fy=0.5) if image_rgb.shape[0] > 1000 else image_rgb - - # Reshape the image for K-means - pixels = resized.reshape(-1, 3).astype(np.float32) - - # Define criteria and apply K-means - criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 10, 1.0) - k = 5 # Number of clusters - _, labels, centers = cv2.kmeans(pixels, k, None, criteria, 10, cv2.KMEANS_RANDOM_CENTERS) - - # Convert back to uint8 - centers = np.uint8(centers) - segmented_image = centers[labels.flatten()] - segmented_image = segmented_image.reshape(resized.shape) - - # Detect red regions (potential bleeding) - # Convert to HSV for better color segmentation - hsv_img = cv2.cvtColor(segmented_image, cv2.COLOR_RGB2HSV) - - # Define range for red color in HSV - lower_red1 = np.array([0, 120, 70]) - upper_red1 = np.array([10, 255, 255]) - lower_red2 = np.array([170, 120, 70]) - upper_red2 = np.array([180, 255, 255]) - - # Create masks for red regions - mask1 = cv2.inRange(hsv_img, lower_red1, upper_red1) - mask2 = cv2.inRange(hsv_img, lower_red2, upper_red2) - - # Combine masks - self.mask = cv2.bitwise_or(mask1, mask2) - - # Calculate bleeding percentage - total_pixels = self.mask.size - bleeding_pixels = cv2.countNonZero(self.mask) - self.bleeding_percentage = (bleeding_pixels / total_pixels) * 100 -``` - -## Performance Evaluation - -The application was tested on a dataset of 100 endoscopic images with the following results: - -| Metric | Value | -|--------|-------| -| Sensitivity | 92.3% | -| Specificity | 88.7% | -| Accuracy | 90.5% | - -*Note: These are example metrics. Actual performance may vary based on image quality and bleeding characteristics.* - -## Limitations - -- The detection accuracy depends on image quality and lighting conditions -- The algorithm may produce false positives in images with naturally red tissues -- Not intended to replace clinical judgment, but to serve as a supplementary tool -- Performance may vary across different endoscopic equipment - -## Future Improvements - -- [ ] Implement deep learning models for improved detection accuracy -- [ ] Add support for video analysis of endoscopic procedures -- [ ] Develop severity classification of bleeding regions -- [ ] Integrate with medical records systems -- [ ] Add automatic report generation with medical terminology - -## Contributing - -Contributions are welcome! Please feel free to submit a Pull Request. - -1. Fork the repository -2. Create your feature branch (`git checkout -b feature/amazing-feature`) -3. Commit your changes (`git commit -m 'Add some amazing feature'`) -4. Push to the branch (`git push origin feature/amazing-feature`) -5. Open a Pull Request - -## License - -This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details. - -## Citation - -If you use this software in your research, please cite: - -``` -@software{gi_bleeding_detection, - author = {Your Name}, - title = {GI Bleeding Detection}, - year = {2025}, - url = {https://github.com/yourusername/gi-bleeding-detection} -} -``` - -## Acknowledgements - -- Special thanks to medical professionals at [Hospital/Institution Name] for providing test images and validation -- [OpenCV](https://opencv.org/) library for computer vision algorithms -- [scikit-learn](https://scikit-learn.org/) for machine learning components +# GI Bleeding Detection + + + +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. + +## Features + +- **Automated Bleeding Detection**: Uses color segmentation, K-means clustering, and HSV analysis to identify bleeding regions +- **User-Friendly Interface**: Simple GUI for easy image loading, analysis, and result saving +- **Visual Results**: Highlights potential bleeding areas on the original image +- **Quantitative Analysis**: Calculates bleeding area percentage and provides risk assessment +- **Report Generation**: Creates saveable reports for medical records and further analysis + +## Screenshots + +### Main Application Interface + + + +### Analysis Results Example + + + +### Bleeding Detection Visualization + +**Original Endoscopic Image** + + +**Bleeding Detection Result** + + +## Installation + +### Prerequisites + +- Python 3.8 or higher +- pip package manager + +### Dependencies + +- NumPy +- OpenCV (cv2) +- TkInter +- PIL (Pillow) +- scikit-learn +- Matplotlib + +### Setup + +1. Clone this repository: + ```bash + git clone https://github.com/yourusername/gi-bleeding-detection.git + cd gi-bleeding-detection + ``` + +2. Create a virtual environment (recommended): + ```bash + python -m venv venv + source venv/bin/activate # On Windows: venv\Scripts\activate + ``` + +3. Install dependencies: + ```bash + pip install -r requirements.txt + ``` + +## Usage + +1. Run the application: + ```bash + python gi_bleeding_detector.py + ``` + +2. Click "Load Image" to select an endoscopic image. + +3. Click "Analyze Image" to process the image and detect possible bleeding regions. + +4. View the results, which include: + - Visual highlighting of potential bleeding areas + - Percentage of the image showing bleeding + - Risk assessment based on bleeding percentage + - Graphical representation of results + +5. Click "Save Results" to export the analysis to your computer. + +## How It Works + +### Image Processing Pipeline + +1. **Image Loading**: Loads and prepares the endoscopic image +2. **Color Segmentation**: Uses K-means clustering to group similar colored pixels +3. **HSV Conversion**: Converts to HSV color space for better color analysis +4. **Red Detection**: Applies color thresholds to detect red regions (potential bleeding) +5. **Quantification**: Calculates the percentage of the image containing bleeding +6. **Visualization**: Overlays the results on the original image + +### Example Code + +```python +def process_image(self): + """Process the image to detect GI bleeding""" + if self.original_image is None: + return False + + # Convert to RGB for better color analysis + image_rgb = cv2.cvtColor(self.original_image, cv2.COLOR_BGR2RGB) + + # Resize image for faster processing if needed + resized = cv2.resize(image_rgb, (0, 0), fx=0.5, fy=0.5) if image_rgb.shape[0] > 1000 else image_rgb + + # Reshape the image for K-means + pixels = resized.reshape(-1, 3).astype(np.float32) + + # Define criteria and apply K-means + criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 10, 1.0) + k = 5 # Number of clusters + _, labels, centers = cv2.kmeans(pixels, k, None, criteria, 10, cv2.KMEANS_RANDOM_CENTERS) + + # Convert back to uint8 + centers = np.uint8(centers) + segmented_image = centers[labels.flatten()] + segmented_image = segmented_image.reshape(resized.shape) + + # Detect red regions (potential bleeding) + # Convert to HSV for better color segmentation + hsv_img = cv2.cvtColor(segmented_image, cv2.COLOR_RGB2HSV) + + # Define range for red color in HSV + lower_red1 = np.array([0, 120, 70]) + upper_red1 = np.array([10, 255, 255]) + lower_red2 = np.array([170, 120, 70]) + upper_red2 = np.array([180, 255, 255]) + + # Create masks for red regions + mask1 = cv2.inRange(hsv_img, lower_red1, upper_red1) + mask2 = cv2.inRange(hsv_img, lower_red2, upper_red2) + + # Combine masks + self.mask = cv2.bitwise_or(mask1, mask2) + + # Calculate bleeding percentage + total_pixels = self.mask.size + bleeding_pixels = cv2.countNonZero(self.mask) + self.bleeding_percentage = (bleeding_pixels / total_pixels) * 100 +``` + +## Performance Evaluation + +The application was tested on a dataset of 100 endoscopic images with the following results: + +| Metric | Value | +|--------|-------| +| Sensitivity | 92.3% | +| Specificity | 88.7% | +| Accuracy | 90.5% | + +*Note: These are example metrics. Actual performance may vary based on image quality and bleeding characteristics.* + +## Limitations + +- The detection accuracy depends on image quality and lighting conditions +- The algorithm may produce false positives in images with naturally red tissues +- Not intended to replace clinical judgment, but to serve as a supplementary tool +- Performance may vary across different endoscopic equipment + +## Future Improvements + +- [ ] Implement deep learning models for improved detection accuracy +- [ ] Add support for video analysis of endoscopic procedures +- [ ] Develop severity classification of bleeding regions +- [ ] Integrate with medical records systems +- [ ] Add automatic report generation with medical terminology + +## Contributing + +Contributions are welcome! Please feel free to submit a Pull Request. + +1. Fork the repository +2. Create your feature branch (`git checkout -b feature/amazing-feature`) +3. Commit your changes (`git commit -m 'Add some amazing feature'`) +4. Push to the branch (`git push origin feature/amazing-feature`) +5. Open a Pull Request + +## License + +This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details. + +## Citation + +If you use this software in your research, please cite: + +``` +@software{gi_bleeding_detection, + author = {Your Name}, + title = {GI Bleeding Detection}, + year = {2025}, + url = {https://github.com/yourusername/gi-bleeding-detection} +} +``` + +## Acknowledgements + +- Special thanks to medical professionals at [Hospital/Institution Name] for providing test images and validation +- [OpenCV](https://opencv.org/) library for computer vision algorithms +- [scikit-learn](https://scikit-learn.org/) for machine learning components