How to Detect Colors in OpenCV [Python]

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello everybody welcome back to another video in today's tutorial video i'll be showing you how to detect as well as track colors in python using opencv i'll be using visual studio code as my code editor so let me just create a python file here real quick after that we need to import all the modules we'll be using opencv obviously and make sure to import numpy as well the next step is to decide what color you want to detect and i need some explaining to do first we'll be detecting colors in hsv format meaning that if we want to detect a color we need its hue saturation and value hue is the color itself saturation is the colorfulness of the color and value is how light or dark the color is in opencv the hue ranges from 0 to 180 the saturation ranges from 0 to 255 and the value ranges from 0 to 255 as well i want you to look at this color map that somebody else made the x-axis represents the hue the y-axis represents the saturation and i want you to just ignore the value for now let's just look at the hue and saturation let's say we want to detect yellow what we then need to do is find it on the color map then we need to set a lower range in an upper range of the color we want to detect and in my case i'll be using yellow but you can obviously use any color you want but since i'm using yellow i'll set the hue lower range at about 15 and the hue upper range at about 35 i'll then also set the saturation lower range at about 150 and the saturation up range at the maximum which is 255. now when we get back to our code we need to specify an upper and lower range of the color we want to detect so let's set a lower range variable and we'll set it to be a numpy array of the lower range values this is the heel lower range this is the saturation lower range and this is the value lower range now we do the same for the upper range except we use all the upper range values now we haven't really talked about the value a lot and that's because by default people set the lower range to around 20 and the upper range to the maximum which is 255. if however you wish to change it you can tweak these two values now that we have the range of the color we want to detect we can read in any image or video and in my case i'll be detecting colors through my webcam so i'll be typing video equals cv2.video capture and passing in xero as an argument this will capture my webcam footage and store it in this variable then we can type while true because we need the following code to run endlessly in order to continually capture our webcam footage if we didn't use a loop then we would only capture one frame from the webcam in the while loop we type success image equals video.read this read function basically reads our video object and it returns a boolean which is stored in success which determines if the video got read successfully the new image then gets stored in a new image variable since opencv stores images in bgr format we need to convert our image to hsv we can do this by typing cv2.cvt color and then passing our image as an argument and then the type of conversion we want in this case cv2.color bgr to hsv then we store this into a new image variable now we need to create a mask object this mask object will find and separate the color we want from the original image we can do this by creating a mask variable and then storing the mask object which is cv2.inrange the first argument is the image the second is the lower range of the color we want to detect and the third argument is the upper range after this we can type cv2.imshow which is a function that takes two arguments a custom window name is a string and an image object this function will show the image that we specify so we need to do this to our original video image and the masked image after this we just type cv2.weightkey and pass in one as an argument which is basically a small delay in milliseconds of how long each image gets shown on the screen after this we can run our code as you can see the mask window picks up our yellow object but now you might be thinking cool we detect the yellow object in this window but what do i do now well we can do a few things but i'm going to show you how you can get the coordinates of the object and draw a rectangle around it so the first thing we need to do now is to find the contours of our most image this means finding the outer edge of our detected object so the whole reason we actually have this masked image right here is so that we can very easily detect the outer edge of our object seeing as our masked image very clearly shows us where the object is we can detect the contours with cv2.find contours the first argument is the image we want to detect contours on so we pause mask as our first argument and then one of these four flags as the second argument i'll leave a link down below on what each one does and in a few seconds i'll go into a bit more detail but for now i'll choose cv2 dot retrieve external and for the third and final argument you can choose one of these two flags and again i'll go into a bit more detail in a second but for now i'll be choosing the simplest one which will also save us more memory cv2.chain approx simple we can then store the return value of this function inside of two variables the first one is a list of all the coordinates of our contours and the second one is also a list and to put it simply this hierarchy variable will store the relationship that the contours have with each other and again i'll leave a link down below if you want to understand this topic a bit better now quickly just getting back to this those four retrieval modes that we talked about all affect the way that the contours get retrieved and stored in the hierarchy variable and then when we get back to this one the difference between these two flags is that chain of proc simple only stores all the necessary coordinates of the contour and chain approx none stores every coordinate of the outer edge of the contour for example i got the screenshot from an opencv documentation and it illustrates the difference between the two contour approximation methods very well the simple one only stored all necessary points which saves a lot of memory while the other methods stored more than 700 points after this we can check the length of our masked contours list to check and see if we have found any contours in the first place so if the length is not equal to zero then we can iterate over all the contours and for each contour that we detect we can check the size of the contour area to see if it is greater than 500 pixels the reason we're doing this is because we don't want to detect single white dots on our masked image then we can type x y width and height equals cv2 dot bounding rect and pass in the current contour as an argument this will draw a rectangle around our contour object and return the coordinates of the rectangle then we can actually draw the rectangle on screen by typing cv2.rectangle the original video frame is the first argument a tuple of the x and y coordinates of the top left corner of where we want the rectangle to be drawn a third argument which is a tuple of the bottom right corner of the rectangle and then another two plus the fourth argument which is the object's color in bgr format and then finally the thickness of the rectangle in pixels and then the final step is to run our code and test it on a few objects and as you can see it works fairly well in the mask you might see a bit noise and there are a lot more fixes to this all we did was to check the size of our object but there are a lot more other ways and i encourage you to check them out as well all the code will be on github and i'll leave links to important websites down below thank you so much for watching this video if you have any other tutorial suggestions or any tips on how you think my tutorials could improve then please do let me know feel free to ask any questions if you have any and also remember to subscribe to the channel and like the video for eternal happiness in this dark cruel world and i hope to see you in the next video
Info
Channel: CreepyD
Views: 39,389
Rating: undefined out of 5
Keywords: how to detect colors in opencv, how to detect color in opencv, how to detect color in python, how to detect color in opencv python, opencv color detection python code, opencv color detection tutorial, python opencv color detection, color detection using python, color detection using opencv, specific color detection using opencv, color tracking opencv, detecting colors using opencv python, detect colors in image python, python color detection hsv, python opencv, opencv python
Id: cMJwqxskyek
Channel Id: undefined
Length: 6min 50sec (410 seconds)
Published: Sat Jan 16 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.