Raspberry Pi LESSON 52: Understanding and Using Trackbars in OpenCV

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello guys this is Paul mccorter with toptechboy.com and we're here today with episode number 52. in our incredible new tutorial Series where you're teaching your Raspberry Pi who's boss what I will need you to do is pour yourself a nice tall glass of ice cold coffee that would be straight up black coffee poured over ice no sugar no sweeteners none needed and as you're pouring your coffee as always I want to give a shout out to our friends over at Sun founder Sun founder is actually sponsoring this most excellent series of video lessons and in this class we will be using the sun founder Raphael kit for Raspberry Pi now most of you guys probably already have your gear but if you don't look down in the description there is a link over to Amazon and you can hop on over there and pick your kid up and believe me your life and my life are going to be a whole lot easier if we are working on identical Hardware but enough of this Shameless self-promotion let's jump in and talk about what I am going to teach you today now in the last few lessons we've learned how to really go in and grab a frame from the Raspberry Pi camera and start analyzing it manipulating it stealing from it adding to it we've really learned the data structure of that frame and how we can go in and play with it and what we were starting to do was starting to move down the track of looking at a frame and then dialing in on an object of Interest based on color but then what we found is it turns into a little bit of an exercise of trial and error and change of value run the program change the value and run the program and so what we need is we need some way to be tweaking parameters as we are looking at our image never fear opencv has a feature called track bars and what I'm going to show you today is I'm going to show you how to use track bars and track bars are going to be very very useful for the remainder of this lesson for the remainder of this class because we're always going to want to go in and be able to tweak something and so that is what I am going to show you today sound good I hope so and so what I will need to do is move over to the code View and then I will need to to move out of your way and we don't want to start completely completely from scratch so I will have you call up the most excellent www.toptechboy.com and then I need you to find a lesson you can use the little search icon here to find a lesson called calculating frames per second in opencv and overlay on frame and then we can we can get this code here that we can start with in order to run the camera so let's just come over here I will paste and just to make sure the world is in proper order we will run this and boom there I am in live color at 30 frames a second okay so let's get out of that so we have our basic program okay now what is it that we want to do we want to create track bars now I'm going to give you the homework assignment to do track bars to go in and dial in on the Hue saturation and value to zero in on your object of Interest so I'm going to do a different track bar example and then that will be your homework assignment so what we're going to do is we are going to create a a square a rectangle on the opencv frame but the position of the rectangle and the size of the rectangle is going to be set by what by track bars and so if we're going to want to set X position and Y position and the Box width and the Box height if we're going to want to fine tune all of those things we're going to need how many track bars four okay so this will be a good example this will be something that will kind of allow you to figure things out and get things going and what am am I doing here pretend you didn't see that okay pretend you didn't see that of course guys when I write the code beforehand so that I've kind of practiced it once before we do it for real but we're going to jump in here and we're gonna come that's kind of strange yeah okay so I thought we had gotten this maybe it didn't copy so let's go www.toptechboy.com see there it is so now I am going to copy this and then I'm going to come over here and paste it [Music] okay that looks good maybe uh maybe I just got a little confused I'm not sure what happened a minute ago but we're not going to worry about it we know that this works and so we're going to jump in now and we are going to get ready to code oh I see this is what threw me off this is actually what puts on that frames per second thing so never fear never fear and never fear so I'm going to come down here now and what are we going to need to do we are going to need to create track bars well if we're going to create track bars we're going to have to put them somewhere so I'm going to create a window I'm going to create an opencv window for us to put these track bars in and that's pretty easy I'm going to say CV2 dot named window so this just creates a window that's going to be empty and if it's going to be named I got to give it a name so I'm going to call it my tracked bars like that okay close the string close the named window that looks good now I need to create the track bars how many track bars am I going to need to create I am going to need to create four track bars and so I'm going to do a c v 2 Dot create track bar pretty intuitive isn't it and so what is the first thing that I'm going to need I'm going to need the X position of the Box the X position of the box now it's going to create this track bar that will allow me to set the x position of the box but I've got to tell it where to put that track bar where do I want to put it I want to put it in the window my [Music] track bars okay and that's what we just created right up here so it's going to put this track bar in that window so that sort of makes sense now I've got to give it an initial value where do I want my initial x value well let's just put it anywhere let's put it at 10 okay and now the second thing that it wants is what is the maximum value like what is the maximum value of the track bar well I want it to be on the screen and so you would think that I would want it to be displayed W so that the Box wouldn't go off the screen okay but remember do I want display w no I want what displayed W minus one because we started counting at zero and we've talked about that ad nauseum in the earlier lessons [Music] now I've got a name a function so this is the track bar but when you monkey with that track bar I've got to do something so I've got to create a function that tells opencv or tells python what to do when you monkey with that track bar so here I'm setting up the track bar now what do I do if the user fools with the track bar well I'm just going to tell it to go to the function track X okay what is the function tracks X we haven't defined it yet but what I'm going to do is let's go in and set up all of our track bars and then we'll go in and set up all of our functions does that make sense I hope it does now I think I am just going to snag this one and I think I am going to paste it and we're going to create another track bar but this one instead of X position this is going to be y position it's still going to my track bars I'll still start at 10 but instead of width this is why this is going to be what height display height and this time I'm going to go to the function track Y which I haven't defined yet and remember we set up display width and display height up here so that should be good okay so now let's come down here and let's go again and this time instead of X position what do I want I want to know the Box width so this one is going to be box width that's what it's going to be labeled and then where is it going to go my track bars and let's just say let's start with an itty bitty box and it's going to be how wide it's going to be 10 pixels wide and then how big do you want the box to be well I guess the Box could be like almost kind of like the whole size of the frame so I'll just leave it that big even though you probably really wouldn't make it that big now we are going to do if we did box width we are also going to need what box height like that okay my track bars and we'll make it 10 and this time instead of display width we'll make it display height because we're doing height and instead of tracking x uh oh wait a minute okay I'm sorry this is track X that's right this is track y That's right this would be track W track the width and then this is going to be track H track the height like that does that make sense okay we've created our four track bars and they would go into this empty window called my track bars now if I tried to run it at this point it would probably crash because we haven't created our functions track X track y track W and track H and so let's go ahead and do that and let's do that kind of up here early because I think we might need to Define them before we call them since python execute things in order so I'm going to define a function what track X and then that track X is going to get a value so when it comes to track X what's it going to do it's going to grab the value from the track bar I've got to have a variable that I put that value in so I'm just going to make it a generic variable that I'm going to call Val okay track X is going to put the value of the track bar at the point that you monkey with it in vowel now what is the problem who is the only part of the program that's going to know what Val is the only part of the program is going to be this function so I need to give a global variable that the rest of the program will be able to know what that value is so I'm going to call that Global that Global variable X position because that's what I want to get from the extract bar now I've got to give X position a value that is going to be what well it's going to be the value that we just read okay it's going to be the value that we just read and just and now so X position is going to be the value that we just read from the track bar when we monkeyed with it okay and now value is put into X position which is a global variable so anywhere else in the program it will know what x position is and then just to help in debugging let's go ahead and say print and I'm going to say X position and then that is going to be X position just so as we're going through the program we can kind of debug it see if it's doing what we want now I get probably in more trouble copying and pasting than just writing the whole thing again but let's see if we can do this okay this time instead of track X we are going to track Y and this time what is our Global variable instead of X position it's going to be y position and then it's going to be y position is value and then this would be Y and this would be y actually I think that was kind of a smart move I think that was a pretty easy one now we want what this time we want uh what are we going to call that we're going to call it uh we're going to call it um I got to think a little bit I want to give a good okay we're going to call it box width or I'll just call it box W for box width okay and then box width is equal to Val and then we are going to call this box with and then what are we going to print box width like that okay I think we have one more to go here and so this time oh and this is not track X this is track what what do we call it track W yeah I told you we were going to get in trouble so I'm going to track X I'm going to track y I'm going to track w we almost got ourselves in trouble there but we saved it at the last minute doesn't count as an error if you find it before you run it now we're going to track height okay so now the global variable will be box height and then box height is going to be equal to Val and now we'll call this box height and then we'll call this box H like that guys I think we can run this program now and I do like to run it as we go along so we can kind of get our silly little errors out before the program gets too long now this should create the track bars we don't do anything with that data but we're going to print it out just to make sure that they're working like we expect and so let's run this thing boom look at that okay so here is the camera view and here is our happy little track bar I am not sure why I have that black bar up there I am not exactly sure what that is oh maybe uh I don't know did I maybe I should have given a size maybe what I would want to do is maybe it created a generic size when I did this named window and maybe I should have given the named window a size but I am not going to worry about that right now okay so now look down here right and you can see box width box height 10 10 Exposition y position you can see those now let's see if I move my X position okay do you see as I move my X position that X position is taking on a new value if I move my y position I'm changing the value of y position now I've got box width and I've got box height and those are all changing okay so now the program has that data coming from the track bar so we have done some really really great progress here okay what do we want to do well I think what we're going to want to do now is we are going to want to we are going to want to create a rectangle on our frame based on those parameters and so let me go ahead and let's see here I think did I that's what I got confused about while ago okay I already set a height this is the opencv parameters for that uh for that frames per second text and so I've got some stuff already uh set up there but I'm just going to do a different one box color and box color is going to be equal to what color do we want this box hmm I want to make it red so I'll just use my color because my color is already red okay so now we're going to come down here and darn it I hate to do this to you guys but I hate using M so I'm going to call it frame and if I could hold that frame gotta call this Frame if I call that frame I've got to call this Frame okay and so now I've put the text now what do I need to do I need to do a CV2 dot rectangle CB2 dot rectangle and then where do I want to put that rectangle I want to put it on frame and then where does it go remember you've got to give the position of the rectangle in a tuple which means inside of parentheses and x and a y inside of parentheses and again opencv wants column comma row when we're dealing with the frame itself and manipulating the frame itself it wants it wants row well I'm sorry I might have said that I might have said that backwards opencv wants column comma row when we're manipulating the frame and selecting pixels or or ranges it wants row comma column and so they're backwards but this is a this is an opencv command here so this will be column comma row and what is column that is X position because that's going back and forth comma y position so that's the upper left corner as a tuple now I need to give it the lower right corner as a tuple how would we do that that would be X position plus what x position plus what box W okay and then the lower right the Y value would be y position plus box height like that close the Tuple now we got to give it uh we got to give it my color what did I call that up there my color as I thought easier to check than to Chrysler program and then the weight of the line was three and that would probably be okay could it really be this simple I will need everyone to hold your breath take a big Swig of jet fuel take a big Swig of jet fuel hold your breath all right we got something happening look we got some good stuff Happening Here giddy up I think this is gonna work so let's come in here and let's move this let's move this up here guys you could do move Windows commands to have these things uh to have these things come up more in a more organized fashion but the problem that I have is I have a second screen and opencv gets confused with two screens so if I start trying to put it in position it puts it up on the screen that you're not looking at okay let's see what happens when we play with X position giddy up changing the X position double chest boom okay and changing the Y position now I think we need a what I think we need a bigger box and what do we need we need a bigger box and you see like I could tune in here I could tune in here on what I could tune in here on my face and so that's the Box width is going to get smaller and then we're going to move it over so you see I could kind of dial in on my face here now you remember what we're really trying to do is dial in on a color but I'm just kind of giving you a demo here so you'll see how to do it and now I've showed you how to do track bars but I can't help it I'm just going to try to do one thing on the fly one thing on the Fly because we've got that little thing defined there well what if I came in and I created a region of interest and that region of interest is going to be frame and what part of the frame do I want well I want that that region now remember when we're manipulating with frame we want to do what we want to do rows first we want to do y first so I want to go from y position okay to what y position plus box width you see I'm trying to create a new region of Interest a new frame based on whatever is inside the box so I've got my rows from y position to Y position plus box width and then I'm going to need what x position to what x position plus ah Y is not box width why do I have this backwards Y is not box width Y is box height I think I had that backwards y plus box height X position X position and it's not box height it's box H I told you I shouldn't do this stuff on the Fly and then X is going to be X position plus box with I sure hope that works I sure hope that works okay and then close that whole bad boy off and then I should never try to do silly stuff like this on the fly but now what we're going to do is let's show that so I'll say CB2 dot i m show and then what I'm going to show I'm going to show the ROI and then what is it going to be it is the data set Ro I like that and I just really have a feeling I made a mistake here but hold your breath okay we got stuff happening we got stuff happening here so we're gonna put this here we're gonna see if we can move this over here and then we have this itty bitty little box called Roi well let's see if we can make that okay that just moved it let's get this thing bigger okay there now we got something going now we got something going okay now let's see if we can move this up and down I can move it over and then I can make it taller and then I can move it back boom okay do you see that do you see how cool that is is now we're using the track bars to define a region of interest and then we're bringing it bringing it over and making it a new data set so how cool is that okay guys I hope you see the power of track bars and I also hope you see how easy the track bars are to set up and use and so they're very powerful and they are pretty easy to set up but then what your homework assignment is going to be what your homework assignment is going to to be is to do something like this okay so you got to go back and think about last week's lesson and let's see if this is actually going to work here so I'm going to bring this down okay and then I'm going to bring this down and then I am going to bring this down and then I'm going to bring my track bar over here okay I'll put the track bar on top of the Mask okay so now if I bring this in okay what do I want to do I want to dial in on this so I'm going to set a hue okay I'm going to set a hue value and then I'm going to come in and do you see how look down ah let me go munchkin on you so that you can see this okay but do you see how I can just so carefully dial in my object of interest and you see how I'm getting rid of a lot of that noise I'm getting rid of the noise but I am completely I'm completely getting ah now I'm getting some other stuff and I see that came way up too far okay do you see how I'm completely capturing my region of Interest my object of interest and I'm I'm tracking my object of Interest okay and I've been able to tune out my hand and I've been able to tune out other things now orange is a little harder but let's see if we can do the orange here okay and so I think what I need to do is I need to come up I've got to bring that further down I got to bring this further down and up and you see how now very quickly I am tracking a new object of Interest okay so that is your homework I hate being munchkin I hate being munching okay that is your homework assignment for next week and you need to post your homework solution to YouTube you guys know the drill I don't have to give you all the details on that but post your homework and show that you can dial in an object of Interest with those track bars and then as we move forward you're going to have the tools that you need to start doing some pretty cool and exciting things and so that is your homework assignment guys I hope you're having as much fun taking these lessons as I am making them if you enjoyed the lesson make sure to give us a thumbs up also leave a comment down below because that helps us with the old YouTube juice if you have not subscribed to the channel already make sure that you subscribe when you do ring that Bell so you'll get notification of future lessons and as always share this video with other people because the world needs more people doing engineering and fewer people sitting around watching silly cat videos Paul McWhorter with toptechboy.com I will talk to you guys later [Music]
Info
Channel: Paul McWhorter
Views: 4,407
Rating: undefined out of 5
Keywords:
Id: sBScHr8H8C0
Channel Id: undefined
Length: 27min 26sec (1646 seconds)
Published: Thu Mar 09 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.