简体   繁体   中英

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.

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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