Color Filtering - OpenCV with Python for Image and Video Analysis 7
Key Takeaways
This video tutorial covers color filtering using OpenCV with Python for image and video analysis, specifically focusing on filtering for a certain color range in the HSV color space and applying masks to frames.
Full Transcript
what is going on everybody welcome to another open CV with Python tutorial in this tutorial we're going to be talking about how we can filter an image or a video for like a specific sort of range of colors so we could use this to either find a specific color or actually remove a specific color uh from a video or only show a specific color or show everything but a very specific kind of color range so this is very much like uh how the operation that's performed with a green screen you find that green and you remove it and maybe even replace it with um you know another image or background so um let's go ahead and get started so we're going to go ahead and start with open CV and numpy imported and then we're going to say cap equals CV2 do video capture and I'm going to put a one here but you should put a zero most likely if you're using a webcam um but my first camera is recording already so I'm using my second camera over here now um if you don't have a webcam what you'll want to do is either use an image or something like that and what we refer to as the frame you will just you'll em in and then you'll use your image in place of our frame or your image data so uh that's our capture and then you won't need a a while loop if you're just using an image and this will be true throughout the rest of this tutorial where we're using video rather than an image so wow true uh we're going to say underscore frame equals cap. read if you're not familiar underscore just means like it's a it's a value that's returned from a function so we pretty much need it there to be unpacked to but we just don't care about it so we give an underscore to just like let any reader or ourselves if we visit this later no we don't use that return so underscore frame equals cap read then HSV and this is uh HSV stands for Hugh saturation and value I believe yes um and this is another way to define colors and we'll be talking about that a little bit more here in a second so um just for now HSV equals CV2 Doon convert color and we're going to convert the color of our frame using the function of CV2 do all uppercase here coloror BGR to HSV so we convert this to HSV colors if you're not familiar here is a lovely chart from Wikipedia and the idea here with HSV the reason why we're going to use this is for range purposes and the reason why it makes more sense to use HSV for range rather than RGB or BGR is due to the nature of how it works so with RGB and BGR each value of your three values is a color value and so a range of those color values to another range just is not going to work whereas with HSV the Hue is the color value the value is basically how much of that color is in existence and the saturation is how much intensity of that color for lack of probably a better explanation of that but each of those is very independent right you've got color you've got intensity and you've got like light I suppose maybe with value uh so each one is very independent of the other one whereas with RGB BGR that's just simply not the case so making a range of colors in that format just will not work very well anyways it'll work it just won't work as you might intend so um so there we have that now we're going to have two colors we're going to need to be seeking an image of something and I'm going to be using this nice Red Hat uh because the hat is red it's a very unique color there is no more red like this in my room um use whatever image of or use Whatever item you want try to make it a unique color though that doesn't exist anywhere else in your area so um so that HSV and then what we're going to do is we're going to have a lower red or lower whatever color for you upper red and these are going to be we'll start with a numpy array so np. array and we'll start with 0000 Z for the lower and then np. array 255 255 255 now in um in CV there is the following we're not going to use it because it not it's just simply not going to work but you can convert a single color to an HSV color using this function convert color you can do that but in the this specific setting it makes no sense to use that because you're going to find that when you convert that the values in your HSV versus the values that you get from this won't even make a legitimate range in a lot of cases and even if it did they're just they're it's like apples and oranges right these are two things that are you can convert one but if you're trying to make a range with that AR range is a very type of operation I suppose and that type of operation performs totally different uh in HSV versus RGB or BGR because the elements that comprise it are just different right it just doesn't make sense to use that but it does exist just keep that in mind but for here we're going to kind of do our own uh natural filters uh so um we'll get there in a minute it'll be trial and error basically so we Define lower and upper red now we're going to create our mask our mask will be equal to CV2 do uh in range so anything that's in range of our uh you well in range from this HSV which was converted to HSV lower red upper red so the mask will be everything that's Within These ranges which right now is everything so the mask currently looks identical to the frame using these parameters here basically then then the result is going to be equal to CV2 bitwise uncore and so this is going to be where applied to the frame where the frame is equal to frame and where the mask equals mask okay so this is an operation that's performed basically where in the frame the there is the frame and the mask is equal to mask so so so it's where there is something in the frame and where the mask is basically true so the mask will be basically zero or one be either it's in the range or it's not so if it's in the range it will be a one so it'll be white and then when we perform this bitwise operation where there are ones in our mask we will show the color from the frame okay so um so as I said right now everything's going to be a one so we're going to see really everything but we we'll talk about um how we're going to filter here in a second so then we're going to say k equals CV2 weight key uh and this will be for an exit so key5 and 0x FF if k equals 27 break you can also just do the weight key zero if you want CB 2. destroy all windows and then do a cap release this lets your your webcam go so it's it if you leave the program running and you haven't done a cap release even if you're not displaying output from the camera it's like using the camera so if you do a cap release that releases it otherwise you can close the program and most likely it'll let your camera go but things can happen so let's go ahead and run that really quick just to see it should be here I'm not sure why it's not popping up yet maybe my camera was in use or something or did I you no I did use the right camera I'm not quite sure why we're not getting our oh we're not actually showing anything what oh goodness why didn't anybody say anything okay CV2 M show we need to show the frame so we'll show the frame I cannot believe this CV2 all right so now frame and then we'll show the mask and then we'll show the results all right all right so as I said this is the original everything is in that range because everything's in that range we get everything in the result so now we're going to work on filtering so the first thing so HSV is you know Hue sat value so if we look back over here Hue is the main thing that dictates what color things are so basically we can start modifying Hue and see how that works for us so we could set Hue to 50 okay and we can see what happens Well turns out we actually with that one change we've removed quite a bit in the background and we've retained our hat at some points where the hat is very bright like right here uh it is removing that cuz it's probably more of like an orangey color maybe uh so you could like lower this maybe a little bit to I don't know 30 maybe maybe that'll be a little better yeah that's a little better but then obviously we got all these other colors here uh let's try to make it let's see if we can make it 10 and keep it kind of in color no matter what we do we just simply just does not want the hat to be shown that's very strange I can't believe that that itty bitty bit okay let's lower this to 180 what happened I don't even know why that would happen that way 0 to 180 and it's still okay well I'm going to set this to let's try let's try 100 and it's still there so let's try and that and then what why did why didn't 180 work initially that's very strange I cannot decide okay well that's good enough okay so let's close uh close these and then we'll set this let's try to set this one to 150 that's basically everything all righty so I don't know set this to pretty much whatever we want let's try 150 nope we lost it all let's set it to 50 that's pretty good let's lower this one a little bit let's set it to let's set that one to 150 still got this background noise but that's okay we'll talk about that in the next tutorial so basically we've got here what happens is we take this uh frame the mask is basically everywhere it's either a zero or one it's a Boolean operation either either the pixel is in that range or not if it is in that range great and then here is where there is yes mask and then we've got color basically so it shows the color and that's how we get the red now obviously we have a little bit of um noise which we can see really well in this one where we've got all these like white pixels you also have a little bit of my ear oops this ear uh showing up and maybe a little bit of my neck Maybe there anyway we've got some noise so in the next tutorial we're going to start talking about how you can begin to eliminate noise with smoothing and blurring and stuff like that so that's what we're going to be talking about in the next tutorial if you got questions comments concerns whatever leave them below otherwise as always thanks for watching thanks for all the support and subscriptions and until next time
Original Description
In this OpenCV with Python tutorial, we're going to cover how to create a sort of filter, revisiting the bitwise operations, where we will filter for specifically a certain color, attempting to just show it. Alternatively, you could also specifically filter out a specific color, and then replace it with a scene, like we did with replacing a ROI (region of image) with something else, much like how a green screen works.
In order to filter like this you have a few options. Generally, you are probably going to convert your colors to HSV, which is "Hue Saturation Value." This can help you actually pinpoint a more specific color, based on hue and saturation ranges, with a variance of value, for example. If you wanted, you could actually produce filters based on BGR values, but this would be a bit more difficult. If you're having a hard time visualizing HSV, don't feel silly, check out the Wikipedia page on HSV, there is a very useful graphic there for you to visualize it. Hue for color, saturation for the strength of the color, and value for light is how I would best describe it personally. Now let's hop in.
http://pythonprogramming.net
https://twitter.com/sentdex
Watch on YouTube ↗
(saves to browser)
Sign in to unlock AI tutor explanation · ⚡30
Playlist
Uploads from sentdex · sentdex · 0 of 60
← Previous
Next →
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
Matplotlib Python Tutorial Part 1: Basics and your first Graph!
sentdex
Python Encryption Tutorial with PyCrypto
sentdex
Python's Logging Function
sentdex
wxPython Tutorials 1: Making Windows GUIs with Python : Installing + 1st window!
sentdex
wxPython Tutorials 2: Making Windows GUIs with Python: Customizing Window Parameters
sentdex
wxPython Programming Tutorial 3: Menu Bar and Menu Button
sentdex
wxPython Programming Tutorial 4: Panels
sentdex
wxPython Programming Tutorial 5: User Input Saved To Variables
sentdex
wxPython Programming Tutorial 6: Multiple Choice Input
sentdex
wxPython Programming Tutorial 7: Adding Static Text and Colors
sentdex
wxPython Programming Tutorial 8: Custom Button Images
sentdex
wxPython Programming Tutorial 9: Tool Bar Items and Sub Menus!
sentdex
Basic PHP Tutorial 13: Multi-dimensional Array
sentdex
Basic PHP Tutorial 15: Functions and Global Variables
sentdex
Basic PHP Tutorial 12: Associative Array
sentdex
Basic PHP Tutorial 14: Foreach loop
sentdex
Basic PHP Tutorial 16: Include and Require
sentdex
Basic PHP Tutorial 7: Assignment, comparison and Logical operators
sentdex
Basic PHP Tutorial 4: Variables and Comments
sentdex
Basic PHP Tutorial 11: Arrays part 1, basic array
sentdex
Basic PHP Tutorial 6: If else and else if conditionals cont'd
sentdex
Basic PHP Tutorial 1: Intro to PHP
sentdex
Basic PHP Tutorial 3: HTML with PHP
sentdex
Basic PHP Tutorial 9: While Loop
sentdex
Basic PHP Tutorial 10: Switch Statement
sentdex
Basic PHP Tutorial 2: Print and Echo
sentdex
Basic PHP Tutorial 5: If else and else if conditional statements
sentdex
Basic PHP Tutorial 8: Arithmatic Operators: Doing math with php
sentdex
Basic PHP Tutorial 17: User Input Form Example / String Manipulation
sentdex
Basic PHP Tutorial 18: HTML Entities and forms cont'd
sentdex
Basic PHP Tutorial 19: Finding words in strings
sentdex
Basic PHP Programming Tutorial 20: Saving to a File / writing and appending
sentdex
Basic PHP Programming Tutorial 22: Hashing part 2: salting
sentdex
Basic PHP Programming Tutorial 23: Variables in Strings and tokenizing
sentdex
Basic PHP Programming Tutorial 21: MD5 Hashing For Security
sentdex
Basic PHP Programming Tutorial 24: String similarity
sentdex
Basic PHP Programming Tutorial 25: Time and Time stamps
sentdex
Basic PHP Programming Tutorial 26: Die and Exit
sentdex
Basic PHP Programming Tutorial 27: MySQL Databases Part 1
sentdex
Basic PHP Programming Tutorial 28: MySQL Database Part 2: Reading From Database
sentdex
Basic PHP Programming Tutorial 29: MySQL Database Part 3: Inputting Data
sentdex
Basic PHP Programming Tutorial 30: MySQL database in Use
sentdex
Django Tutorial Web Development with Python Part 1: Installing Django
sentdex
Python Tutorial: File Deletion and Folder Deletion / directory deletion
sentdex
Python Tutorial: How to Rename Files and Move Files with Python
sentdex
3D Graphs in Matplotlib for Python: Basic 3D Line
sentdex
3D Plotting in Matplotlib for Python: 3D Scatter Plot
sentdex
3D Charts in Matplotlib for Python: Multiple datasets scatter plot
sentdex
Sikuli Tutorial 1: Visually programming in python!
sentdex
Sikuli Tutorial 2: Program visually in python!
sentdex
Sikuli Tutorial 3: Program visually in python!
sentdex
3D Bar Charts in Python and Matplotlib
sentdex
3D Plane wire frame Graph Chart in Python
sentdex
Raspberry Pi Part 1 Introduction
sentdex
Raspberry Pi Part 8: First Download and Update! (Firmware)
sentdex
Raspberry Pi Part 10: How to set up a Linux Web Server on your Pi
sentdex
Raspberry Pi Part 11: Remote Desktop
sentdex
Twitter Analysis: How to rank a user's influence
sentdex
GPIO Tutorial for Pi Part 2 - Programming the GPIO
sentdex
GPIO Tutorial for Raspberry Pi Part 1 - Setting up
sentdex
More on: CV Basics
View skill →Related Reads
📰
📰
📰
📰
Why Developers Are Switching from Feedly to Open‑Core AI RSS Readers
Dev.to · Mohammad Sorouri
I added an AI helper to my JSON mock-API tool — the hybrid design, and a Workers AI gotcha
Dev.to · solca
How I Built a Blank Page Detector for PDFs with Vue 3 and Canvas
Dev.to · sunshey
What I Learned Building an AI Resume Optimizer — And What Recruiters Actually Told Me
Dev.to · dayu2333-jinyul
🎓
Tutor Explanation
DeepCamp AI