按颜色提取图像区域

import cv2
import numpy as np


def split_color(path):
    img = cv2.imread(path)
    hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
    # range of red
    lower_red = np.array([160, 60, 60])
    upper_red = np.array([180, 255, 255])
    lower_red2 = np.array([0, 60, 60])
    upper_red2 = np.array([10, 255, 255])  # thers is two ranges of red
    mask_r = cv2.inRange(hsv, lower_red, upper_red)
    mask_r2 = cv2.inRange(hsv, lower_red2, upper_red2)
    red = mask_r + mask_r2
    lower_green = np.array([35, 60, 60])
    upper_green = np.array([77, 255, 255])
    green = cv2.inRange(hsv, lower_green, upper_green)
    lower_blue = np.array([100, 60, 60])
    upper_blue = np.array([124, 255, 255])
    blue = cv2.inRange(hsv, lower_blue, upper_blue)
    return red,green,blue

red,green,blue = split_color(f"download/{file}/ortho/ortho.jpg")

思路:RGB转HSV,按照HSV颜色表提取即可。

HSV表见:https://blog.csdn.net/u013270326/article/details/80704754

你可能感兴趣的:(编程)