Target Tracking and Color Recognition (How inRange Finds Target HSV Value)

Keywords: less OpenCV

What can the inRange function do?

Target tracking and color recognition.

In the process of inRange application, it is important to find the appropriate HSV value.

RGB is the most widely heard color space, which is also the natural representation of computer color, but for human beings, HSV color space is more in line with human perception. HSV (color saturation value):

H (Hue): Tone, the range of values is [0,179], which is used to limit the spectral range of a color.

S (Saturation): Saturation, the range of values is [0,255], which is used to limit the depth of the color, the larger the value, the darker the color;

V (Value): The range of values is [0,255], which is used to limit the brightness of the pixel. The larger the value, the brighter the pixel is.

So how to find the appropriate color tracking threshold?

First read a picture to be tracked:

import cv2
import imutils

frame = cv2.imread("UAV.jpg")
frame = imutils.resize(frame, width=320)  #Adjust the image size
cv2.imshow('frame', frame)
cv2.waitKey(0)

Description: cv2.waitKey(0) is a keyboard binding function. If this function is not used, the imshow image will flash back (or not show). When outputting a camera or video, a parameter greater than 0 should be specified. cv2.waitKey(10): Wait 10 milliseconds to play the next frame.

You can see the lower left corner of the image with RGB values showing corresponding coordinates and corresponding coordinates. Red and blue cards are selected as targets to identify and track UAV. The purpose of two cards is to mark the positive and negative pitch of UAV, or the head and tail of UAV. At the same time, the angle between UAV and target point can be obtained, so yaw can be controlled.

Then the mouse is placed on the red and blue cardboard to get the corresponding RGB value and record it. (Red: 251, 136, 191) Blue: 153, 217, 211)

The corresponding HSV is calculated.

import cv2
import numpy as np

color=np.uint8([[[191 ,136 ,251]]])
hsv_color=cv2.cvtColor(color,cv2.COLOR_BGR2HSV)

print(hsv_color)

Explanation: opencv reads pictures in BGR format, so input the RGB value backwards.

Get HSV: Red: 166, 117, 251 Blue: 87, 75, 217

We can use [H-50 50 50 50] and [H+50 255 255] to do the upper and lower thresholds respectively (adjusting according to the actual situation, the range of less background interference can be relaxed, the range of more background interference can be restricted strictly, but we should ensure that the obtained HSV is within the upper and lower thresholds).

import cv2
import numpy as np
import imutils


frame = cv2.imread("UAV.jpg")
frame = imutils.resize(frame, width=320)
hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)

mask_red = cv2.inRange(hsv, np.array((110., 50., 50)), np.array((180., 255., 255.)))
mask_red = cv2.medianBlur(mask_red, 3)
cnts_red = cv2.findContours(mask_red.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cnts_red = cnts_red[0] if imutils.is_cv2() else cnts_red[1]

mask_blue = cv2.inRange(hsv, np.array((40., 50., 50.)), np.array((140., 255., 255.)))
mask_blue = cv2.medianBlur(mask_blue, 3)
cnts_blue = cv2.findContours(mask_blue.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cnts_blue = cnts_blue[0] if imutils.is_cv2() else cnts_blue[1]

if (len(cnts_red) > 0):
    area = [cv2.contourArea(i) for i in cnts_red]
    index = np.argmax(area)
    rect_red = cv2.minAreaRect(cnts_red[index])
    box_red = np.int0(cv2.boxPoints(rect_red))
    cv2.drawContours(frame, [box_red], 0, (0, 0, 255), 2)

if (len(cnts_blue) > 0):
    area = [cv2.contourArea(i) for i in cnts_blue]
    index = np.argmax(area)
    rect_blue = cv2.minAreaRect(cnts_blue[index])
    box_blue = np.int0(cv2.boxPoints(rect_blue))
    cv2.drawContours(frame, [box_blue], 0, (0, 0, 255), 2)

cv2.imshow('frame', frame)
cv2.imshow('mask_red', mask_red)
cv2.imshow('mask_blue', mask_blue)

cv2.waitKey(0)



As you can see, because the single chip computer is dark red, it is disturbed when extracting red, so it needs to be narrowed down a little. The dark red H value of MCU is 177, so I changed mask_red to cv2. inRange (hsv, np. array ((110, 50, 50)), np. array ((170., 255, 255.)).

At this point, the object of the test is still a static image. When the camera is turned on for real-time tracking, it will be affected by light. The RGB value varies greatly, and the range of HSV needs to be adjusted again. And when choosing the target tracking color, the color with less background interference should be selected. There is also a HSV range table available on the Internet, which I use quite well.

 

Other applications:

The camera recognizes the yellow bar code and saves the picture:

Source download:

 

 

 

 

 

Posted by alvinho on Fri, 23 Aug 2019 06:54:08 -0700