简体   繁体   English

openCV 中的 HSV 颜色范围

[英]HSV colour range in openCV

I wrote a program that used trackbars, to find out the appropriate HSV values (range) for segmenting out the white lines from the image.我编写了一个使用轨迹栏的程序,以找出合适的 HSV 值(范围)以从图像中分割出白线。

For a long time this seemed like the best shot:很长一段时间,这似乎是最好的镜头: 在此处输入图像描述

But its still not very accurate, its leaving out chunks of the line...但它仍然不是很准确,它遗漏了大块的线......

After messing around some more, I realised something:又折腾了一阵子,我才意识到:

在此处输入图像描述

This is very accurate: apart from the fact that the black and white regions are swapped.这是非常准确的:除了黑色和白色区域交换的事实。

Is there any way to invert this colour scheme to swap the black and white regions?有什么办法可以反转这种配色方案来交换黑白区域? If not, what exactly can I do to not leave out chunks of the line like the first image...I have tried out various HSV combinations and it seems like this is the closest I can get.如果没有,我究竟能做些什么才能不遗漏第一张图片中的大块线条……我已经尝试了各种 HSV 组合,这似乎是我能得到的最接近的组合。

code:代码:

import cv2 as cv
import numpy as np




def nothing(x):
    pass


img= cv.imread("ti2.jpeg")



cv.namedWindow("image")  #create a window that will contain the trackbars for HSV values

cv.createTrackbar('HMin','image',0,179,nothing) 
cv.createTrackbar('SMin','image',0,255,nothing)
cv.createTrackbar('VMin','image',0,255,nothing)
cv.createTrackbar('HMax','image',0,179,nothing)
cv.createTrackbar('SMax','image',0,255,nothing)
cv.createTrackbar('VMax','image',0,255,nothing)


cv.setTrackbarPos('HMax', 'image', 179) #setting default trackbar pos for max HSV values at max
cv.setTrackbarPos('SMax', 'image', 255)
cv.setTrackbarPos('VMax', 'image', 255)

while True:

    hMin = cv.getTrackbarPos('HMin','image') #get the current slider position
    sMin = cv.getTrackbarPos('SMin','image')
    vMin = cv.getTrackbarPos('VMin','image')

    hMax = cv.getTrackbarPos('HMax','image')
    sMax = cv.getTrackbarPos('SMax','image')
    vMax = cv.getTrackbarPos('VMax','image')
    
    hsv=cv.cvtColor(img, cv.COLOR_BGR2HSV)
    
    lower=np.array([hMin,sMin,vMin])
    upper=np.array([hMax,sMax,vMax])

    mask=cv.inRange(hsv,lower,upper)
    


    #result=cv.bitwise_and(frame,frame,mask=mask)
    
    cv.imshow("img",img)
    cv.imshow("mask",mask)
    #cv.imshow("result",result)
    
    k=cv.waitKey(1)
    
    if k==27 :
        break
       
cv.destroyAllWindows()

Test Image:测试图像:

在此处输入图像描述

To invert the mask反转蒙版

mask = 255-mask # if mask is a uint8 which ranges 0 to 255
mask = 1-mask # if mask is a bool which is either 0 or 1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM