<jupyter_start><jupyter_text>Triangle Similarity for Object/Marker to Camera DistanceIn order to determine the distance from our camera to a known object or marker, we are going to utilize triangle similarity.Given a marker or object with a known width W.When this marker is some distance D from our camera.And a picture of our object is taken from a camera is analysed to obtain the apparent width in pixels P. Then we can derive the perceived focal length F of our camera`F = (P x  D) / W`Place a standard piece of 8.5 x 11in piece of paper (horizontally; W = 11) D = 24 inches in front of a camera and capture the image. Measure the width of the piece of paper in the image Let the perceived width of the paper be P = 248 pixels. let focal length F be calculated:`F = (248px x 24in) / 11in = 543.45`Continue to move the camera both closer and farther away from the object/marker, and capply the triangle similarity to determine the distance of the object to the camera:`D’ = (W x F) / P`Let the camera position be 3 ft (or 36 inches) away from the marker.Let the perceived width of the piece of the marker be 170 pixels. Let Distance be calculated:`D’ = (11in x 543.45) / 170 = 35in`Or roughly 36 inches, which is 3 feet.<jupyter_code># import the necessary packages
from imutils import paths
import numpy as np
import imutils
import cv2

def find_marker(image):
    # convert the image to grayscale, blur it, and detect edges
    gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
    gray = cv2.GaussianBlur(gray, (5, 5), 0)
    edged = cv2.Canny(gray, 35, 125)
    # find the contours in the edged image and keep the largest one;
    # we'll assume that this is our piece of paper in the image
    cnts = cv2.findContours(edged.copy(), cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)
    cnts = imutils.grab_contours(cnts)
    c = max(cnts, key = cv2.contourArea)
    # compute the bounding box of the of the paper region and return it
    return cv2.minAreaRect(c)
# This function takes a knownWidth of the marker, a computed focalLength , and perceived width of an object in an 
# image (measured in pixels), and applies the triangle similarity detailed above to compute the actual distance to
# the object.

def distance_to_camera(knownWidth, focalLength, perWidth):
    # compute and return the distance from the maker to the camera
    return (knownWidth * focalLength) / perWidth<jupyter_output><empty_output><jupyter_text>Use a single image to calibrate with<jupyter_code># initialize the known distance from the camera to the object, which
# in this case is 24 inches
KNOWN_DISTANCE = 24.0
# initialize the known object width, which in this case, the piece of
# paper is 12 inches wide
KNOWN_WIDTH = 11.0
# load the image that contains an object that is KNOWN TO BE 2 feet
# from our camera, then find the paper marker in the image, and initialize
# the focal length
image = cv2.imread("images/2ft.png")
marker = find_marker(image)
focalLength = (marker[1][0] * KNOWN_DISTANCE) / KNOWN_WIDTH<jupyter_output><empty_output><jupyter_text>Now that we have a calibration then we can make judgements about other images based on this calibration<jupyter_code># loop over all the test images
for imagePath in sorted(paths.list_images("images")):
    # load the image, find the marker in the image, then compute the
    # distance to the marker from the camera
    image = cv2.imread(imagePath)
    marker = find_marker(image)
    inches = distance_to_camera(KNOWN_WIDTH, focalLength, marker[1][0])
    # draw a bounding box around the image and display it
    box = cv2.cv.BoxPoints(marker) if imutils.is_cv2() else cv2.boxPoints(marker)
    box = np.int0(box)
    cv2.drawContours(image, [box], -1, (0, 255, 0), 2)
    cv2.putText(image, "%.2fft" % (inches / 12),
        (image.shape[1] - 200, image.shape[0] - 20), cv2.FONT_HERSHEY_SIMPLEX,
        2.0, (0, 255, 0), 3)
    cv2.imshow("image", image)
    cv2.waitKey(0)

cv2.destroyAllWindows()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.07529589524049358, 'fraction_numerical': 0.029463611181062704, 'mean_word_length': 4.1450777202072535, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '464438', 'license': 'MIT', 'n_tokens_mistral': 1291, 'n_tokens_neox': 1187, 'n_words': 537, 'path': 'notebooks/distance-to-camera.ipynb', 'repo_name': 'ronheywood/opencv', 'size': '3971'}