MedSimilarity is an open source Python to compare 2D medical images.
If you use this software in your publication, please cite it using:
@software{medsimilarity,
title={MedSimilarity},
author={Kulkarni, Pranav},
month={May},
year={2023},
url={https://github.com/UM2ii/MedSimilarity},
doi={10.5281/zenodo.7937894}
}
You can install MedSimilarity through pip
or you can manually install it from source.
pip install MedSimilarity
You can manually install MedSimilarity as follows:
git clone https://github.com/UM2ii/MedSimilarity
pip install -e MedSimilarity/
We have provided an example notebook in this repository, along with 100 test images to experiment with. You can find the example notebook here.
- End-to-end similarity pipeline
- Add support for DICOM and NII formats
- Extend functionality to 3D volumes
Computes the mean structural similarity index measure (SSIM) between two images. This implementation is an extension of skimage.metrics.structural_similarity
(https://scikit-image.org/docs/stable/api/skimage.metrics.html#skimage.metrics.structural_similarity) with preprocessing steps for medical images.
img1, img2: PIL.Image Input images
score: float The mean structural similarity index measure over the image grad: ndarray The gradient of the structural similarity between img1 and img2 diff: ndarray The full SSIM image
- Structural similarity is not invariant to transformations
Computes the pairwise structural similarity index measure (SSIM) between an image and a dataset and returns the top K matches.
img: str
Path to image
dataset: list
List containing paths to each image in dataset
top_k: int, optional
Number of best matches for img
in dataset
use_multiprocessing: bool, optional
Enables spawning of multiple processes to speed up pairwise SSIM calculation
score: ndarray
The top_k
matches for img
in dataset
with SSIM score
Computes the cosine similarity scores using dense vector representations (DVRS) between an image and dataset and returns the top K matches. This method uses SentenceTransformers ViT-B transformer for computation.
img: str
Path to image
dataset: list
List containing paths to each image in dataset
top_k: int, optional
Number of best matches for img
in dataset
use_multiprocessing: bool, optional
Enables encoding images into embeddings using multiprocessing. If device
is 'cuda', images are encoded using multiple GPUs. If device
is 'cpu', multiple CPUs are used
device: str, optional
Specifies device to move all resources to. Use 'cuda' to enable GPU acceleration. If left blank, by default 'cuda' is used if available. If not, 'cpu' is used
score: ndarray
The top_k
matches for img
in dataset
with DVRS score
Computes the combined score from structural similarity index measure (SSIM) and dense vector representations (DVRS) scores for a pair of images using the formula:
x_combined = sqrt(x_ssim)*(x_dvrs)^2
x_ssim: float The SSIM score for pair of images x_dvrs: float The DVRS score for pair of images
x_combined: float Combined score for pair of images
- This worked well in my testing but please take this with a grain of salt!