# perform edge detection, then perform a dilation + erosion toĮdged = cv2.dilate(edged, None, iterations=1)Įdged = cv2.erode(edged, None, iterations=1)Ĭnts = cv2.findContours(py(), cv2.RETR_EXTERNAL, Gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) Next, we need to preprocess our image: # load the image, convert it to grayscale, and blur it slightly We need two switches here: -image, which is the path to the input image containing the objects we want to measure, and -width, the width (in inches) of our reference object. Lines 14-19 parse our command line arguments.
Otherwise, you should upgrade to the latest version ( 0.3.6 at the time of this writing) so you have the updated order_points function: $ pip install -upgrade imutils If you don’t already have the imutils package installed, stop now to install it: $ pip install imutils We start by importing our required Python packages on Lines 2-8.
Our code here is near identical to last week. Help="width of the left-most object in the image (in inches)") # construct the argument parse and parse the argumentsĪp.add_argument("-i", "-image", required=True,Īp.add_argument("-w", "-width", type=float, required=True, Open up a new file, name it distance_between.py, and insert the following code: # import the necessary packagesįrom scipy.spatial import distance as dist Let’s go ahead and get this example started. Defining our reference object and computing distances Our goal in this image is to (1) find the quarter and then (2) use the dimensions of the quarter to measure the distance between the quarter and all other objects. We’ll also ensure that our quarter is always the left-most object in our image, thereby satisfying Property #2: Figure 1: We’ll identify our reference object based on location, hence we’ll always ensure our quarter is the left-most object in the image. Just as we did last week, we’ll be using a US quarter as our reference object which has a width of 0.955 inches (satisfying Property #1). Property #2: We can easily find and identify the reference object in our image.Property #1: We know the dimensions of the object in some measurable unit (such as inches, millimeters, etc.).Looking for the source code to this post? Jump Right To The Downloads Section Measuring distance between objects in an image with OpenCVĬomputing the distance between objects is very similar to computing the size of objects in an image - it all starts with the reference object.Īs detailed in our previous blog post, our reference object should have two important properties: