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
| Name | Required | Description | Default |
|---|---|---|---|
| series_uid | Yes | Series UID of the loaded DICOM image | |
| boundary_percentage | No | Percentage of image to crop from each boundary (0.0-0.5) |
Implementation Reference
- src/dicom_mcp/server.py:367-405 (handler)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}" ) ]
- src/dicom_mcp/server.py:211-222 (registration)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"], }, )
- src/dicom_mcp/dicom_tools.py:255-267 (helper)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]