F = (P x D) / W
As I continue to move my camera both closer and farther away from the object/marker, I can apply the triangle similarity to determine the distance of the object to the camera:
D’ = (W x F) / P
card has known width 0.28ft, camera is also 0.28ft above
apparent width of card on image is 397
focal length is also 397 from fomular 1
canny edge find contour
apparent width is 208 now when camera is lifted
distance is calculated from fouler 2
#main.py
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, 30, 100)
cv2.imshow("canny edge", edged)
# 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)
def distance_to_camera(knownWidth, focalLength, perWidth):
# compute and return the distance from the maker to the camera
return (knownWidth * focalLength) / perWidth
# initialize the known distance from the camera to the object, which
# in this case is 3.4 inches
KNOWN_DISTANCE = 3.4
# initialize the known object width, which in this case, the piece of
# card is 3.4 inches wide
KNOWN_WIDTH = 3.4
# load the furst 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("assets/distance/initial.jpg")
marker = find_marker(image)
print(marker)
focalLength = (marker[1][0] * KNOWN_DISTANCE) / KNOWN_WIDTH
print("focal length is " + str(focalLength))
# loop over the images
for imagePath in sorted(paths.list_images("assets/distance")):
# 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)
print(marker)
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)
reference:
No comments:
Post a Comment