Python script which recolors spectrographic images (or heatmaps) using the OpenCV Python package, in the following steps:
- Convert image to grayscale
- Applying Canny edge detection and slightly dilating the image regions around found edge locations (optional), found in the grayscale representation, to reduce smudging and pre-process images for segmentation models.
- Overlay found edges in black & white atop the color image.
- Run:
cd repositoryName/
pip install -r requirements.txt # to install required dependencies- After running:
python recolorimage.py --helpWe can see the possible options:
usage: recolorimage.py [-h] [--image-path IMAGE_PATH] [--cmap-name CMAP_NAME] [--test TEST]
options:
-h, --help show this help message and exit
--image-path IMAGE_PATH
image path to recolor (relative to current directory).
--cmap-name CMAP_NAME
perceptually uniform sequential colormap (from matplotlib) to convert to (throws error if not a valid type).
--test TEST create sample outputs of each perceptually uniform sequential colormap type, in matplotlib, in original image directory.
It may be a good option to try all possibilities using the test flag!
- Change the edge detection margins in the script as you find useful for your images, here:
def load_and_recolor_image(image_path, new_cmap='hot'):
edge_threshold1 = 500
edge_threshold2 = 1250- Substitute some your desired, alternative colormaps at this point of the script
try:
# generate colormap
cmap = plt.get_cmap(new_cmap)
except Exception:
# if invalid colormap name entered, throw:
print('please enter a valid colormap name.')
sys.exit(1) by consulting the Matplotlib Colormap Documentation
More on Canny Edge Detection (from the OpenCV Docs):
- [The comparision of gradients between borders of pixels] decides which are all edges are really edges and which are not.
- For this, we need two threshold values, minVal and maxVal. [hysterisis-based inclusion]
- Any edges with intensity gradient more than maxVal are sure to be edges and those below minVal are sure to be non-edges, so discarded.
- Those who lie between these two thresholds are classified edges or non-edges based on their connectivity.
- If they are connected to "sure-edge" pixels, they are considered to be part of edges.
- Otherwise, they are also discarded.