Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove cmapy dependency #30

Merged
merged 1 commit into from
May 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ classifiers = [
]
dependencies = [
"appdirs == 1.4.4",
"cmapy == 0.6.6", # TODO(ecyoung3): Replace with own implementation
"matplotlib == 3.8.*",
"numpy == 1.26.*",
"opencv-python == 4.9.0.*",
"PyQt6 == 6.7.*",
Expand Down
16 changes: 6 additions & 10 deletions requirements-dev.txt
Original file line number Diff line number Diff line change
@@ -1,45 +1,41 @@
# This file was autogenerated by uv via the following command:
# uv pip compile pyproject.toml -o requirements-dev.txt --extra dev
appdirs==1.4.4
cmapy==0.6.6
colorama==0.4.6
# via pytest
contourpy==1.1.1
contourpy==1.2.1
# via matplotlib
cycler==0.12.1
# via matplotlib
exceptiongroup==1.2.1
# via pytest
fonttools==4.43.1
fonttools==4.51.0
# via matplotlib
iniconfig==2.0.0
# via pytest
kiwisolver==1.4.5
# via matplotlib
matplotlib==3.8.0
# via cmapy
matplotlib==3.8.4
mypy==1.10.0
mypy-extensions==1.0.0
# via mypy
numpy==1.26.1
# via
# cmapy
# contourpy
# matplotlib
# opencv-python
# pyqtgraph
# scipy
opencv-python==4.9.0.80
# via cmapy
packaging==23.2
# via
# matplotlib
# pytest
pillow==10.1.0
pillow==10.3.0
# via matplotlib
pluggy==1.5.0
# via pytest
pyparsing==3.1.1
pyparsing==3.1.2
# via matplotlib
pyqt6==6.7.0
pyqt6-qt6==6.7.0
Expand All @@ -48,7 +44,7 @@ pyqt6-sip==13.6.0
# via pyqt6
pyqtgraph==0.13.3
pytest==8.2.0
python-dateutil==2.8.2
python-dateutil==2.9.0.post0
# via matplotlib
ruff==0.4.3
scipy==1.13.0
Expand Down
18 changes: 7 additions & 11 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,40 +1,36 @@
# This file was autogenerated by uv via the following command:
# uv pip compile pyproject.toml -o requirements.txt
appdirs==1.4.4
cmapy==0.6.6
contourpy==1.1.1
contourpy==1.2.1
# via matplotlib
cycler==0.12.1
# via matplotlib
fonttools==4.43.1
fonttools==4.51.0
# via matplotlib
kiwisolver==1.4.5
# via matplotlib
matplotlib==3.8.0
# via cmapy
matplotlib==3.8.4
numpy==1.26.1
# via
# cmapy
# contourpy
# matplotlib
# opencv-python
# pyqtgraph
# scipy
opencv-python==4.9.0.80
# via cmapy
packaging==23.2
packaging==24.0
# via matplotlib
pillow==10.1.0
pillow==10.3.0
# via matplotlib
pyparsing==3.1.1
pyparsing==3.1.2
# via matplotlib
pyqt6==6.7.0
pyqt6-qt6==6.7.0
# via pyqt6
pyqt6-sip==13.6.0
# via pyqt6
pyqtgraph==0.13.3
python-dateutil==2.8.2
python-dateutil==2.9.0.post0
# via matplotlib
scipy==1.13.0
six==1.16.0
Expand Down
28 changes: 19 additions & 9 deletions src/frheed/image_processing.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
Assorted image processing operations.
"""

import cmapy
import cv2
import numpy as np
from matplotlib import pyplot as plt
Expand Down Expand Up @@ -42,19 +41,18 @@ def normalize(arr: np.ndarray) -> np.ndarray:
return arr.astype(np.uint8, copy=True)


def apply_cmap(arr: np.ndarray, cmap: str) -> np.ndarray:
def apply_cmap(arr: np.ndarray, cmap_name: str, bgr_order: bool = False) -> np.ndarray:
"""
Apply a named colormap to an array. This function uses the cmapy library
to convert matplotlib colormaps to cv2 colormaps, since cv2.applyColormap is
approximately 5x faster than using matplotlib/numpy methods
(tested using uint8 2048 x 1536 arrays, ~30ms vs ~6ms).
Apply a named colormap to an array.

Parameters
----------
arr : np.ndarray
The array to apply the colormap to. The array must be single-channel (not RGB).
cmap : str
The name of the colormap to apply (any valid matplotlib colormap).
rgbA_order : bool
Whether the provided array is in BGR order instead of RGB order.

Returns
-------
Expand All @@ -65,8 +63,20 @@ def apply_cmap(arr: np.ndarray, cmap: str) -> np.ndarray:
and width of the input array.

"""
colorized_arr: np.ndarray = cmapy.colorize(normalize(arr), cmap, rgb_order=True)
return colorized_arr
cmap = plt.get_cmap(cmap_name, 256)
rgba_data = plt.cm.ScalarMappable(cmap=cmap).to_rgba(np.arange(0, 1, 1 / 256), bytes=True)
rgba_data = rgba_data[:, 0:-1].reshape((256, 1, 3))

# Convert to 3-channel RGB/BGR uint8 for OpenCV
cmap_data = np.zeros((256, 1, 3), np.uint8)

# Remove the alpha channel and optionally reverse RGB to BGR
if bgr_order:
cmap_data[:, :, :] = rgba_data[:, :, ::-1]
else:
cmap_data[:, :, :] = rgba_data[:, :, :]

return cv2.applyColorMap(arr, cmap_data)


def to_grayscale(array: np.ndarray) -> np.ndarray:
Expand Down Expand Up @@ -124,4 +134,4 @@ def extend_image(image: np.ndarray, new_col: np.ndarray) -> np.ndarray:


def get_valid_colormaps() -> list[str]:
return plt.colormaps()
return list(plt.colormaps())