help@rskworld.in +91 93305 39277
RSK World
  • Home
  • Development
    • Web Development
    • Mobile Apps
    • Software
    • Games
    • Project
  • Technologies
    • Data Science
    • AI Development
    • Cloud Development
    • Blockchain
    • Cyber Security
    • Dev Tools
    • Testing Tools
  • Blog
  • About
  • Contact

Theme Settings

Color Scheme
Display Options
Font Size
100%
Back to Project
RSK World
medical-imaging
/
scripts
/
__pycache__
RSK World
medical-imaging
Medical Imaging Dataset - X-ray CT Scan MRI + Disease Detection + Computer-Aided Diagnosis + Medical AI
__pycache__
  • __init__.cpython-313.pyc1.1 KB
  • load_data.cpython-313.pyc12.6 KB
  • preprocess.cpython-313.pyc7.5 KB
  • visualize.cpython-313.pyc9.2 KB
load_data.pypreprocess.pyvisualize.py
scripts/load_data.py
Raw Download
Find: Go to:
"""
Medical Imaging Dataset - Data Loader
======================================

Project: Medical Imaging Dataset
Website: https://rskworld.in
Contact: help@rskworld.in, support@rskworld.in
Phone: +91 93305 39277
Founder: Molla Samser
Designer & Tester: Rima Khatun

This module provides functionality to load medical imaging data including
X-rays, CT scans, and MRI images with their diagnostic labels.
"""

import os
import numpy as np
from PIL import Image
import cv2
import json
from pathlib import Path
try:
    import pydicom
    DICOM_AVAILABLE = True
except ImportError:
    DICOM_AVAILABLE = False
    print("Warning: pydicom not available. DICOM files cannot be loaded.")


