欢迎光临散文网 会员登陆 & 注册

图像处理颜色阈值(蓝色分割)

2023-08-14 14:52 作者:一帘幽梦叹清魂  | 我要投稿

HSV代码

import CV2

import numpy as np


def on_trackbar(val):

    pass


# 创建滑动条窗口

CV2.namedWindow("Threshold Adjustments")


# 读取原图

image = CV2.imread("C:/Users/0.jpg")


# 获得图像的尺寸

height, width, _ = image.shape


# 将图像转换为HSV颜色空间

hsv_image = CV2.cvtColor(image, CV2.COLOR_BGR2HSV)


# 设定初始蓝色的阈值范围

init_lower_blue = [0, 0, 0]

init_upper_blue = [255, 255, 255]


# 创建滑动条,设置回调函数

CV2.createTrackbar("Lower H", "Threshold Adjustments", init_lower_blue[0], 179, on_trackbar)

CV2.createTrackbar("Lower S", "Threshold Adjustments", init_lower_blue[1], 255, on_trackbar)

CV2.createTrackbar("Lower V", "Threshold Adjustments", init_lower_blue[2], 255, on_trackbar)

CV2.createTrackbar("Upper H", "Threshold Adjustments", init_upper_blue[0], 179, on_trackbar)

CV2.createTrackbar("Upper S", "Threshold Adjustments", init_upper_blue[1], 255, on_trackbar)

CV2.createTrackbar("Upper V", "Threshold Adjustments", init_upper_blue[2], 255, on_trackbar)


while True:

    # 获取滑动条的当前值

    lower_hue = CV2.getTrackbarPos("Lower H", "Threshold Adjustments")

    lower_saturation = CV2.getTrackbarPos("Lower S", "Threshold Adjustments")

    lower_value = CV2.getTrackbarPos("Lower V", "Threshold Adjustments")

    upper_hue = CV2.getTrackbarPos("Upper H", "Threshold Adjustments")

    upper_saturation = CV2.getTrackbarPos("Upper S", "Threshold Adjustments")

    upper_value = CV2.getTrackbarPos("Upper V", "Threshold Adjustments")


    # 设定蓝色的阈值范围

    lower_blue = np.array([lower_hue, lower_saturation, lower_value])

    upper_blue = np.array([upper_hue, upper_saturation, upper_value])


    # 根据阈值范围分割图像

    mask = CV2.inRange(hsv_image, lower_blue, upper_blue)

    result = CV2.bitwise_and(image, image, mask=mask)


    # 显示原图和结果图像

    CV2.imshow("Original Image", image)

    CV2.imshow("Result Image", result)


    # 按下ESC键退出循环

    key = CV2.waitKey(1) & 0xFF

    if key == 27:

        break


# 关闭窗口

CV2.destroyAllWindows()

Lab代码

import CV2

import numpy as np


def on_trackbar(val):

    pass


# 创建滑动条窗口

CV2.namedWindow("Threshold Adjustments")


# 读取原图

image = CV2.imread("C:/Users/0.jpg")


# 获得图像的尺寸

height, width, _ = image.shape


# 将图像转换为LAB颜色空间

lab_image = CV2.cvtColor(image, CV2.COLOR_BGR2LAB)


# 设定初始蓝色的阈值范围

init_lower_blue = [0, 0, 0]

init_upper_blue = [255, 255, 255]


# 创建滑动条,设置回调函数

CV2.createTrackbar("Lower L", "Threshold Adjustments", init_lower_blue[0], 255, on_trackbar)

CV2.createTrackbar("Lower A", "Threshold Adjustments", init_lower_blue[1], 255, on_trackbar)

CV2.createTrackbar("Lower B", "Threshold Adjustments", init_lower_blue[2], 255, on_trackbar)

CV2.createTrackbar("Upper L", "Threshold Adjustments", init_upper_blue[0], 255, on_trackbar)

CV2.createTrackbar("Upper A", "Threshold Adjustments", init_upper_blue[1], 255, on_trackbar)

CV2.createTrackbar("Upper B", "Threshold Adjustments", init_upper_blue[2], 255, on_trackbar)


while True:

    # 获取滑动条的当前值

    lower_L = CV2.getTrackbarPos("Lower L", "Threshold Adjustments")

    lower_A = CV2.getTrackbarPos("Lower A", "Threshold Adjustments")

    lower_B = CV2.getTrackbarPos("Lower B", "Threshold Adjustments")

    upper_L = CV2.getTrackbarPos("Upper L", "Threshold Adjustments")

    upper_A = CV2.getTrackbarPos("Upper A", "Threshold Adjustments")

    upper_B = CV2.getTrackbarPos("Upper B", "Threshold Adjustments")


    # 设定蓝色的阈值范围

    lower_blue = np.array([lower_L, lower_A, lower_B])

    upper_blue = np.array([upper_L, upper_A, upper_B])


    # 根据阈值范围分割图像

    mask = CV2.inRange(lab_image, lower_blue, upper_blue)

    result = CV2.bitwise_and(image, image, mask=mask)


    # 显示原图和结果图像

    CV2.imshow("Original Image", image)

    CV2.imshow("Result Image", result)


    # 按下ESC键退出循环

    key = CV2.waitKey(1) & 0xFF

    if key == 27:

        break


# 关闭窗口

CV2.destroyAllWindows()


图像处理颜色阈值(蓝色分割)的评论 (共 条)

分享到微博请遵守国家法律