Handling of HSV color space lower and upper in OpenCV

The advantage of HSV is that you can select the color with lower and upper, but I had a hard time because the lower and upper for the color are unknown, so make a note.

What is HSV

It's like an extension of rgb, and consists of hue (Hue), saturation (Saturation / Chroma), and lightness. What makes me happy is that it is easy to specify the range of colors. The H part corresponds to the rgb part.

Hue-Color type 0-360 range Saturation-Color vividness in the range of 0-100%. Brightness-Color brightness in the range of 0-100%.

The value set in is said to change depending on the software. With opencv, I think the brightness is between 100 and 255.

スクリーンショット 2017-07-05 21.53.25.png

See wiki https://ja.wikipedia.org/wiki/HSV%E8%89%B2%E7%A9%BA%E9%96%93

simulator http://www.rapidtables.com/convert/color/hsv-to-rgb.htm

Convert from RGB to lower, upper

convert.py


import sys
import numpy as np
import cv2
 
#rgb
red = sys.argv[1]
green = sys.argv[2]
blue = sys.argv[3]  
 
color = np.uint8([[[blue, green, red]]])
hsv_color = cv2.cvtColor(color, cv2.COLOR_BGR2HSV)
 
hue = hsv_color[0][0][0]
 
print("Lower bound is :"),
print("[" + str(hue-10) + ", 100, 100]\n")
 
print("Upper bound is :"),
print("[" + str(hue + 10) + ", 255, 255]")

python convert.py 255 255 255

I convert it like this, but this one can only be converted roughly.

Debug HSV lower and upper

hsv.py


import cv2
import numpy as np

image_hsv = None   # global ;(
pixel = (20,60,80) # some stupid default

# mouse callback function
def pick_color(event,x,y,flags,param):
    if event == cv2.EVENT_LBUTTONDOWN:
        pixel = image_hsv[y,x]

        #you might want to adjust the ranges(+-10, etc):
        upper =  np.array([pixel[0] + 10, pixel[1] + 10, pixel[2] + 40])
        lower =  np.array([pixel[0] - 10, pixel[1] - 10, pixel[2] - 40])
        print(pixel, lower, upper)

        image_mask = cv2.inRange(image_hsv,lower,upper)
        cv2.imshow("mask",image_mask)

def main():
    import sys
    global image_hsv, pixel # so we can use it in mouse callback

    image_src = cv2.imread(sys.argv[1])  # pick.py my.png
    if image_src is None:
        print ("the image read is None............")
        return
    cv2.imshow("bgr",image_src)

    ## NEW ##
    cv2.namedWindow('hsv')
    cv2.setMouseCallback('hsv', pick_color)

    # now click into the hsv img , and look at values:
    image_hsv = cv2.cvtColor(image_src,cv2.COLOR_BGR2HSV)
    cv2.imshow("hsv",image_hsv)

    cv2.waitKey(0)
    cv2.destroyAllWindows()

if __name__=='__main__':
    main()
python hsv.py hoge.png

It is masked when it is started and double-clicked on the hsv screen. For example, yellow and green are very similar. Pink and red too. In this case, even though you click pink, red also reacts. In other words, you have to fine-tune the color depth and brightness. I didn't know how to do it automatically, so I played with it manually.

Adjust HSV manually.

hsv_sample.py


import sys
import cv2
import numpy as np

import sys
import cv2
import numpy as np
#yellow
# lower =  np.array([30,100,250])
# upper =  np.array([40,255,255])
#pink
lower =  np.array([160,50,50])
upper =  np.array([180,255,255])
#yellowgreen
# lower =  np.array([30,100,200])
# upper =  np.array([60,255,250])

image_src = cv2.imread(sys.argv[1])
image_src = cv2.cvtColor(image_src,cv2.COLOR_BGR2HSV)
image_src = cv2.inRange(image_src,lower,upper)

while True:
    cv2.imshow("image_src",image_src)

    if cv2.waitKey(10) == 27: # ESC
        break

A very primitive method, but a yellowgreen mask, which is very similar to yellow, also worked.

I haven't done it though. .. ..

Because of this, it may have been possible to automate by displaying it and specifying the chot range.

python


>>> green = np.uint8([[[0,255,0 ]]])
>>> hsv_green = cv2.cvtColor(green,cv2.COLOR_BGR2HSV)
>>> print( hsv_green )
[[[ 60 255 255]]]

reference

How to define the “lower” and “upper” range of a color? http://answers.opencv.org/question/134248/how-to-define-the-lower-and-upper-range-of-a-color/ http://docs.opencv.org/trunk/df/d9d/tutorial_py_colorspaces.html

Recommended Posts

Handling of HSV color space lower and upper in OpenCV
How to get RGB and HSV histograms in OpenCV
Python implementation of CSS3 blend mode and talk of color space
Handling of quotes in [bash]
Environment construction of python and opencv
Handling of JSON files in Python
Open an Excel file in Python and color the map of Japan
Implementation of particle filters in Python and application to state space models
Python: Preprocessing in machine learning: Handling of missing, outlier, and imbalanced data
Handling of character code of file in IronPython
Screenshots of Megalodon in selenium and Chrome.
Separation of design and data in matplotlib
Summary of modules and classes in Python-TensorFlow2-
Module import and exception handling in python
Project Euler # 1 "Multiples of 3 and 5" in Python
Extract the color of the object in the image with Mask R-CNN and K-Means clustering