class MedicalImagingDataset:
    """
    Class to load and manage medical imaging dataset.
    
    Attributes:
        data_path (str): Path to the dataset directory
        xray_path (str): Path to X-ray images
        ct_path (str): Path to CT scan images
        mri_path (str): Path to MRI images
    """
    
    def __init__(self, data_path='./data'):
        """
        Initialize the MedicalImagingDataset.
        
        Args:
            data_path (str): Root path to the dataset directory
        """
        self.data_path = Path(data_path)
        self.xray_path = self.data_path / 'xray'
        self.ct_path = self.data_path / 'ct_scan'
        self.mri_path = self.data_path / 'mri'
        
    def load_xray_images(self, image_format='PNG'):
        """
        Load X-ray images from the dataset.
        
        Args:
            image_format (str): Image format to load (PNG, JPG, DICOM)
            
        Returns:
            list: List of image arrays and their corresponding labels
        """
        images = []
        labels = []
        
        images_dir = self.xray_path / 'images'
        labels_dir = self.xray_path / 'labels'
        
        if not images_dir.exists():
            print(f"Warning: X-ray images directory not found at {images_dir}")
            return images, labels
        
        for image_file in images_dir.iterdir():
            if not image_file.is_file():
                continue
            if image_file.suffix.lower() in ['.png', '.jpg', '.jpeg']:
                image = self._load_image(image_file)
                if image is not None:
                    images.append(image)
                    # Load corresponding label
                    label_file = labels_dir / f"{image_file.stem}.json"
                    if label_file.exists():
                        try:
                            with open(label_file, 'r') as f:
                                labels.append(json.load(f))
                        except (json.JSONDecodeError, IOError) as e:
                            print(f"Warning: Could not load label file {label_file}: {e}")
                            labels.append(None)
                    else:
                        labels.append(None)
        
        return images, labels
    
    def load_ct_images(self, image_format='PNG'):
        """
        Load CT scan images from the dataset.
        
        Args:
            image_format (str): Image format to load (PNG, JPG, DICOM)
            
        Returns:
            list: List of image arrays and their corresponding labels
        """
        images = []
        labels = []
        
        images_dir = self.ct_path / 'images'
        labels_dir = self.ct_path / 'labels'
        
        if not images_dir.exists():
            print(f"Warning: CT scan images directory not found at {images_dir}")
            return images, labels
        
        for image_file in images_dir.iterdir():
            if not image_file.is_file():
                continue
            if image_file.suffix.lower() in ['.png', '.jpg', '.jpeg']:
                image = self._load_image(image_file)
                if image is not None:
                    images.append(image)
                    # Load corresponding label
                    label_file = labels_dir / f"{image_file.stem}.json"
                    if label_file.exists():
                        try:
                            with open(label_file, 'r') as f:
                                labels.append(json.load(f))
                        except (json.JSONDecodeError, IOError) as e:
                            print(f"Warning: Could not load label file {label_file}: {e}")
                            labels.append(None)
                    else:
                        labels.append(None)
            elif image_file.suffix.lower() == '.dcm' and DICOM_AVAILABLE:
                image = self._load_dicom(image_file)
                if image is not None:
                    images.append(image)
                    label_file = labels_dir / f"{image_file.stem}.json"
                    if label_file.exists():
                        with open(label_file, 'r') as f:
                            labels.append(json.load(f))
                    else:
                        labels.append(None)
        
        return images, labels
    
    def load_mri_images(self, image_format='PNG'):
        """
        Load MRI images from the dataset.
        
        Args:
            image_format (str): Image format to load (PNG, JPG, DICOM)
            
        Returns:
            list: List of image arrays and their corresponding labels
        """
        images = []
        labels = []
        
        images_dir = self.mri_path / 'images'
        labels_dir = self.mri_path / 'labels'
        
        if not images_dir.exists():
            print(f"Warning: MRI images directory not found at {images_dir}")
            return images, labels
        
        for image_file in images_dir.iterdir():
            if not image_file.is_file():
                continue
            if image_file.suffix.lower() in ['.png', '.jpg', '.jpeg']:
                image = self._load_image(image_file)
                if image is not None:
                    images.append(image)
                    # Load corresponding label
                    label_file = labels_dir / f"{image_file.stem}.json"
                    if label_file.exists():
                        try:
                            with open(label_file, 'r') as f:
                                labels.append(json.load(f))
                        except (json.JSONDecodeError, IOError) as e:
                            print(f"Warning: Could not load label file {label_file}: {e}")
                            labels.append(None)
                    else:
                        labels.append(None)
            elif image_file.suffix.lower() == '.dcm' and DICOM_AVAILABLE:
                image = self._load_dicom(image_file)
                if image is not None:
                    images.append(image)
                    label_file = labels_dir / f"{image_file.stem}.json"
                    if label_file.exists():
                        with open(label_file, 'r') as f:
                            labels.append(json.load(f))
                    else:
                        labels.append(None)
        
        return images, labels
    
    def _load_image(self, image_path):
        """
        Load a standard image file (PNG, JPG).
        
        Args:
            image_path (Path): Path to the image file
            
        Returns:
            numpy.ndarray: Image array or None if loading fails
        """
        try:
            image = Image.open(image_path)
            return np.array(image)
        except Exception as e:
            print(f"Error loading image {image_path}: {e}")
            return None
    
    def _load_dicom(self, dicom_path):
        """
        Load a DICOM file.
        
        Args:
            dicom_path (Path): Path to the DICOM file
            
        Returns:
            numpy.ndarray: Image array or None if loading fails
        """
        if not DICOM_AVAILABLE:
            print("pydicom is not available. Cannot load DICOM files.")
            return None
        
        try:
            dicom_data = pydicom.dcmread(str(dicom_path))
            image = dicom_data.pixel_array
            return image
        except Exception as e:
            print(f"Error loading DICOM file {dicom_path}: {e}")
            return None
    
    def get_dataset_info(self):
        """
        Get information about the dataset.
        
        Returns:
            dict: Dictionary containing dataset statistics
        """
        info = {
            'xray_count': 0,
            'ct_count': 0,
            'mri_count': 0,
            'total_images': 0
        }
        
        # Count X-ray images
        if self.xray_path.exists():
            images_dir = self.xray_path / 'images'
            if images_dir.exists():
                info['xray_count'] = len([f for f in images_dir.iterdir() 
                                         if f.is_file() and f.suffix.lower() in ['.png', '.jpg', '.jpeg']])
        
        # Count CT scan images
        if self.ct_path.exists():
            images_dir = self.ct_path / 'images'
            if images_dir.exists():
                info['ct_count'] = len([f for f in images_dir.iterdir() 
                                       if f.is_file() and f.suffix.lower() in ['.png', '.jpg', '.jpeg', '.dcm']])
        
        # Count MRI images
        if self.mri_path.exists():
            images_dir = self.mri_path / 'images'
            if images_dir.exists():
                info['mri_count'] = len([f for f in images_dir.iterdir() 
                                        if f.is_file() and f.suffix.lower() in ['.png', '.jpg', '.jpeg', '.dcm']])
        
        info['total_images'] = info['xray_count'] + info['ct_count'] + info['mri_count']
        
        return info


