OpenCV quick start four: TrackBar control (slider)

1: Function API

1:createTrackbar

int createTrackbar(
					const String &trackbarname,    #轨迹条的名字
					const String &winname,         #窗口名字。窗口得存在才能创建滑动条
					int* value,                    #滑块的初始值
					int count,                     #滑块的最大值(最小位置的值始终为0)
					TrackbarCallback onChange=0,   #回调函数
					void* userdata=0               #用户传给回调函数的数据
);

2:setTrackbarPos

setTrackbarPos	(
				 const String &trackbarname,   	  #需要设置trackbar的名称,与创建的名称相对应
				 const String &winname,       	  #trackbar所在窗体的名称
				 int pos                      	  #该trackbar的值
);	

Two: Code demonstration

1: BGR palette

import cv2
import numpy as np

#鼠标回调函数
def mcallback(event,x,y,flags,userdata):
    pass

cv2.namedWindow('trackbar', cv2.WINDOW_AUTOSIZE)
cv2.resizeWindow('trackbar', 640, 480)

#设置鼠标回调
cv2.createTrackbar('R',"trackbar",0,255,mcallback)
cv2.createTrackbar('G',"trackbar",0,255,mcallback)
cv2.createTrackbar('B',"trackbar",0,255,mcallback)

img=np.zeros((480,640,3),np.uint8)#全黑图片
while True:

    r=cv2.getTrackbarPos('R','trackbar')
    b=cv2.getTrackbarPos('B','trackbar')
    g=cv2.getTrackbarPos('G','trackbar')
    #改变颜色
    img[:]=[b,g,r]
    cv2.imshow('trackbar',img)
    key=cv2.waitKey(10)#为0则只显示一帧
    if(key&0xff==ord('q')):
        break

cv2.destroyAllWindows()

If you think the red mark on the terminal is not good-looking, you can change the callback function to: def mcallback(event):

output
insert image description here

2: Specific color area extraction

import cv2
import numpy as np


def mcallback(x):
    pass


cv2.namedWindow("Tracking")
#绿色的HSV:可能不是很准
#l_g = np.array([35, 43, 46]) # 下限
#u_g = np.array([77,255,255]) # 上限
cv2.createTrackbar("LH", "Tracking", 35, 255, mcallback)
cv2.createTrackbar("LS", "Tracking", 43, 255, mcallback)
cv2.createTrackbar("LV", "Tracking", 46, 255, mcallback)
cv2.createTrackbar("UH", "Tracking", 77, 255, mcallback)
cv2.createTrackbar("US", "Tracking", 255, 255, mcallback)
cv2.createTrackbar("UV", "Tracking", 255, 255, mcallback)

while True:
    img = cv2.imread(r"C:\Users\DMr\Pictures\text\book.jpg")
    hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)

    l_h = cv2.getTrackbarPos("LH", "Tracking")
    l_s = cv2.getTrackbarPos("LS", "Tracking")
    l_v = cv2.getTrackbarPos("LV", "Tracking")

    u_h = cv2.getTrackbarPos("UH", "Tracking")
    u_s = cv2.getTrackbarPos("US", "Tracking")
    u_v = cv2.getTrackbarPos("UV", "Tracking")

    l_g = np.array([l_h, l_s, l_v])  # lower green value
    u_g = np.array([u_h, u_s, u_v])

    mask = cv2.inRange(hsv, l_g, u_g)

    res = cv2.bitwise_and(img, img, mask=mask)  # src1,src2

    cv2.imshow("img", img)
    cv2.imshow("mask", mask)
    cv2.imshow("res", res)
    key = cv2.waitKey(10)
    if(key & 0xff == ord('q')):
        break

cv2.destroyAllWindows()

output
color segmentation

Why are the separated channels black, white and gray instead of red, green and blue?
The reason is that it is a single channel after separation, which is equivalent to filling the other two channels with the same value while separating the channel. For example, the red channel, when the red channel is separated, green and blue are filled with the same value as red, so that there are only black, white and gray.
So where is green? You can observe, and you will find that the closer the color is to green in the original image, the closer it is to white in the green channel.
Where there is pure green there will be pure white in the green channel.
The G value is 255 -> BGR (255, 255, 255), which is pure white

I will make up the hsv study notes for everyone when I find time. Recently, I learned five in the internship OpenCV: color space conversion . 2022/8/2 15:51

Daily "big cake":
May everyone follow their own clock and make choices without regret

Guess you like

Origin blog.csdn.net/weixin_52051554/article/details/126018160