REAL TIME OBJECT MEASUREMENT | OpenCV Python (2020)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey everyone welcome to my channel in this video we are going to learn how to perform object measurement using open CV and Buyten we will use an a4 paper as our guide and find the width and the height of objects placed in this region I upload videos on a weekly basis so don't forget to subscribe and hit that like button if you find this video useful so let's get started so let's get started with the code first we are going to head on to our settings and import our packages required and we'll go to the interpreter and we will add open CV and numpy so open CV Python and then we will install nan pi okay so once they are done we are going to import CV - and then we are going to import numpy as NP so we will be using webcam as well as an image so we will start off with the image first so we have here one dot jpg and we can see that we have a four paper here and we also have two cards that we are going to detect the size of so let's put our settings here and we will say that webcam is equals to false in the beginning because we are only going to use an image and we will define the path of our image as one dot jpg and then we will define our camera object so camera we will define with capture and CV - dot dot video capture and we will define the ID so in this case it will be zero and then we can set some parameters such as width height and brightness so we can write gap dot set for each one of them they have a different ID for brightness we have ten so let's keep that as 160 and then for the width and the height we have three for the width and we can write one nine to zero and then for the height we have four when we can put it 1080 so this will add our settings and then we can simply say while true and here we can write success and here we can write image is equals to capital to speed and we can write C v2 dot weight key and we will give it a delay of one milliseconds and we also want to show so CV to dot I am show and we will say that this is our output or let's make it original and we will give it the image so if we run this it should output the webcam so let's run that and we have an error why do we have an error let me check my camera yeah there you go so you can see my camera feed I just plugged it out and plugged it in back I guess the ID was not correct maybe it was one so when I plugged it again it became zero again anyways so next we are going to use our image so we have this flag that we can use we can say that if webcam then use this else our image is equals to I am read CB to not I am read and we will give our path so in this case if we put this as false now it should give us an image so this is our original image that we are getting so when we run this we are not actually getting the complete picture because it's too large so what we can do is we can resize this image before it's displayed so during the process it will not be resized just to display it will be resized so we can say IMG is equal to C v2 dot resize and we want to resize our do we want to change pixels no not really so we'll put zero zero and none and then we will give it a scale so let's say 0.5 and 0.5 so this will scale it down and there you go so this is our original image that we have so the idea here is that we want to detect this paper this a4 paper and we know that the size of this a4 paper is a 297 in the height and 210 in the width so this is millimeters so based on this information we can extract the image of our paper and we can scale it we can give it exactly the dimensions of 297 by 210 and based on that we can find the objects inside of this paper and then based on their pixels we can define what is the size of each of these objects so the first thing we have to do is to extract the paper itself now we can use the contour method to actually find the biggest contour which will be our paper in this case so in the previous videos we actually have used this functionality of finding contours and finding the biggest one but we didn't actually create a generic function that we can use in different projects so today what we will do is we will create this generic function so that we can use it in different projects as we go along so what we can do is we can create a new file item file and we will call this U Telus and all the utilities are all the functions that will be used will be written over here so the idea here is that we input a colored image to our function and whatever process is required to finding the contours it will apply those processes and then it will give us an output of where all the objects are in a nicely formatted way so let's start off by writing our function so here we will import cv2 and we will import numpy as NP now we are going to define the function to get our contour so we'll say define get on tours and then the first thing that we will need is our image our original image so first we are going to convert it into grayscale so we will say image gray is equal to CB 2 dot C V T color and then we are going to give our image and we will say c v2 dot color BG are two gray so this will convert it into grayscale next we are going to apply some blur so image blur is equals to C v2 dot Gaussian blur and then we will say image gray and then we are going to define the size of the kernel which is 5 by 5 let's say and then we are going to define the Sigma which is let's say 1 so if we don't get good results we can always come back and change these parameters so next we are going to apply our canny edge detector to find all the edges so we will write image canny is equals to C be 2 dots canny and then we will send our blur image and we are going to define a threshold now for the threshold I think it's better to ask the user to define it so we can say canny threshold is equals to let's say 100 by 100 this is by default and if the user requires to change it they can using this parameter so over here we can write in the threshold at zero and then the threshold at one so this will give us our canny image so we can display this we can write C v2 dot I am sure and we can write canny and we can write image okay so this can be useful and what we can do we can write a flag here that if we want to show the canny image or not so we can write here show Connie is equals to true or we should keep it false by default and then we can write here if show canny and there we go so now we need to call this function to see if it's act if it actually works up to low so we will go back and before the resizing here we are going to write so we will write your Tillis dots oh we didn't import it so we will write here import utility over here we write it silly starts get contours we'll give it our image and we will keep the threshold as it is and we will say show caning is equal to true you want to see the outputs so let's run that and there you go so we are getting our canny image and it seems to be good so next we are going to apply some dilation and erosion just to be on the safe side so that we get good edges so image dilation is equals to C v2 dot dilate dilate and we are going to send our image canny and for this we need to define a kernel so we will use numpy so kernel is equals to numpy ones and we have to define the kernel size so we will say five by five and over here we will write kernel and we have to define the iterations so we will put them as three next we are going to apply some erosion so that it becomes thin again so we will write image let's let's call it threshold so this is the final step for thresholding so threshold is equal to CB 2 dot erode and then image dilation and then we apply the same kernel and the iterations this time around we give it as 2 and for the canny image we can just output the image threshold after some processing and we have an error now if this is once and there you go so we are getting our edges quite smoothly now so next we are going to find the contours so for that we have the find contours function so cv2 dot find contours we have to send in our image which is the threshold image then we have to define our mode in this case we need the outer edges so we are going to use the external mode so we will write C v2 dot external and then we are going to use this simple approximation or CB to dot chain approximation as simple and then we have to unpack these sort of contours and hi are okay so now we should have all the contours in our contours variable so next we can loop through these and find out their areas and draw them and see how well we are detecting so we can write here for I in contours we are going to find the area of our contour CB 2 dot area say v2 dot contour area and we will give it I and now we will pass in we will filter out based on a minimum threshold so we'll say if area is greater than minimum area so this minimum area we want the user to define so we will write here minimum area is equals to 1000 and if the user wants to change it they can do that as well so next we are going to find our parameters or parameter is equals to C V 2 dots arc length and we will send in our contour and we also have to mention that it is closed so we will say true and then we are going to find the corner points so we will use aprox as our variable and we will use CB 2 . aprox aprox Poli to find our corner points so we have to define our contour we have to give it the resolution and we have to again define that it is closed lastly we can find the bounding box as well so we can approximate so see v2 dot bounding box the bounding rectangle and we will send in our approximation so all of this will give us the information of our current detected object now what if we only want a certain type of object so in our case we only want rectangles so we know that the rectangles will only have four points for corner points so we can filter that out so we can say that if filter is greater than zero so we can ask the user if they want to filter something so we can right here filter filter is equals to zero by default and if they give in some value then we will filter based on that value so we will say that if the length of our approximation is equals to our filter value then we are going to put it in our final list now we have to create our final list so our final list will be final contours that we are going to send out we are going to return from this function so we will say that final contours dot append and we can append a few things so the first one we can append is the length itself so this is the case where we are filtering but in the case we are not filtering we still want to know the size the shape of our object so we can say length aprox and then we can send an area or then we can also send our approximation points and then we can send our bounding boxes that we have found one else can we send we can also send the actual information this might not be very useful but in case we need it because we are trying to make a generic function so we can do that and then we can say else if that is not the case then you append final contours dot append and then you append exactly the same thing so we copy this and we paste it here this else is not for this if it's for this yes so if we have a value of the filter we are going to filter based on that value and then we will append it to our final contours that we will return to through our function otherwise we will append all of the values all of the contours that we are getting of course given that they meet the minimum area criteria so next something we can do is to sort these contours out based on their size so this information can be very helpful because for example in our case of a4 paper we want to find the biggest contour so the if we sort it out properly we can have the first one as the biggest contour so that will be very helpful so what we can do is we can say that final contours is equals to sorted and we can write we want to sort our final contours now what do we want to sort it based on we want to sort it based on a key now that key is basically the second element which is our area so based on the size of the area we want to sort these out so what what this does is this only inputs a function so we we have to define a name of a function here so what we can do instead we can use lambda so we can write lambda and then we can say X and our second element will be X 1 so X 0 will be the length and X 1 will be the area so so we can write here X 1 and then we want everything to be in ascending order so by default it is in ascending order we want it in descending because we want the biggest one to be the first one so we are going to write reverse is equals to true so here you can see reverse is equals to true so the final thing we can do here is to draw the contours that we have detected so we can see if we are detecting the proper ones or not so that's right if drop now we can ask the user if they want to draw not for contours in our final contours we are going to draw cb2 dot draw contours and we will give it an image so this is our original image and we will say that for each contour we will take the fourth element and then we are going to give it all contours and we will write the color as red and we will give it a thickness of let's say 3 and the draw we have to define up here so by default we can say a draw is equals to false and we can return image and we can return our final contours so here we can write image and then final contours and we want to display so we will write display what it display no it was draw draw is equals to true so let's run that and there you go so these are the contour points that we are getting from our find contours function so now that we need to find our a4 paper what we can do is we can optimize our get contour function that we are calling so that it can easily detect our a4 paper so draw we don't need to draw it but we do need to define the minimum the minimum area because it's going to be really big we can put it as 50,000 and then we also know that it will have 4 points so we can filter the number 4 so it only takes out the objects that have 4 corners so this way we will get much better result and then what we can do is we can find out the biggest contour that we have but before we do that we have to make sure that our contours are detected it's not an empty list actually let me reduce the name of this let's just put cons so we can say that if length of contours is not equals to zero oh then we are going to find our biggest one so biggest now the good thing is that we have already sorted it out using the descending order so the first one should be the biggest one so we can say that contours we are going to get our first one and what we need is the where is it what we need is our approximation so we need the four points at which which are our corner points so this is zero one and two so we need the second element so we will write here 2 so this will give us our points so we can print this out so see if we are getting the correct ones so let's run that and there you go so we are getting four points so one two three and four points so next we are going to use these four corner points and we are going to work our image based on these four corner points so let's get into warping so again we are going to create a new function for that and we can go back to our utilities and here we can write work image and the input should be our image we should have some sort of points and then we also should have some width and height of our final image so we can write width and height so before we actually start warping in our image let's look at the concept so sometimes what happens is that we are getting our points in this sequence where one is our first corner point which is on the top left corner and then - is on the bottom left corner whereas sometimes number two will be on the top left corner so we do not have a particular sequence in which we are detecting our contours so our sequence might be 1 3 2 4 2 1 4 3 it can be anything so what we need to do is we need to sort it out so that it becomes 1 2 3 4 so we always get our first point at our left corner and then we have the second one the top right corner then the bottom left and then the bottom right corner so how can we determine that we are always getting 1 2 3 4 so what we can do is we can find out our points based on summation and subtraction so what does that mean this means that for example our point 1 will be our smallest value why because if we add 0 and 0 for example this is 0 0 and this is width and height then this will be a very smaller value than this over here and this value will be also smaller than this because this will be width and 0 and this will be height and 0 sorry 0 and height so if you add this up and if you add this up the the answer of this one will be the smallest so we can add this up and we can find which one of these has the smallest value and based on that summation we can also check which one of these has the highest value so if we add the width and the height we will have the maximum value and then based on the subtraction we can find where is our width and 0 and where is our 0 and height so let's create a function for that so for now we will say pass and on the top here we are going to define our function so this function we will call this as reorder so define reorder and we are going to send our points in so here we can write for example print points dot shape and I want to show you guys something so my points here and then we can write reorder and points so if we go and do this and copy that we go here were Pidge we are sending our image points and width and height so the width for now I will just give it the random values because I just want to run the function and if we run this now we should get an output so we are getting a shape of 4 by 1 by 2 so this is what our points are in so the 4 we understand because we have 4 values and for each value for each point we have an x and a y so that's why we have two but this one here is redundant so what we will do is we will remove that so the first thing we will do is my points is equals to my points dot reshape and we are going to reshape it to 4 by 2 now we can add our points so as we discussed we can simply say add is equals to my points and we will do a sum in the axis 1 so this will give us the summation of each one of them now the smallest one of these will be our element 0 and the highest one the maximum one will be our corner point of width and height so because we received our point in this shape we have to send them back in the same shape so what we can do is we can say that my new points my point new is equals to non 5.0 like our my points before we actually reshape it then in our my new points so we will say my points new the first element the element zero will be my points we are going to get the actual points based on the minimum the minimum index so lump I thought minimum of our ad so wherever you find the minimum value we will get the index of that and based on that index we are gain we are going to get the points from that index and we will store it to element number zero then we are going to do my points new at number three because these are the width and the height so this will be the maximum so we will say my points we are going to take the index at the maximum of our ad so the same way we are going to use differentiation so we can write differentiation is equal to an umpire dot def my points and we are again going to define our axis so access is one now my points the new one at number one which is our second element will be the index the points that we get from the index and P dot minimum arguments which will be from our differentiation and let me just copy that of typing it again and for the second one it will be the maximum now we can simply return our my new points my points new there you go so now they will be reordered so what we can do is we can write here two points print points and then we can reorder them and we can print it again to see if they work properly let's remove this let's run it so here we can see that we have our initial points so you can see we have seven five five two nine three here one fifty four three zero three and then when we sort them out you can see that 154 and three zero three is clearly the the smallest one so this will be our zero zero then we will have our width and zero and then we will have our zero and height and then we will have our width and height so this is how we are getting after sorting out our values so now we can simply work our image based on these points so we are going to say that our points one is equals to numpy dot float of 32 and this is basically our points and then we are going to say points two is equals to numpy dot float 32 and here we have to define our pattern so the first one will be zero zero and then the second one will be width and 0 with 0 the third one will be zero and height and the fourth one will be width and height so this is our pattern and then we are going to get our matrix so matrix is equals to C v2 dot ket respective transform and then we have to define our points points 1 and points 2 and then we are going to create our image out of this based on this matrix so we will say our image verb is equal to CB 2 dot work perspective and we are going to use our original image and we will use our matrix and our final image size should be width and height so this width and height we are going to define and we can remove this and lastly we can return our image so return image work so let's go back so here we can write our image work is equals to this and the width and the height we have to define now so we know that the size of our a4 paper is basically 210 by 297 so this will be our width and height so we can define here that the width of our paper is equals to 210 and the height of our paper is equals to 297 and what we can do is we don't have to use the exact same pixel values because this if you use this one-to-one ratio the image will be very small 200 pixels by 300 pixels is not a lot of pixels so we will not we will lose a lot of detail so what we can do is we can scale it up a little bit so we can put a scale factor so for example we can put the scale factor as 3 so we can create our image 3 times bigger than this and then when we are doing the calculation or the maths we can use this scale to reduce our we can divide it we can divide everything by 3 so then over here we will write width and height and what we can do is let's put this as false I think by default it was false so we can just remove that and then should we do that should we display image original yeah we should display original image as well so we can display this here so here we can say image work and here we can say word image or we can say a 4 paper No okay so we need to turn on the reorder so here we have just printed that out is equal to reorder points so you can see if we don't reorder them we will not get the correct work and there you go so now you can see the image as I was saying it's very small so we can scale it up so here we have the scale of three we can put that here and we can say scale and we can say scale so now that we run that here we have our main image so here we can notice that we have a little bit of an edge which is not off the paper so we can use some padding to remove the corner pixels so we can go back and here we can write our image work is equals to image work and then we can define a pad and we can write image work dot shape dot shape at 0 minus pad so we are going to remove the pad from each side so we can write here back so we can put it default as 20 and then we have to define so this was for the height and then we have to define for the width but from image verb dot shape add one and we are going to subtract our pad so if we run that now there you go so now we have a much cleaner image now this will not affect any of the calculations that we will do for the distance later on so next we are going to find the objects within our a4 paper so now that we have our a4 paper by the name image werp we can use this to find further contours within this image so we can write here let's say contour to actually let me just copy that let's copy this we can say contour to and image contours let's say - and get contours we are going to get it from image work let's put it 2,000 filter again we will put it as four this time we can also add our custom threshold for our canny so we can write custom threshold is equal to let's say 1550 and for now let's just draw draw is equals to true let's run that we did not display so we have to display it afterwards and we need to display image contours too and there you go so these are the colors that we are detecting and it seems to be pretty good what we can do is we can take the corner points of these contours and based on that we can find out the the measurement of these objects so before we go let's let's draw them properly so we can write here that if our length of contours 2 is not equals to 0 this is exactly what we have done before just a check and then we are going to save for object in our contours tool so we are going to get a few of our contours so we will write here that CV 2 dots poly lines we are going to draw our poly lines so we are going to draw it on image contours 2 then we are going to give it the points so our points are basically the objects at elements number what was it I think element number two and then we are going to put true yes because it is closed and then we are going to give it some color so let's give it green and then we can define a thickness of two so if we let's let's put this as false and if we run that so this time we can see that we have nice good lines covering the complete area of our contour so this means that we are getting our points corner points properly and now based on these corner points what we have to do is we have to define at the length of our the length and height of our objects so let me show you what we can do so let's say that this is our object that we have detected and we have the x1 y1 here and x2 y2 over here so we can use a little bit of maths to actually find out our magnitude of this line so we can subtract them and we can subtract the X 2 by X 1 and Y 2 by y 1 and we can use this simple formula a squared plus B squared is equals to C squared to find the value of C so we can square root our a square plus B Square so this is what we will be doing over here so to do that we are going to create a new function here we are going to define find distance and we have to give it the x1 y1 and then x2 y2 so points 1 and then points too and all we have to do is we have to return point 2 at element 0 minus point 1 at element 0 and then we will square them and then we will add it to again the same thing but element 1 and element 1 and then we will do a square root all off of all of this so we will write here power of 0.5 so this will return us the distance or the magnitude of our line given that we entered the correct points given that we entered the correct points so if we go back here so here we can find the distance so we already know that our points are not in the correct order so we need to reorder them before we can actually use them so we will write here our new points is equals to reorder utila start to reorder and we will send our old points which is ope object adds elements number two so this will reorder them and then based on this we can send these points to our find distance function so we will write utilities dot find distance and point one will be our end points at element 0 and 0 and remember we have to divide this by the scale because now we are doing the calculation so the number of pixels divided by our scale value and then our points to end points is basically 1 and 0 and again divided by scale and what we can do is we can print this out so here we are getting our value ninety point eight seven that is in millimeters so we can divide it by 10 to get our centimeters and we can also round it so that we are getting only up to one decimal place so we can say around and we can say 1 so now we are getting a nice clean value so now we're getting 9.0 so this value now is our weight so we can say our new weight is equals to this and our new height is equals to we will copy this and instead of so let me show you this so to find the width we are using this point and this point so this will be our value and to find the height we are using this point and this point so that one is to zero okay there is an issue with the brackets there you go so now it should be fine so the last thing we can do here is to create our arrows and actually display these values so I've written down some code for that I will just paste it here so it's basically just using arrowed line and put text to actually display so we have to change so we have to change the final image so the one we want to display on there you go and let's run that and there you have it so now you can see we are getting their estimated measurement of our objects you
Info
Channel: Murtaza's Workshop - Robotics and AI
Views: 89,326
Rating: 4.9697642 out of 5
Keywords: object measurement, object measurement using opencv, object measurement opencv, object size opencv, object size, opencv, python, opencv python, opencv python contours, opencv contours, opencv warp, opencv tutorial, object detection, opencv tutorials for beginners
Id: tk9war7_y0Q
Channel Id: undefined
Length: 53min 24sec (3204 seconds)
Published: Sun Jun 14 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.