SoFunction
Updated on 2024-10-29

OpenCV basic HSV color space *args vs **kwargs slider pass parameter problem

I. Basic Theory

HSV: HSV is a concept designed to speed up color mixing efficiency and is easy to understand.

Hue: Hue (specific color)

Saturation: Saturation, color purity

Value: Brightness

1. Hue (color phase)

Hue: Hue (specific color)

2. Value (brightness)

Brightness: how bright the color is, single channel brightness (not equal to the overall amount of light emitted).

(The higher the luminance the whiter, the lower the blacker, generally increase the luminance will increase the value of R, G, B channel at the same time)

3、Saturation(saturation)

Saturation: Saturation, color purity. (the lower the more gray, the higher the more pure)

(Generally cranking up saturation lowers the relatively low values in RGB, bringing out the purity of the main colors. )

B-site video explanation:

Short animation slow speech speed 1 minute to speak clear film and television color mixing in the principle of color formation basis - RGB and HSV

Second, hsv three-channel and single-channel effect

III. *args && **kwargs

*args: The incoming parameter is unknown and the name of the parameter does not need to be known.

**args: The incoming parameter is unknown, but the name of the parameter needs to be known.

IV. Scroll bar control h, s, v (min & & max)

1、Create a scroll bar

API

CV_EXPORTS int createTrackbar(const string& trackbarname, const string& winname,
                              int* value, int count,
                              TrackbarCallback onChange = 0,
                              void* userdata = 0);

Formal parameter Itrackbarname: The name of the sliding space;

Formal parameter IIwinname: The name of the image window that the sliding space is used to depend on;

Formal parameter IIIvalue: Initialization threshold;

Formal parameter IVcount: The scale range of the slide control;

Formal parameter VTrackbarCallback: is a callback function, which is defined as follows

typedef void (CV_CDECL *TrackbarCallback)(int pos, void* userdata);
# 3. Creating h, s, and v scrollbars
    ('hmin', 'h', 12, 179, Renew)
    ('hmax', 'h', 37, 179, Renew)
    ('smin', 's', 12, 179, Renew)
    ('smax', 's', 37, 179, Renew)
    ('vmin', 'v', 12, 179, Renew)
    ('vmax', 'v', 37, 179, Renew)

2. Callback function -- threshold setting

API

inRange()

The main purpose is to set the pixel values that are within the two thresholds to white (255) and the pixel values that are not within the threshold interval to black (0), this function is similar to the double thresholding operation described between.

    void inRange(InputArray src, InputArray lowerb,
                              InputArray upperb, OutputArray dst);

Parameter 1: Input the image to be processed, which can be single or multi-channel.

Parameter 2: An array or scalar containing the lower boundary.

Parameter 3: Contains the upper boundary array or scalar.

Parameter 4: output image, same size as input image src and of type CV_8U.

(Note: dst outputs the image after binarization)

# 1. Getting slider feedback values
    hmin = ('hmin', 'h')
    hmax = ('hmax', 'h')
    smin = ('smin', 's')
    smax = ('smax', 's')
    vmin = ('vmin', 'v')
    vmax = ('vmax', 'v')
 
    # 2, set the threshold (inRange: within the threshold (min,max), set to white; outside the threshold, set to black)
    h_thresh = ((h), (hmin), (hmax))
    s_thresh = ((s), (smin), (smax))
    v_thresh = ((v), (vmin), (vmax))

3. Callback functions -- values of interest

API

bitwise_and()

The image sum operation is mainly used to obtain the part of interest in a particular image, and is a bitwise sum for two image matrix arrays or an array and a scalar.

# 3. Getting binary values of interest (with operations)
    interest = cv2.bitwise_and(h_thresh, cv2.bitwise_and(s_thresh, v_thresh))

master code

# HSV color space with sliders (*args && **args)
import cv2
import numpy as np
# Callback functions
# *args: the incoming parameters are unknown and the names of the parameters are not required to be known
# **args: incoming parameters are unknown, but need to know the parameter names
def HSV_CallBack(*args):
    # 1. Getting slider feedback values
    hmin = ('hmin', 'h_binary')
    hmax = ('hmax', 'h_binary')
    smin = ('smin', 's_binary')
    smax = ('smax', 's_binary')
    vmin = ('vmin', 'v_binary')
    vmax = ('vmax', 'v_binary')
    # 2, set the threshold (inRange: within the threshold (min,max), set to white; outside the threshold, set to black)
    h_binary = ((h), (hmin), (hmax))
    s_binary = ((s), (smin), (smax))
    v_binary = ((v), (vmin), (vmax)) 
    # 3. Getting binary values of interest (with operations)
    binary = cv2.bitwise_and(h_binary, cv2.bitwise_and(s_binary, v_binary)) 
    # 4, show
    ('h_binary', h_binary)
    ('s_binary', s_binary)
    ('v_binary', v_binary)
    ('binary', binary) 
def Show_HSV():
    global hsv, h, s, v
    # 0, create a window
    ('h_binary')
    ('s_binary')
    ('v_binary')
    # 1, get hsv picture
    hsv = (img, cv2.COLOR_RGB2HSV)
    ('hsv', hsv)
    # 2, get h, s, v three-channel picture
    h, s, v = (hsv)
    # 3. Creating h, s, and v scrollbars
    ('hmin', 'h_binary', 12, 179, HSV_CallBack)
    ('hmax', 'h_binary', 37, 179, HSV_CallBack)
    ('smin', 's_binary', 12, 179, HSV_CallBack)
    ('smax', 's_binary', 37, 179, HSV_CallBack)
    ('vmin', 'v_binary', 12, 179, HSV_CallBack)
    ('vmax', 'v_binary', 37, 179, HSV_CallBack)
    HSV_CallBack()
if __name__ == '__main__':
    global img
    img = ('Resource/')
    ('img', img)
    # Show h, s, v
    Show_HSV() 
    (0)

bibliography

Short animation slow speech speed 1 minute clear film and television color mixing in the principle of color formation basis - RGB and HSV_Beili_bilibili

​​​Python programming *args and **kwargs difference in the role of detail

How to use createTrackbar?

The above is OpenCV basic HSV color space *args and **args slider passing parameter problem in detail, more about HSV color space *args and **args slider passing parameter information please pay attention to my other related articles!