Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
962 views
in Technique[技术] by (71.8m points)

image processing - python opencv HSV range finder creating trackbars

I want to find the HSV value of a LASER dot using opencv and python. I got the code http://opencv-srf.blogspot.com.au/2010/09/object-detection-using-color-seperation.html from here but it is in c++, installing visual studio and opencv takes time so i changed the code in python

import cv2
import numpy as np

def callback(x):
pass

cap = cv2.VideoCapture(0)
cv2.namedWindow('image')

ilowH = 0
ihighH = 179

ilowS = 0
ihighS = 255
ilowV = 0
ihighV = 255

# create trackbars for color change
cv2.createTrackbar('lowH','image',ilowH,179,callback)
cv2.createTrackbar('highH','image',ihighH,179,callback)

cv2.createTrackbar('lowS','image',ilowS,255,callback)
cv2.createTrackbar('highS','image',ihighS,255,callback)

cv2.createTrackbar('lowV','image',ilowV,255,callback)
cv2.createTrackbar('highV','image',ihighV,255,callback)



while(1):
 ret, frame = cap.read()
 hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
 cv2.imshow('hsv', hsv)
 lower_hsv = np.array([ilowH, ilowS, ilowV])
 higher_hsv = np.array([ihighH, ihighS, ihighV])
 mask = cv2.inRange(hsv, lower_hsv, higher_hsv)
 cv2.imshow('mask', mask)
 cv2.imshow('frame', frame)
 print ilowH, ilowS, ilowV
 if(cv2.waitKey(1) & 0xFF == ord('q')):
    break


cv2.destroyAllWindows()
cap.release()

but this code doesnot threshold anything. It seems like the trackbars i created doesnot change the value of ilowH ,ilowS, ilowV . I checked it by printing those values inside while loop. What could be the problem for not thresholding any of those values or is there better code in python to find HSV values of the LASER. Thank you, any help is appreciated.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can grab the trackbar values with cv2.getTrackbarPos(). Also note that sometimes it puts trackbars out of order, which is annoying, but at least they're labeled.

However, I don't think that these trackbars will work very well for live video feed. There's a lot of freezing issues. You'll have to have a super low framerate (works for me with cv2.waitKey(500) if you're actually trying to display it). This is mostly due to the trackbars sucking, not the thresholding operation, which is not that slow.

You need to add your trackbars after you create the named window. Then, for your while loop, try:

while True:
    # grab the frame
    ret, frame = cap.read()

    # get trackbar positions
    ilowH = cv2.getTrackbarPos('lowH', 'image')
    ihighH = cv2.getTrackbarPos('highH', 'image')
    ilowS = cv2.getTrackbarPos('lowS', 'image')
    ihighS = cv2.getTrackbarPos('highS', 'image')
    ilowV = cv2.getTrackbarPos('lowV', 'image')
    ihighV = cv2.getTrackbarPos('highV', 'image')

    hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
    lower_hsv = np.array([ilowH, ilowS, ilowV])
    higher_hsv = np.array([ihighH, ihighS, ihighV])
    mask = cv2.inRange(hsv, lower_hsv, higher_hsv)

    frame = cv2.bitwise_and(frame, frame, mask=mask)

    # show thresholded image
    cv2.imshow('image', frame)
    k = cv2.waitKey(1000) & 0xFF # large wait time to remove freezing
    if k == 113 or k == 27:
        break

and finally end the file with a cv2.destroyAllWindows()

As an aside, the maximum H value for HSV is 180, not 179.

Shameless plug: I happened to just finish a project doing precisely this, but on images. You can grab it on GitHub here. There is an example; try running it and then modifying as you need. It will let you change the colorspace and threshold inside each different colorspace, and it will print the final thresholding values that you ended on. Additionally it will return the output image from the operation for you to use, too. Hopefully it is useful for you! Feel free to send any issues or suggestions through GitHub for the project.

Here is an example of it running:

Example of cspaceThresh running

And as output it gives you:

Colorspace: HSV 
Lower bound: [68.4, 0.0, 0.0] 
Upper bound: [180.0, 255.0, 255.0]

as well as the binary image. I am currently working on getting this into a web application as well, but that probably won't be finished for a few days.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

56.7k users

...