Determining the appropriate color range on a mask

46 Views Asked by At

my task is to find the orange cube that is on the right without intersecting with the red one
my task is to find the orange cube that is on the right without intersecting with the red one

The view from camera.
The view from camera.

I try to find orange cube wit this color range.
I try to find orange cube wit this color range.

This is my code:

import cv2
import numpy as np

if __name__ == '__main__':
    def nothing(*arg):
        passcv2.namedWindow("result")  # создаем главное окноcv2.namedWindow("settings")  # создаем окно настроекcap = cv2.VideoCapture(0)
# создаем 6 бегунков для настройки начального и конечного цвета фильтраcv2.createTrackbar('h1', 'settings', 0, 255, nothing)
cv2.createTrackbar('s1', 'settings', 0, 255, nothing)
cv2.createTrackbar('v1', 'settings', 0, 255, nothing)
cv2.createTrackbar('h2', 'settings', 255, 255, nothing)
cv2.createTrackbar('s2', 'settings', 255, 255, nothing)
cv2.createTrackbar('v2', 'settings', 255, 255, nothing)
crange = [0, 0, 0, 0, 0, 0]

while True:
    flag, img = cap.read()
    hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)

    # считываем значения бегунков    h1 = cv2.getTrackbarPos('h1', 'settings')
    s1 = cv2.getTrackbarPos('s1', 'settings')
    v1 = cv2.getTrackbarPos('v1', 'settings')
    h2 = cv2.getTrackbarPos('h2', 'settings')
    s2 = cv2.getTrackbarPos('s2', 'settings')
    v2 = cv2.getTrackbarPos('v2', 'settings')

    # формируем начальный и конечный цвет фильтра    h_min = np.array((h1, s1, v1), np.uint8)
    h_max = np.array((h2, s2, v2), np.uint8)

    # накладываем фильтр на кадр в модели HSV    thresh = cv2.inRange(hsv, h_min, h_max)

    cv2.imshow('result', thresh)

    ch = cv2.waitKey(5)
    if ch == 27:
        breakcap.release()
cv2.destroyAllWindows()````
0

There are 0 best solutions below