Python distance to object
- Image Resizing using OpenCV | Python
- Python OpenCV | cv2.erode() method
- Python | Image blurring using OpenCV
- Python OpenCV | cv2.copyMakeBorder() method
- Python | Grayscaling of Images using OpenCV
- Image Processing in Python (Scaling, Rotating, Shifting and Edge Detection)
- Erosion and Dilation of images using OpenCV in python
- OpenCV Python Program to analyze an image using Histogram
- Histograms Equalization in OpenCV
- Python | Thresholding techniques using OpenCV | Set-1 (Simple Thresholding)
- Python | Thresholding techniques using OpenCV | Set-2 (Adaptive Thresholding)
- Python | Thresholding techniques using OpenCV | Set-3 (Otsu Thresholding)
- OpenCV: Segmentation using Thresholding
- Python OpenCV | cv2.cvtColor() method
- Filter Color with OpenCV
- Python | Denoising of colored images using opencv
- Python | Visualizing image in different color spaces
- Find Co-ordinates of Contours using OpenCV | Python
- Python | Bilateral Filtering
- Image Inpainting using OpenCV
- Python | Intensity Transformation Operations on Images
- Python | Image Registration using OpenCV
- Python | Background subtraction using OpenCV
- Background Subtraction in an Image using Concept of Running Average
- Python | Foreground Extraction in an Image using Grabcut Algorithm
- Python | Morphological Operations in Image Processing (Opening) | Set-1
- Python | Morphological Operations in Image Processing (Closing) | Set-2
- Python | Morphological Operations in Image Processing (Gradient) | Set-3
- Image segmentation using Morphological operations in Python
- Image Translation using OpenCV | Python
- Image Pyramid using OpenCV | Python
- Python | Program to extract frames using OpenCV
- Displaying the coordinates of the points clicked on the image using Python-OpenCV
- White and black dot detection using OpenCV | Python
- Python | OpenCV BGR color palette with trackbars
- Draw a rectangular shape and extract objects using Python’s OpenCV
- Invisible Cloak using OpenCV | Python Project
- ML | Unsupervised Face Clustering Pipeline
- Saving Operated Video from a webcam using OpenCV
- Face Detection using Python and OpenCV with webcam
- Opening multiple color windows to capture using OpenCV in Python
- Python | Play a video in reverse mode using OpenCV
- Template matching using OpenCV in Python
- Cartooning an Image using OpenCV – Python
- Vehicle detection using OpenCV Python
- Count number of Faces using Python – OpenCV
- Live Webcam Drawing using OpenCV
- Detect and Recognize Car License Plate from a video in real time
- Build GUI Application Pencil Sketch from Photo in Python
- Python OpenCV – Drowsiness Detection
- Face Alignment with OpenCV and Python
- Age Detection using Deep Learning in OpenCV
- Right and Left Hand Detection Using Python
- OpenCV Python: How to detect if a window is closed?
- Save frames of live video with timestamps – Python OpenCV
- Detecting low contrast images with OpenCV, scikit-image, and Python
- Animate image using OpenCV in Python
- Drawing a cross on an image with OpenCV
- Blur and anonymize faces with OpenCV and Python
- Face detection using Cascade Classifier using OpenCV-Python
- Real time object color detection using OpenCV
- Python – Writing to video with OpenCV
- Add image to a live camera feed using OpenCV-Python
- Face and Hand Landmarks Detection using Python – Mediapipe, OpenCV
- Emotion Based Music Player – Python Project
- Realtime Distance Estimation Using OpenCV – Python
- Webcam QR code scanner using OpenCV
- Color Identification in Images using Python – OpenCV
- Real-Time Edge Detection using OpenCV in Python | Canny edge detection method
- Opencv Python program for Face Detection
- Image Resizing using OpenCV | Python
- Python OpenCV | cv2.erode() method
- Python | Image blurring using OpenCV
- Python OpenCV | cv2.copyMakeBorder() method
- Python | Grayscaling of Images using OpenCV
- Image Processing in Python (Scaling, Rotating, Shifting and Edge Detection)
- Erosion and Dilation of images using OpenCV in python
- OpenCV Python Program to analyze an image using Histogram
- Histograms Equalization in OpenCV
- Python | Thresholding techniques using OpenCV | Set-1 (Simple Thresholding)
- Python | Thresholding techniques using OpenCV | Set-2 (Adaptive Thresholding)
- Python | Thresholding techniques using OpenCV | Set-3 (Otsu Thresholding)
- OpenCV: Segmentation using Thresholding
- Python OpenCV | cv2.cvtColor() method
- Filter Color with OpenCV
- Python | Denoising of colored images using opencv
- Python | Visualizing image in different color spaces
- Find Co-ordinates of Contours using OpenCV | Python
- Python | Bilateral Filtering
- Image Inpainting using OpenCV
- Python | Intensity Transformation Operations on Images
- Python | Image Registration using OpenCV
- Python | Background subtraction using OpenCV
- Background Subtraction in an Image using Concept of Running Average
- Python | Foreground Extraction in an Image using Grabcut Algorithm
- Python | Morphological Operations in Image Processing (Opening) | Set-1
- Python | Morphological Operations in Image Processing (Closing) | Set-2
- Python | Morphological Operations in Image Processing (Gradient) | Set-3
- Image segmentation using Morphological operations in Python
- Image Translation using OpenCV | Python
- Image Pyramid using OpenCV | Python
- Python | Program to extract frames using OpenCV
- Displaying the coordinates of the points clicked on the image using Python-OpenCV
- White and black dot detection using OpenCV | Python
- Python | OpenCV BGR color palette with trackbars
- Draw a rectangular shape and extract objects using Python’s OpenCV
- Invisible Cloak using OpenCV | Python Project
- ML | Unsupervised Face Clustering Pipeline
- Saving Operated Video from a webcam using OpenCV
- Face Detection using Python and OpenCV with webcam
- Opening multiple color windows to capture using OpenCV in Python
- Python | Play a video in reverse mode using OpenCV
- Template matching using OpenCV in Python
- Cartooning an Image using OpenCV – Python
- Vehicle detection using OpenCV Python
- Count number of Faces using Python – OpenCV
- Live Webcam Drawing using OpenCV
- Detect and Recognize Car License Plate from a video in real time
- Build GUI Application Pencil Sketch from Photo in Python
- Python OpenCV – Drowsiness Detection
- Face Alignment with OpenCV and Python
- Age Detection using Deep Learning in OpenCV
- Right and Left Hand Detection Using Python
- OpenCV Python: How to detect if a window is closed?
- Save frames of live video with timestamps – Python OpenCV
- Detecting low contrast images with OpenCV, scikit-image, and Python
- Animate image using OpenCV in Python
- Drawing a cross on an image with OpenCV
- Blur and anonymize faces with OpenCV and Python
- Face detection using Cascade Classifier using OpenCV-Python
- Real time object color detection using OpenCV
- Python – Writing to video with OpenCV
- Add image to a live camera feed using OpenCV-Python
- Face and Hand Landmarks Detection using Python – Mediapipe, OpenCV
- Emotion Based Music Player – Python Project
- Realtime Distance Estimation Using OpenCV – Python
- Webcam QR code scanner using OpenCV
- Color Identification in Images using Python – OpenCV
- Real-Time Edge Detection using OpenCV in Python | Canny edge detection method
- Opencv Python program for Face Detection
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 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
def midpoint(ptA, ptB): return ((ptA[0] + ptB[0]) * 0.5, (ptA[1] + ptB[1]) * 0.5)
image = cv2.imread('images/test.jpg') gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) gray = cv2.GaussianBlur(gray, (7, 7), 0)
edged = cv2.Canny(gray, 50, 100) edged = cv2.dilate(edged, None, iterations=1) edged = cv2.erode(edged, None, iterations=1)
cnts = cv2.findContours(edged.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) cnts = imutils.grab_contours(cnts)
(cnts, _) = contours.sort_contours(cnts) colors = ((0, 0, 255), (240, 0, 159), (0, 165, 255), (255, 255, 0), (255, 0, 255)) refObj = None
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)])
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, "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: Thank you all.