Skip to main content
Glama

crop-dicom-image

Crop DICOM medical images by removing specified boundary percentages to focus on relevant anatomical regions within medical imaging workflows.

Instructions

Crop a loaded DICOM image by removing boundary percentage

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
series_uidYesSeries UID of the loaded DICOM image
boundary_percentageNoPercentage of image to crop from each boundary (0.0-0.5)

Implementation Reference

  • MCP tool handler for 'crop-dicom-image'. Retrieves cached DICOM image by series_uid, crops it using crop_image helper with boundary_percentage, caches the result, and returns shape information.
    elif name == "crop-dicom-image":
        series_uid = arguments.get("series_uid")
        boundary_percentage = float(arguments.get("boundary_percentage", 0.2))
        
        if not series_uid or series_uid not in dicom_cache:
            raise ValueError(f"Invalid or not loaded series UID: {series_uid}. Use load-dicom-series first.")
            
        if boundary_percentage <= 0 or boundary_percentage >= 0.5:
            raise ValueError("Boundary percentage must be between 0 and 0.5")
            
        # Get the cached image
        cached_data = dicom_cache[series_uid]
        original_image = cached_data["image"]
        
        # Crop the image
        cropped_image = crop_image(original_image, boundary_percentage)
        
        # Cache the cropped image with a new ID
        cropped_uid = f"{series_uid}_cropped_{int(boundary_percentage*100)}"
        dicom_cache[cropped_uid] = {
            "image": cropped_image,
            "metadata": cached_data["metadata"],
            "original": series_uid,
            "crop_percentage": boundary_percentage
        }
        
        # Return summary information
        original_shape = original_image.shape
        cropped_shape = cropped_image.shape
        
        return [
            types.TextContent(
                type="text",
                text=f"Cropped DICOM image {series_uid} to {cropped_uid}\n" +
                     f"Original shape: {original_shape}\n" +
                     f"Cropped shape: {cropped_shape}\n" +
                     f"Boundary percentage: {boundary_percentage}"
            )
        ]
  • Registration of the 'crop-dicom-image' tool in the MCP server's list_tools handler, including name, description, and input schema.
    types.Tool(
        name="crop-dicom-image",
        description="Crop a loaded DICOM image by removing boundary percentage",
        inputSchema={
            "type": "object",
            "properties": {
                "series_uid": {"type": "string", "description": "Series UID of the loaded DICOM image"},
                "boundary_percentage": {"type": "number", "description": "Percentage of image to crop from each boundary (0.0-0.5)"},
            },
            "required": ["series_uid"],
        },
    )
  • Helper function crop_image that performs the actual image cropping by slicing the numpy array based on boundary percentage.
    def crop_image(image: np.ndarray, boundary_percentage: float = 0.2) -> np.ndarray:
        """Crop an image by removing the specified percentage from each boundary"""
        if boundary_percentage <= 0 or boundary_percentage >= 0.5:
            raise ValueError("Boundary percentage must be between 0 and 0.5")
        
        shape = image.shape
        boundary = [int(s * boundary_percentage) for s in shape]
        
        # Calculate slice indices
        slices = tuple(slice(b, s - b) for b, s in zip(boundary, shape))
        
        # Return cropped image
        return image[slices]

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/shaunporwal/DICOM-MCP'

If you have feedback or need assistance with the MCP directory API, please join our Discord server