How to calculate the distance between the objects in the image with Python

Today I want to show you how to calculate the distance between the objects in the image. We will write an awesome algorithm that you can modify and extend to your needs.

This is our test image:

Let’s jump to the code!

First, we need to import t…


This content originally appeared on DEV Community and was authored by Stokry

Today I want to show you how to calculate the distance between the objects in the image. We will write an awesome algorithm that you can modify and extend to your needs.

This is our test image:

enter image description here

Let's jump to the code!

First, we need to import the necessary packages:

from scipy.spatial import distance as dist
from imutils import perspective
from imutils import contours
import numpy as np
import argparse
import imutils
import cv2

Then we construct the argument parse and parse the arguments

def midpoint(ptA, ptB):
    return ((ptA[0] + ptB[0]) * 0.5, (ptA[1] + ptB[1]) * 0.5)

after that we load the image, convert it to grayscale:

image = cv2.imread('images/test.jpg')
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
gray = cv2.GaussianBlur(gray, (7, 7), 0)

then we perform edge detection and close gaps in between object edges:

edged = cv2.Canny(gray, 50, 100)
edged = cv2.dilate(edged, None, iterations=1)
edged = cv2.erode(edged, None, iterations=1)

find contours in the edge map

cnts = cv2.findContours(edged.copy(), cv2.RETR_EXTERNAL,
                        cv2.CHAIN_APPROX_SIMPLE)
cnts = imutils.grab_contours(cnts)

then initialize the distance colors and reference object:

(cnts, _) = contours.sort_contours(cnts)
colors = ((0, 0, 255), (240, 0, 159), (0, 165, 255), (255, 255, 0),
          (255, 0, 255))
refObj = None

then we loop over the contours individually:

for c in cnts:
    if cv2.contourArea(c) < 100:
        continue
    box = cv2.minAreaRect(c)
    box = cv2.cv.BoxPoints(box) if imutils.is_cv2() else cv2.boxPoints(box)
    box = np.array(box, dtype="int")
    box = perspective.order_points(box)
    cX = np.average(box[:, 0])
    cY = np.average(box[:, 1])
    if refObj is None:
        (tl, tr, br, bl) = box
        (tlblX, tlblY) = midpoint(tl, bl)
        (trbrX, trbrY) = midpoint(tr, br)
        D = dist.euclidean((tlblX, tlblY), (trbrX, trbrY))
        refObj = (box, (cX, cY), D / 70)
        continue
    orig = image.copy()
    cv2.drawContours(orig, [box.astype("int")], -1, (0, 255, 0), 2)
    cv2.drawContours(orig, [refObj[0].astype("int")], -1, (0, 255, 0), 2)
    refCoords = np.vstack([refObj[0], refObj[1]])
    objCoords = np.vstack([box, (cX, cY)])

then we loop over the original points:

for ((xA, yA), (xB, yB), color) in zip(refCoords, objCoords, colors):
        cv2.circle(orig, (int(xA), int(yA)), 5, color, -1)
        cv2.circle(orig, (int(xB), int(yB)), 5, color, -1)
        cv2.line(orig, (int(xA), int(yA)), (int(xB), int(yB)),
                 color, 2)
        D = dist.euclidean((xA, yA), (xB, yB)) / refObj[2]
        (mX, mY) = midpoint((xA, yA), (xB, yB))
        cv2.putText(orig, "{:.1f}in".format(D), (int(mX), int(mY - 10)),
                    cv2.FONT_HERSHEY_SIMPLEX, 0.55, color, 2)
        cv2.imshow("Image", orig)
        cv2.waitKey(0)
cv2.destroyAllWindows()

This is our final result:

enter image description here

Thank you all.


This content originally appeared on DEV Community and was authored by Stokry


Print Share Comment Cite Upload Translate Updates
APA

Stokry | Sciencx (2021-05-12T18:09:57+00:00) How to calculate the distance between the objects in the image with Python. Retrieved from https://www.scien.cx/2021/05/12/how-to-calculate-the-distance-between-the-objects-in-the-image-with-python/

MLA
" » How to calculate the distance between the objects in the image with Python." Stokry | Sciencx - Wednesday May 12, 2021, https://www.scien.cx/2021/05/12/how-to-calculate-the-distance-between-the-objects-in-the-image-with-python/
HARVARD
Stokry | Sciencx Wednesday May 12, 2021 » How to calculate the distance between the objects in the image with Python., viewed ,<https://www.scien.cx/2021/05/12/how-to-calculate-the-distance-between-the-objects-in-the-image-with-python/>
VANCOUVER
Stokry | Sciencx - » How to calculate the distance between the objects in the image with Python. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/05/12/how-to-calculate-the-distance-between-the-objects-in-the-image-with-python/
CHICAGO
" » How to calculate the distance between the objects in the image with Python." Stokry | Sciencx - Accessed . https://www.scien.cx/2021/05/12/how-to-calculate-the-distance-between-the-objects-in-the-image-with-python/
IEEE
" » How to calculate the distance between the objects in the image with Python." Stokry | Sciencx [Online]. Available: https://www.scien.cx/2021/05/12/how-to-calculate-the-distance-between-the-objects-in-the-image-with-python/. [Accessed: ]
rf:citation
» How to calculate the distance between the objects in the image with Python | Stokry | Sciencx | https://www.scien.cx/2021/05/12/how-to-calculate-the-distance-between-the-objects-in-the-image-with-python/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.