Choosing the Right HSV Upper and Lower Boundaries for Color Determination Using 'cv :: inRange' (OpenCV)

I have an image of coffee with an orange cap that I want to find. There he is image .

Utility

gcolor2 shows HSV in the center of the cap (22, 59, 100). The question is how to choose color borders? I tried min = (18, 40, 90) and max = (27, 255, 255) but got unexpected result

Here is the Python code:

import cv in_image = 'kaffee.png' out_image = 'kaffee_out.png' out_image_thr = 'kaffee_thr.png' ORANGE_MIN = cv.Scalar(18, 40, 90) ORANGE_MAX = cv.Scalar(27, 255, 255) COLOR_MIN = ORANGE_MIN COLOR_MAX = ORANGE_MAX def test1(): frame = cv.LoadImage(in_image) frameHSV = cv.CreateImage(cv.GetSize(frame), 8, 3) cv.CvtColor(frame, frameHSV, cv.CV_RGB2HSV) frame_threshed = cv.CreateImage(cv.GetSize(frameHSV), 8, 1) cv.InRangeS(frameHSV, COLOR_MIN, COLOR_MAX, frame_threshed) cv.SaveImage(out_image_thr, frame_threshed) if __name__ == '__main__': test1() 
+63
opencv hsv
Jun 08 2018-12-12T00:
source share
5 answers

Problem 1: Different applications use different scales for HSV. For example, gimp uses H = 0-360, S = 0-100 and V = 0-100 . But OpenCV uses H: 0-179, S: 0-255, V: 0-255 . Here I got a hue value of 22 in GIMP. So I took half, 11, and determined the range for this. those. (5,50,50) - (15,255,255) .

Problem 2: Also, OpenCV uses the BGR format, not RGB. Therefore, change the code that converts RGB to HSV as follows:

 cv.CvtColor(frame, frameHSV, cv.CV_BGR2HSV) 

Now run this. I got the output as follows:

enter image description here

Hope this is what you wanted. There are a few false detections, but they are small, so you can choose the largest outline that your cover is.

EDIT:

As Carl Philipp said in his comment, it would be nice to add new code. But there is a change in only one line. So, I would like to add the same code implemented in the new cv2 module cv2 that users can compare the lightness and flexibility of the new cv2 module.

 import cv2 import numpy as np img = cv2.imread('sof.jpg') ORANGE_MIN = np.array([5, 50, 50],np.uint8) ORANGE_MAX = np.array([15, 255, 255],np.uint8) hsv_img = cv2.cvtColor(img,cv2.COLOR_BGR2HSV) frame_threshed = cv2.inRange(hsv_img, ORANGE_MIN, ORANGE_MAX) cv2.imwrite('output2.jpg', frame_threshed) 

This gives the same result as above. But the code is much simpler.

+124
Jun 08 2018-12-12T00:
source share
โ€” -

I created this simple program for receiving real-time HSV codes

 import cv2 import numpy as np cap = cv2.VideoCapture(0) def nothing(x): pass # Creating a window for later use cv2.namedWindow('result') # Starting with 100 to prevent error while masking h,s,v = 100,100,100 # Creating track bar cv2.createTrackbar('h', 'result',0,179,nothing) cv2.createTrackbar('s', 'result',0,255,nothing) cv2.createTrackbar('v', 'result',0,255,nothing) while(1): _, frame = cap.read() #converting to HSV hsv = cv2.cvtColor(frame,cv2.COLOR_BGR2HSV) # get info from track bar and appy to result h = cv2.getTrackbarPos('h','result') s = cv2.getTrackbarPos('s','result') v = cv2.getTrackbarPos('v','result') # Normal masking algorithm lower_blue = np.array([h,s,v]) upper_blue = np.array([180,255,255]) mask = cv2.inRange(hsv,lower_blue, upper_blue) result = cv2.bitwise_and(frame,frame,mask = mask) cv2.imshow('result',result) k = cv2.waitKey(5) & 0xFF if k == 27: break cap.release() cv2.destroyAllWindows() 
+23
Sep 25 '14 at 16:58
source share

Well, finding color in the HSV space is an old but common question. I made hsv-colormap to quickly find a particular color. Here:

enter image description here

The X axis represents Hue at [0.180), the y1 axis represents Saturation at [0.255], the y2 axis represents S = 255 , and V = 255 .

To find the color, usually just find the range of H and S and set v to the range of (20, 255).

To find the orange color, we look for a map and find the best range: H :[10, 25], S: [100, 255], and V: [20, 255] . So the mask is cv2.inRange(hsv,(10, 100, 20), (25, 255, 255) )

Then we use the found range to search for orange, this is the result:

enter image description here




The method is simple but common to use:

 #!/usr/bin/python3 # 2018.01.21 20:46:41 CST import cv2 img = cv2.imread("test.jpg") hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV) mask = cv2.inRange(hsv,(10, 100, 20), (25, 255, 255) ) cv2.imshow("orange", mask);cv2.waitKey();cv2.destroyAllWindows() 

Related answers:

  1. How to define a threshold for detecting only green objects in an image: Opencv

  2. Choosing the Right HSV for OpenCV Threshold Using InRangeS

+20
Jan 21 '18 at 13:18
source share

OpenVV HSV Range: H: 0 to 179 S: 0 to 255 V: 0 to 255

In Gimp (or other manipulation of sw photos), the range of colors is from 0 to 360, since opencv puts color information in one byte, the maximum numerical value in one byte is 255, so the values โ€‹โ€‹of openCV Hue are equivalent to the values โ€‹โ€‹of the shade from gimp divided by 2.

I found that when trying to detect an object based on the HSV color space, range 5 (opencv range) was sufficient to filter a specific color. I would advise you to use the HSV color sky to figure out the range that is best for your application.

HSV color sky with HSV color detection

+3
Mar 01 '17 at 0:27
source share

To find the HSV value for Green, try the following commands in a Python terminal

 green = np.uint8([[[0,255,0 ]]]) hsv_green = cv2.cvtColor(green,cv2.COLOR_BGR2HSV) print hsv_green [[[ 60 255 255]]] 
0
Oct. 31 '18 at 6:47
source share



All Articles