extract more than one colored object, for example, extract red, blue, and green objects simultaneous

前言

题目地址:https://docs.opencv.org/4.x/df/d9d/tutorial_py_colorspaces.html
题目内容:
Try to find a way to extract more than one colored object, for example, extract red, blue, and green objects simultaneously.

Source Code

# 开发时间:2022/2/21  21:18
import cv2 as cv
import numpy as np

cap = cv.VideoCapture(0)

while True:
    # 读取帧
    _, frame = cap.read()
    # 转换颜色空间 BGR 到 HSV
    hsv = cv.cvtColor(frame, cv.COLOR_BGR2HSV)
    # 定义HSV中蓝色的范围
    lower_blue = np.array([106, 43, 46])
    upper_blue = np.array([130, 255, 255])
    # 设置HSV的阈值只取蓝色
    mask_B = cv.inRange(hsv, lower_blue, upper_blue)

    # 定义HSV中绿色的范围
    lower_green = np.array([35, 43, 46])
    upper_green = np.array([77, 255, 255])
    # 设置HSV的阈值只取绿色
    mask_G = cv.inRange(hsv, lower_green, upper_green)

    # 定于HSV中红色的范围
    lower_red1 = np.array([0, 43, 46])
    upper_red1 = np.array([20, 255, 255])
    # 设置HSV的阈值只取红色
    mask_R1 = cv.inRange(hsv, lower_red1, upper_red1)

    # 注意红色有两个色域
    lower_red2 = np.array([150, 43, 46])
    upper_red2 = np.array([180, 255, 255])
    mask_R2 = cv.inRange(hsv, lower_red2, upper_red2)

    mask=mask_B+mask_G+mask_R1+mask_R2
    #mask = mask_R1 + mask_R2

    #	dst	=	cv.inRange(	src, lowerb, upperb[, dst]	)

    # 按位与掩码和原始图像
    res = cv.bitwise_and(frame, frame, mask=mask)
    # dst	=	cv.bitwise_and(	src1, src2[, dst[, mask]]	)
    # 调整OpenCV弹出窗口大小:参考博客https://blog.csdn.net/m0_37303351/article/details/78944904
    cv.namedWindow("frame", 0)
    cv.namedWindow("mask", 0)
    cv.namedWindow("res", 0)
    cv.imshow('frame', frame)
    cv.imshow('mask', mask)
    cv.imshow('res', res)
    k = cv.waitKey(5) & 0xFF
    if k == 27:
        break
cv.destroyAllWindows()

Result

result

总结

参考代码地址:
https://docs.opencv.org/4.x/df/d9d/tutorial_py_colorspaces.html
调整opencv弹出窗口的大小

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
# Single Color Code Tracking Example # # This example shows off single color code tracking using the CanMV Cam. # # A color code is a blob composed of two or more colors. The example below will # only track colored objects which have both the colors below in them. import sensor, image, time, math # Color Tracking Thresholds (L Min, L Max, A Min, A Max, B Min, B Max) # The below thresholds track in general red/green things. You may wish to tune them... thresholds = [(30, 100, 15, 127, 15, 127), # generic_red_thresholds -> index is 0 so code == (1 << 0) (30, 100, -64, -8, -32, 32)] # generic_green_thresholds -> index is 1 so code == (1 << 1) # Codes are or'ed together when "merge=True" for "find_blobs". sensor.reset() sensor.set_pixformat(sensor.RGB565) sensor.set_framesize(sensor.QVGA) sensor.skip_frames(time = 2000) sensor.set_auto_gain(False) # must be turned off for color tracking sensor.set_auto_whitebal(False) # must be turned off for color tracking clock = time.clock() # Only blobs that with more pixels than "pixel_threshold" and more area than "area_threshold" are # returned by "find_blobs" below. Change "pixels_threshold" and "area_threshold" if you change the # camera resolution. "merge=True" must be set to merge overlapping color blobs for color codes. while(True): clock.tick() img = sensor.snapshot() for blob in img.find_blobs(thresholds, pixels_threshold=100, area_threshold=100, merge=True): if blob.code() == 3: # r/g code == (1 << 1) | (1 << 0) # These values depend on the blob not being circular - otherwise they will be shaky. # if blob.elongation() > 0.5: # img.draw_edges(blob.min_corners(), color=(255,0,0)) # img.draw_line(blob.major_axis_line(), color=(0,255,0)) # img.draw_line(blob.minor_axis_line(), color=(0,0,255)) # These values are stable all the time. img.draw_rectangle(blob.rect()) img.draw_cross(blob.cx(), blob.cy()) # Note - the blob rotation is unique to 0-180 only. img.draw_keypoints([(blob.cx(), blob.cy(), int(math.degrees(blob.rotation())))], size=20) print(clock.fps())
07-10
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

布兹学长

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值