Implementing Pseudocolor in Python: Examples with Matplotlib and OpenCV
1) Quick overview
- Pseudocolor maps single-channel (grayscale) data to color for better visual perception.
- Use Matplotlib for plotting and custom colormaps; use OpenCV for fast image-based applyColorMap / LUT operations.
2) Code examples
Matplotlib — simple pseudocolor (imshow + colormap)
python
import numpy as np import matplotlib.pyplot as plt # sample data data = np.random.randn(200, 200) plt.imshow(data, cmap=‘viridis’, origin=‘lower’) plt.colorbar() plt.title(‘Pseudocolor with Matplotlib (viridis)’) plt.show()
Common Matplotlib colormaps: ‘viridis’, ‘plasma’, ‘magma’, ‘inferno’, ‘cividis’, ‘jet’ (avoid for perceptual issues).
Matplotlib — custom colormap (linear segmented)
python
import numpy as np import matplotlib.pyplot as plt from matplotlib.colors import LinearSegmentedColormap cmap = LinearSegmentedColormap.from_list(‘mycmap’, [(0.0, ’#000000’), (0.5, ’#ff0000’), (1.0, ’#ffff00’)]) data = np.linspace(0, 1, 256).reshape(16,16) plt.imshow(data, cmap=cmap, origin=‘lower’) plt.colorbar() plt.title(‘Custom Matplotlib colormap’) plt.show()
OpenCV — apply built-in colormap
”`python import cv2
Leave a Reply