if __name__ == '__main__':
    # Example usage
    dataset = MedicalImagingDataset(data_path='./data')
    
    # Get dataset information
    info = dataset.get_dataset_info()
    print("Dataset Information:")
    print(f"X-ray images: {info['xray_count']}")
    print(f"CT scan images: {info['ct_count']}")
    print(f"MRI images: {info['mri_count']}")
    print(f"Total images: {info['total_images']}")
    
    # Load images
    xray_images, xray_labels = dataset.load_xray_images()
    ct_images, ct_labels = dataset.load_ct_images()
    mri_images, mri_labels = dataset.load_mri_images()
    
    print(f"\nLoaded {len(xray_images)} X-ray images")
    print(f"Loaded {len(ct_images)} CT scan images")
    print(f"Loaded {len(mri_images)} MRI images")

296 lines•10.6 KB
python
scripts/preprocess.py
Raw Download
Find: Go to:
"""
Medical Imaging Dataset - Image Preprocessing
==============================================

Project: Medical Imaging Dataset
Website: https://rskworld.in
Contact: help@rskworld.in, support@rskworld.in
Phone: +91 93305 39277
Founder: Molla Samser
Designer & Tester: Rima Khatun

This module provides image preprocessing functions for medical images
including normalization, resizing, and enhancement.
"""

import numpy as np
import cv2
from PIL import Image
from typing import Tuple, Optional


def preprocess_image(image_path: str, 
                    image_type: str = 'xray',
                    target_size: Optional[Tuple[int, int]] = None,
                    normalize: bool = True) -> np.ndarray:
    """
    Preprocess a medical image.
    
    Args:
        image_path (str): Path to the image file
        image_type (str): Type of medical image ('xray', 'ct_scan', 'mri')
        target_size (tuple): Target size (width, height) for resizing
        normalize (bool): Whether to normalize pixel values to [0, 1]
        
    Returns:
        numpy.ndarray: Preprocessed image array
    """
    # Load image
    image = cv2.imread(image_path, cv2.IMREAD_GRAYSCALE)
    
    if image is None:
        raise ValueError(f"Could not load image from {image_path}")
    
    # Resize if target size is specified
    if target_size:
        image = cv2.resize(image, target_size, interpolation=cv2.INTER_AREA)
    
    # Apply image type specific preprocessing
    if image_type == 'xray':
        image = preprocess_xray(image)
    elif image_type == 'ct_scan':
        image = preprocess_ct_scan(image)
    elif image_type == 'mri':
        image = preprocess_mri(image)
    
    # Normalize if requested
    if normalize:
        image = normalize_image(image)
    
    return image


def preprocess_xray(image: np.ndarray) -> np.ndarray:
    """
    Preprocess X-ray images.
    
    Args:
        image (numpy.ndarray): Input X-ray image
        
    Returns:
        numpy.ndarray: Preprocessed X-ray image
    """
    # Apply CLAHE (Contrast Limited Adaptive Histogram Equalization)
    clahe = cv2.createCLAHE(clipLimit=2.0, tileGridSize=(8, 8))
    image = clahe.apply(image)
    
    # Apply Gaussian blur to reduce noise
    image = cv2.GaussianBlur(image, (3, 3), 0)
    
    return image


