Last active
July 25, 2022 06:14
-
-
Save CCXXXI/629092fe0ba515b1755899ec33862d79 to your computer and use it in GitHub Desktop.
Some useful functions with opencv.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import cv2 | |
| import matplotlib.pyplot as plt | |
| import numpy as np | |
| import numpy.typing as npt | |
| from imutils import opencv2matplotlib | |
| from opencv_utils import cvt_single_color | |
| def show(image: npt.NDArray, contours=None): | |
| """ | |
| Show image for debugging. | |
| :param image: The image to show. | |
| :param contours: Optional contours. | |
| """ | |
| if contours is None: | |
| to_show = image | |
| else: | |
| to_show = image.copy() | |
| cv2.drawContours( | |
| to_show, contours, -1, _get_contour_color(image), image.shape[0] // 100 | |
| ) | |
| plt.figure() | |
| plt.imshow(opencv2matplotlib(to_show)) | |
| def _get_contour_color(image: npt.NDArray) -> tuple[int, int, int]: | |
| """ | |
| Get a proper color for the contour. | |
| :param image: The image. | |
| :return: A BGR color for the contour. | |
| """ | |
| # get hsv of center pixel | |
| h, w = image.shape[:2] | |
| center = image[h // 2, w // 2] | |
| hsv = cvt_single_color(center, cv2.COLOR_BGR2HSV) | |
| # use red contour for green image | |
| if np.all((40, 40, 40) <= hsv) and np.all(hsv <= (70, 255, 255)): | |
| return 0, 0, 255 | |
| # green for others | |
| return 0, 255, 0 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import itertools | |
| import os | |
| import shutil | |
| import tempfile | |
| import cv2 | |
| import numpy as np | |
| def cvt_single_color(src, code, np_type=np.uint8): | |
| """ | |
| Single color conversion. | |
| :param src: The source color. | |
| :param code: Something like cv2.COLOR_BGR2HSV. | |
| :param np_type: np.uint8 or np.float32 or something else. | |
| :return: The converted color. | |
| """ | |
| return cv2.cvtColor(np_type([[src]]), code)[0][0] | |
| def cv_read(path, *args): | |
| """ | |
| Read from a path with Unicode characters. | |
| :param path: path of a single image or a directory which contains images | |
| :param args: other args passed to cv2.imread | |
| :return: a single image or a list of images | |
| """ | |
| with tempfile.TemporaryDirectory() as tmp_dir: | |
| if os.path.isdir(path): | |
| shutil.copytree(path, tmp_dir, dirs_exist_ok=True) | |
| elif os.path.isfile(path): | |
| shutil.copy(path, tmp_dir) | |
| else: | |
| raise FileNotFoundError | |
| img_arr = [ | |
| cv2.imread(os.path.join(tmp_dir, img), *args) for img in os.listdir(tmp_dir) | |
| ] | |
| return img_arr if os.path.isdir(path) else img_arr[0] | |
| def show_images(images, *, delay=200, cycle=False): | |
| """Show images as slides.""" | |
| for img in itertools.cycle(images) if cycle else images: | |
| cv2.imshow("", img) | |
| cv2.waitKey(delay) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment