Auto-Measuring with OpenCV + Python - Try It Yourself

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
there's one that's a two peso there's an old silver coin here's a bicentennial here's the one with no clad here's elizabeth sneaking in here hey everybody you know ever since i made that video on triangulation one of the things that people keep asking me about is how can i measure size how can i take something like this and look at it with a camera and you know determine the size in an automated way do i have to use triangulation how many cameras do i have to use and all that sort of thing what kind of calibration and all that well the reason we use two cameras in triangulation if you think about it is because we're missing information we don't know how far something is away from the camera and so in order to do the trigonometry or the geometry what we have to do is start with a fixed distance and we use the distance between the camera as a known distance we know this distance very exactly and then we can use that to calculate all the other differences but if you've got a camera and a say let's say a table then you know this distance or this distance always stays the same and if that distance stays the same then there's a direct relationship to the pixels on the table you know what you see in the frame and the size of objects okay so then it becomes really easy to do the only thing we have to compensate for is like if you if you look um do you see how these curtains are kind of curved like that okay or you look over here at the door they're kind of curved well lenses are spherical so in the center as you move away from the lens the distances so you know like if you looked at essentially like the ruler here in the middle okay you got a distance but then as the ruler gets way over here it gets smaller right so you have to compensate that as you move from the center of the lens out to the edges your distance gets shorter but once you compensate for that then you're good to go and it's easy to compensate you don't have to do all that crazy mathematical compensation of your frame because that that actually reduces your frame size your usable frame size a lot okay so you don't have to do that you can just do a real simple calibration and you can make yourself something that measures and i did that i made some software for you uh that that does that so you can get started okay so this this should be pretty cool now one thing i did do is i went ahead and let's look this up at this i went ahead and uh put a youtube projects thing on git lab with the rest of my gitlab stuff so my git lab id is uh duder1966 uh so you can see that right there uh yeah that's from the big lemoski and the year i was born so uh so the software is gonna be there i'm gonna start putting stuff up there when i do it just to make it easier for you guys to get it so all right let's uh let's get started with this let me show you my setup okay all i've got going is i've just got this stand right here and here's my webcam that i just recorded the intro on and i've got this little clamp thing and it it allows it to turn straight down and you'll see this looks a little crooked that's because my lens is not quite straight but you want this as straight up and down as possible right from you know from both angles right straight up and down and then i've just got this platform down here so this is my table and this is my fixed distance it doesn't really matter what it is because we'll compensate for that when we do the calibration and then i just got some junk down here that we can that we can work on and measure and i've got this white background because we're going to be using opencv and we need some contrast right between the background and here so that opencv can recognize objects without us pointing it out okay so that's just piece of paper for contrast and then the other important thing is i've got these two lights right here okay that i've got set up so i'm going to close those curtains that'll be dark in a minute but i'm going to close those curtains and then these lights will reflect up on the ceiling and then down here because what we're trying to create is um when we're focusing in on these objects we don't want a shadow so we're trying to eliminate the shadow because again with opencv if we want to do something in an automated way we've got to have a contrast between the back and the object and then we try to eliminate shadow because opencv will think that the shadow is the object and it'll mess up your stuff right so and then here's some rulers we'll use to calibrate okay so there we go that's all the stuff let's get started with the software okay however you get your scripts when you get them you should have three so cam ruler is the main script and then there's two others one is this draw function which i use to write and put some lines on the frame and then the frame capture is is actually a camera thread so it's what cam ruler uses to to get frames from the camera it does it in a separate thread it just helps you manage your speed at which you can capture frames and then the main thing is cam ruler so uh you can pop it open and so it's about 500 lines of code and we will have to edit only one line so let me just scroll on down there we're going to edit this line right here the calibration range line but we'll do that in just a second okay so i'm gonna pop open and i happen to know what uh camera number i'm camera zero but if you don't know you can always do a um i'm sorry you can list dev and then if you look down here here's your here's your video right there so i'm using video one those are your options really each camera will give you two things so this is the one camera and then this is another camera but you can only use the first of the two so i my choices are zero and two but i'm on zero i know that okay so i'm just going to run cam ruler on zero and hit enter and it pops up i'm gonna maximize the screen all right so the first thing we notice is that this is upside down so we need to fix that up here in the corner is a key tells you everything that's going on and this second part right there r for rotate so we'll just hit r and that'll flip our screen over the way that we want so it just depends on how your camera is set up you you may need to leave it unrotated or you may need to to rotate it uh so up here we've got the camera we've got the um the size of the frame our feet per second that it's running uh this would be we clicked so if we if we click then we get this last click data we get the current um xy data that sort of stuff now that that's in pixels and our we'll get measurements in a minute okay but the first thing that we have to do before we explore any of this other stuff is we need to calibrate the camera so i'm going to hit c up and it says c for config mode so i'm going to hit c and what we need to do that this this is the center of the frame and then this is the farthest distance that we can be away from the frame down to here so i'm going to take my ruler that i'm going to use for calibration i'm calibrating in millimeters i'm going to put it right there right on that line kind of get it parallel with this red line coming down here and i'm going to look down and i can say that i can see 72 millimeters and that's the number that we need so now i'm going to hit q for quit and get out of there we're going to pop this open and this is on line 74 and we're going to put the 72 that's how many millimeters so this is all i'm changing right here and that's just going to help us in the calibration deal so that's how far i can see so i'm saving that we'll minimize that and then we'll restart okay gotta hit r to flip it over and then we're gonna hit c to start the calibration or config mode and everything's lined up and then so what you see right here it just says config click on distance equals five so that's what we want to do now here's a little secret if you're running linux like me you can scroll your mouse and zoom in so i'm gonna put this down here and i'm gonna scroll my mouse and zoom in okay and then uh now i can see that my ruler is a tiny tiny bit off and there we go okay so i'm just going to follow these instructions click on d equals five now i want to line up that's why this these cross hairs are tipped so they they help you out you want to line up on on both this line and the distance away from the center point there's the center point come the distance away line up and it's it's hard to do but you know your mouse sensitivity i guess i need to change my mouse sensitivity but oh that's pretty good so click on five now i need to click on 10 and right there and then i'm going to work my way down so i'll speed it up a little bit 65 and seventy okay now we're done so i'm zooming back out and now we're all calibrated now of course you know the more accurate you can be the better the more straight up and down your camera can be you know all those things contribute to it and and this is a rudimentary form of calibration but it does a pretty good job so we should be able to uh now by default we're in the measurement mode so i'm just going to click oh i don't want to click there right click right click clears everything so i'm going to click on roughly on 50 and then i'm going to come over here and you know i'm uh i'm within a seven hundredths of a millimeter right there so yeah i did a pretty good job it depends on sometimes your mouse will jump you know one pixel difference will change you a lot yeah okay so we did a pretty good job getting things calibrated all right so now we're ready to go so the first thing we can do i got some coins out here to mess around with now if you see the screen flickering like that it's really hard to get the light to manage itself so sometimes if i put something in there it keeps the screen from flickering it's it's just for video for youtube you know it's hard to get it to work right all right so here's a cool quarter this quarter has um it's missing the cladding i found it in a drink machine it just was given to me as change and i think it's worth it's worth between 100 and 150 dollars because it's missing all the cladding so that's the copper missing all the clotting on that side okay so for measuring uh then we can just pick a point and move across so if we were measuring the diameter it'd be about there okay and so now i can zoom in on that so you see here's a dimension here um here's your dimension for for this way and then your diagonal dimension is in the green which is is here and then down at the bottom you get an area of the square that you created and then if if these are within if these two measurements are within i think 95 then it'll give you an average down here okay so if i wanted to measure this coin i would guess about right there using these pixels and then i could say about you know right there there you go and we're really close at 24 where our average is is 24. okay so to navigate around in this mode what you're doing is you okay right click i'm right clicking on my mouse and it clears everything i'm left clicking to start a measurement i'm dragging it out and you can just use it that way if you right click it'll clear it or you can drag it out and hit it again or right click again and then it locks it in for you okay and you'll see over here there's let me get this out of the way you'll see over here you've got your measurements there here's your pixel stuff up here okay so anyway you can just pick things and measure them yeah just like that okay that's pretty cool um and then but the the cooler one so if you really need to be accurate that's what you got to do okay but the cooler one i think is uh let me clear that out i'm just gonna hit a and a is the auto mode okay so let's uh let's take here's a here's a cool one okay now this is a shiny one so we let's okay before we do that let's do a dark one all right so this is using so it just automatically measures it so this is using the contrast between the background and the coin and it's picking out contours in opencv and then it's measuring the contours okay but if we use something like this it's a little bit harder for it to adjust and so we can adjust the threshold so if you look over here it says threshold to press t so i'm going to press the t and then by moving the mouse if i move the mouse over here threshold goes up do you see this number right here okay so if i can move my mouse so i can start with a really low threshold you can see that mercury dime is okay now we're starting to get okay a little bigger now the shiny thing is picking up all right and then we open it up so right there and then if i go up and down i can change the glo the gaussian blur see this number right here if i go up and down i can go up to 20 blur and that kind of blurs the edges for opencv and helps it helps it do a little better so i like to go about five and you know sometimes you know about right there is pretty good now notice it's not it's measuring on that dark area and that's why you got to try hard to get rid of the shadows so if i get it in here where it doesn't have too many shadows then it will do a pretty good job of of measuring okay so put this dime in here let's see what else we have oh here's queen elizabeth she's very shiny let's see if she uh oh he had a big i'm gonna have to change my gaussian blur or my uh my light changed now if you want to change your light actually down here in for normalize so if i push on in i can adjust things around a little bit this is your default and then you can lighten darken that's as dark as you can get and then this is i think this is as light as you can get down here okay but i'll just put it right there okay so she's shiny see how shiny she is she me she kind of messes everybody up but it'll still work you just got to get your adjustments done you know to so in this case it's picking up that shadow right there so if we brought the you know we just have to bring the threshold to where we want it to make it work reasonably well for what we're doing so the shinier things are the worse the contrast is okay but let's so let's uh set we're going to set the threshold to about you know 120 and then we'll see which ones we can do all right the dark ones do pretty good i'm gonna up the threshold now to what's that change the blur all right that's pretty good all right so we've got those let's see what else we got here's a canadian penny and here's a here's an old u.s penny and last coin we'll do i guess here's a buffalo or uh you know it's got a buffalo on the back side it's an old nickel from the 30s probably 37. okay so this this jumping around is just because of the shinier coins are difficult for the contrast to work out so maybe we need to change i'm going to set the threshold a little bit a little bit higher there we go okay so now we got everything working and then here let's try some other objects here's a uh all right so we're gonna have to adjust now if we took something maybe like this this would probably work yeah so that works okay it's dark you know everything in here is dark whereas with this led what we're picking up the shiny stuff doesn't have enough contrast so we would have to up so i'm hitting t for threshold i'm going to up the threshold and see what we can do all right so we can we can up the threshold to where it does that but it it's hard to deal with that shiny stuff you know so okay now one place where you do have problems is with um things with shadows so this has some depth to it and so what you're getting is the shadow all the way around it let me get rid of some of these other things so you're getting you're picking up the shadow all around it so let's see if we if we drop threshold down you know so you can drop the threshold and just adjust the types of things that you'll pick up see if i put the threshold really wide that quarter see how it expands away from the margins that's really where it needs to be for the quarter but it doesn't work quite right for these other things and of course if you put something that has a i don't know i got my lighting pretty good but do you see how it's picking you know it's just picking up the darkness around the edge you know okay so anyway that's it that's how you measure things it's really simple it's just about figuring the ratio oh i forgot to tell you that when you when we went through that calibration um let's see let's open this it it wrote this file for you so as long as your can camera doesn't change you don't move anything around then the next time it opens up it will still be calibrated all right so all right well here's the let's see how many coins we can get get going on here it's gonna flicker a little bit until we get some stuff here actually i'm gonna i'm gonna set the threshold about there okay so we're gonna get some flicker so there's one that's a two peso there's an old silver coin here's a bicentennial here's the one with no clad here's elizabeth sneaking in here she's shiny she's hard to deal with and here's a mercury dime now notice see how these join together got to spread them apart a little bit okay that's about it that's what it does it measures and then i'm just going to hit a and that's going to get out of the automatic matic and then you know you could do something real much more accurate doing it that way with the regular measurement mode but anyway so a is automatic on a all right i'm quitting now oh
Info
Channel: Clayton Darwin
Views: 17,079
Rating: 4.9851027 out of 5
Keywords: DIY, Homemade, opencv, python, measuring, measurement, triangulation, cnc, precision, camera, webcam, auto, automatic, auto-measuring, ai, artificial intelligence, automatic for the people
Id: 1CVmjTcSpIw
Channel Id: undefined
Length: 25min 17sec (1517 seconds)
Published: Mon Nov 02 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.