def preprocess_ct_scan(image: np.ndarray) -> np.ndarray:
    """
    Preprocess CT scan images.
    
    Args:
        image (numpy.ndarray): Input CT scan image
        
    Returns:
        numpy.ndarray: Preprocessed CT scan image
    """
    # Apply histogram equalization
    image = cv2.equalizeHist(image)
    
    # Apply median filter to reduce noise
    image = cv2.medianBlur(image, 5)
    
    return image


def preprocess_mri(image: np.ndarray) -> np.ndarray:
    """
    Preprocess MRI images.
    
    Args:
        image (numpy.ndarray): Input MRI image
        
    Returns:
        numpy.ndarray: Preprocessed MRI image
    """
    # Apply CLAHE for better contrast
    clahe = cv2.createCLAHE(clipLimit=3.0, tileGridSize=(8, 8))
    image = clahe.apply(image)
    
    # Apply bilateral filter to preserve edges while reducing noise
    image = cv2.bilateralFilter(image, 9, 75, 75)
    
    return image


def normalize_image(image: np.ndarray) -> np.ndarray:
    """
    Normalize image pixel values to [0, 1] range.
    
    Args:
        image (numpy.ndarray): Input image
        
    Returns:
        numpy.ndarray: Normalized image
    """
    # Convert to float
    image = image.astype(np.float32)
    
    # Normalize to [0, 1]
    image = (image - image.min()) / (image.max() - image.min() + 1e-8)
    
    return image


def enhance_contrast(image: np.ndarray, alpha: float = 1.5, beta: int = 0) -> np.ndarray:
    """
    Enhance image contrast.
    
    Args:
        image (numpy.ndarray): Input image
        alpha (float): Contrast control (1.0-3.0)
        beta (int): Brightness control (0-100)
        
    Returns:
        numpy.ndarray: Enhanced image
    """
    enhanced = cv2.convertScaleAbs(image, alpha=alpha, beta=beta)
    return enhanced


def apply_window_level(image: np.ndarray, window: int = 400, level: int = 50) -> np.ndarray:
    """
    Apply window/level transformation (common in medical imaging).
    
    Args:
        image (numpy.ndarray): Input image
        window (int): Window width
        level (int): Window center/level
        
    Returns:
        numpy.ndarray: Windowed image
    """
    min_val = level - window // 2
    max_val = level + window // 2
    
    # Clip values
    image = np.clip(image, min_val, max_val)
    
    # Normalize to [0, 255]
    image = ((image - min_val) / (max_val - min_val) * 255).astype(np.uint8)
    
    return image


def batch_preprocess(images: list, 
                    image_type: str = 'xray',
                    target_size: Optional[Tuple[int, int]] = None,
                    normalize: bool = True) -> list:
    """
    Preprocess a batch of images.
    
    Args:
        images (list): List of image paths or arrays
        image_type (str): Type of medical image
        target_size (tuple): Target size for resizing
        normalize (bool): Whether to normalize
        
    Returns:
        list: List of preprocessed images
    """
    preprocessed = []
    
    for image in images:
        if isinstance(image, str):
            # If it's a path, load and preprocess
            processed = preprocess_image(image, image_type, target_size, normalize)
        else:
            # If it's already an array, preprocess directly
            if target_size:
                image = cv2.resize(image, target_size, interpolation=cv2.INTER_AREA)
            
            if image_type == 'xray':
                processed = preprocess_xray(image)
            elif image_type == 'ct_scan':
                processed = preprocess_ct_scan(image)
            elif image_type == 'mri':
                processed = preprocess_mri(image)
            else:
                processed = image
            
            if normalize:
                processed = normalize_image(processed)
        
        preprocessed.append(processed)
    
    return preprocessed


if __name__ == '__main__':
    # Example usage
    print("Medical Imaging Dataset - Preprocessing Module")
    print("This module provides preprocessing functions for medical images.")
    print("\nAvailable functions:")
    print("- preprocess_image(): Preprocess a single image")
    print("- preprocess_xray(): Preprocess X-ray images")
    print("- preprocess_ct_scan(): Preprocess CT scan images")
    print("- preprocess_mri(): Preprocess MRI images")
    print("- normalize_image(): Normalize image pixel values")
    print("- enhance_contrast(): Enhance image contrast")
    print("- apply_window_level(): Apply window/level transformation")
    print("- batch_preprocess(): Preprocess a batch of images")

239 lines•7 KB
python
scripts/visualize.py
Raw Download
Find: Go to:
"""
Medical Imaging Dataset - Visualization
========================================

Project: Medical Imaging Dataset
Website: https://rskworld.in
Contact: help@rskworld.in, support@rskworld.in
Phone: +91 93305 39277
Founder: Molla Samser
Designer & Tester: Rima Khatun

This module provides visualization functions for medical images
with annotations and diagnostic labels.
"""

import numpy as np
import matplotlib.pyplot as plt
import cv2
from pathlib import Path
import json
from typing import Optional, Dict, List


def visualize_medical_image(image_path: str, 
                            label_path: Optional[str] = None,
                            title: Optional[str] = None,
                            save_path: Optional[str] = None) -> None:
    """
    Visualize a medical image with optional annotations.
    
    Args:
        image_path (str): Path to the medical image
        label_path (str): Path to the label/annotation file (JSON)
        title (str): Title for the plot
        save_path (str): Path to save the visualization
    """
    # Load image
    image = cv2.imread(image_path, cv2.IMREAD_GRAYSCALE)
    
    if image is None:
        raise ValueError(f"Could not load image from {image_path}")
    
    # Load labels if provided
    labels = None
    if label_path and Path(label_path).exists():
        try:
            with open(label_path, 'r') as f:
                labels = json.load(f)
        except (json.JSONDecodeError, IOError) as e:
            print(f"Warning: Could not load label file {label_path}: {e}")
            labels = None
    
    # Create figure
    fig, ax = plt.subplots(1, 1, figsize=(10, 10))
    
    # Display image
    ax.imshow(image, cmap='gray')
    ax.axis('off')
    
    # Add title
    if title:
        ax.set_title(title, fontsize=16, fontweight='bold')
    elif labels and 'diagnosis' in labels:
        ax.set_title(f"Diagnosis: {labels['diagnosis']}", fontsize=16, fontweight='bold')
    else:
        ax.set_title(Path(image_path).stem, fontsize=16, fontweight='bold')
    
    # Add annotations if available
    if labels:
        annotation_text = []
        if 'diagnosis' in labels:
            annotation_text.append(f"Diagnosis: {labels['diagnosis']}")
        if 'confidence' in labels:
            annotation_text.append(f"Confidence: {labels['confidence']:.2f}")
        if 'date' in labels:
            annotation_text.append(f"Date: {labels['date']}")
        
        if annotation_text:
            text = '\n'.join(annotation_text)
            ax.text(0.02, 0.98, text, transform=ax.transAxes,
                   fontsize=12, verticalalignment='top',
                   bbox=dict(boxstyle='round', facecolor='white', alpha=0.8))
    
    plt.tight_layout()
    
    # Save if path provided
    if save_path:
        plt.savefig(save_path, dpi=300, bbox_inches='tight')
        print(f"Visualization saved to {save_path}")
    
    plt.show()


def visualize_batch(images: List[np.ndarray],
                   labels: Optional[List[Dict]] = None,
                   titles: Optional[List[str]] = None,
                   cols: int = 3,
                   figsize: tuple = (15, 5)) -> None:
    """
    Visualize a batch of medical images.
    
    Args:
        images (list): List of image arrays
        labels (list): List of label dictionaries
        titles (list): List of titles for each image
        cols (int): Number of columns in the grid
        figsize (tuple): Figure size
    """
    n_images = len(images)
    rows = (n_images + cols - 1) // cols
    
    fig, axes = plt.subplots(rows, cols, figsize=figsize)
    
    if n_images == 1:
        axes = [axes]
    else:
        axes = axes.flatten()
    
    for i, image in enumerate(images):
        ax = axes[i]
        ax.imshow(image, cmap='gray')
        ax.axis('off')
        
        # Add title
        if titles and i < len(titles):
            ax.set_title(titles[i], fontsize=10)
        elif labels and i < len(labels) and labels[i] and 'diagnosis' in labels[i]:
            ax.set_title(labels[i]['diagnosis'], fontsize=10)
        else:
            ax.set_title(f"Image {i+1}", fontsize=10)
    
    # Hide unused subplots
    for i in range(n_images, len(axes)):
        axes[i].axis('off')
    
    plt.tight_layout()
    plt.show()


def compare_preprocessing(image_path: str,
                          save_path: Optional[str] = None) -> None:
    """
    Compare original and preprocessed versions of a medical image.
    
    Args:
        image_path (str): Path to the medical image
        save_path (str): Path to save the comparison
    """
    from scripts.preprocess import preprocess_image
    
    # Load original image
    original = cv2.imread(image_path, cv2.IMREAD_GRAYSCALE)
    
    # Determine image type from path
    image_type = 'xray'
    if 'ct' in image_path.lower() or 'ct_scan' in image_path.lower():
        image_type = 'ct_scan'
    elif 'mri' in image_path.lower():
        image_type = 'mri'
    
    # Preprocess image
    processed = preprocess_image(image_path, image_type=image_type)
    
    # Create comparison plot
    fig, axes = plt.subplots(1, 2, figsize=(15, 7))
    
    # Original
    axes[0].imshow(original, cmap='gray')
    axes[0].set_title('Original Image', fontsize=14, fontweight='bold')
    axes[0].axis('off')
    
    # Processed
    axes[1].imshow(processed, cmap='gray')
    axes[1].set_title('Preprocessed Image', fontsize=14, fontweight='bold')
    axes[1].axis('off')
    
    plt.tight_layout()
    
    if save_path:
        plt.savefig(save_path, dpi=300, bbox_inches='tight')
        print(f"Comparison saved to {save_path}")
    
    plt.show()


def plot_statistics(dataset_info: Dict) -> None:
    """
    Plot dataset statistics.
    
    Args:
        dataset_info (dict): Dictionary containing dataset statistics
    """
    categories = ['X-ray', 'CT Scan', 'MRI']
    counts = [
        dataset_info.get('xray_count', 0),
        dataset_info.get('ct_count', 0),
        dataset_info.get('mri_count', 0)
    ]
    
    fig, ax = plt.subplots(1, 1, figsize=(10, 6))
    
    bars = ax.bar(categories, counts, color=['#0dcaf0', '#6c757d', '#0d6efd'])
    
    # Add value labels on bars
    for bar, count in zip(bars, counts):
        height = bar.get_height()
        ax.text(bar.get_x() + bar.get_width()/2., height,
               f'{count}',
               ha='center', va='bottom', fontsize=12, fontweight='bold')
    
    ax.set_ylabel('Number of Images', fontsize=12)
    ax.set_title('Medical Imaging Dataset Statistics', fontsize=14, fontweight='bold')
    ax.grid(axis='y', alpha=0.3)
    
    plt.tight_layout()
    plt.show()


if __name__ == '__main__':
    # Example usage
    print("Medical Imaging Dataset - Visualization Module")
    print("This module provides visualization functions for medical images.")
    print("\nAvailable functions:")
    print("- visualize_medical_image(): Visualize a single medical image")
    print("- visualize_batch(): Visualize multiple images in a grid")
    print("- compare_preprocessing(): Compare original and preprocessed images")
    print("- plot_statistics(): Plot dataset statistics")

229 lines•7.2 KB
python
🚀 Support RSK World

Subscribe to our YouTube channel for latest tutorials & updates!



Click subscribe & support our work ❤️

About RSK World

Founded by Molla Samser, with Designer & Tester Rima Khatun, RSK World is your one-stop destination for free programming resources, source code, and development tools.

Founder: Molla Samser
Designer & Tester: Rima Khatun

Development

  • Game Development
  • Web Development
  • Mobile Development
  • AI Development
  • Development Tools

Legal

  • Terms & Conditions
  • Privacy Policy
  • Disclaimer

Contact Info

Nutanhat, Mongolkote
Purba Burdwan, West Bengal
India, 713147

+91 93305 39277

hello@rskworld.in
support@rskworld.in

© 2026 RSK World. All rights reserved.

Content used for educational purposes only. View Disclaimer