Transfer: https://www.cnblogs.com/fpzs/p/9513932.html
1 Introduction:
/******************************************************************* The project is implemented using monocular camera distance measurement, first monocular camera and the like kinect maximum depth camera The difference is not effectively obtain depth information, it is first of all this in a bid to try to get people away from the camera with the image from. Read online a few days on camera calibration and camera focal length principle and other articles, then this article will really inspire I: opencv with python and to measure the distance from the target to the main principle is to use the camera's distance meter similar triangles Count objects to the distance from the camera. https://blog.csdn.net/m0_37811342/article/details/80394935 http://python.jobbole.com/84378/ ********************************************************************/
2. monocular ranging principle
/*********************************************************************
We will calculate the distance to the camera a known or target object using similar triangles. It is similar triangles Such a thing: Suppose we have a width W of target or object. Then we will put that goal away from us D camera for the position of. We take pictures of an object with a camera and measuring the pixel width P of the object. We have A focal length of the camera equation: F = (P x D) / W For example, suppose I am away from the camera distance D = 24 inches place to put a standard 8.5 x 11 inch paper in A4 (Sideways put; W = 11) and take a picture. I measured the pixel width of A4 paper photograph P = 249 pixels. So I F is the focal length: F = (248px x 24in) / 11in = 543.45 As I continued my stay away or moved closer to the camera or the target object, I can use similar triangles of the object from the calculated phase Machine distance: D '= (W x F) / P From the above explanation, we can see that, in order to get away, we have to know the size of the target object and the focal length of the camera, These two known conditions according to the formula: D '= (W x F) / P Target to obtain the distance D of the camera, where P is a pixel distance, W is the width of A4 paper, F is the focal length of the camera. ********************************************************************/
3. monocular ranging Python code (with comments)
1 import numpy as np 2 import cv2 # 3 to find the target function 4 def find_marker(image): 5 # convert the image to grayscale, blur it, and detect edges # 6 converts the image to grayscale, and detects an edge blur 7 gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) 8 gray = cv2.GaussianBlur(gray, (5, 5), 0) 9 edged = cv2.Canny(gray, 35, 125) 10 11 # find the contours in the edged image and keep the largest one; # 12 profile found at the edge of the image and maintain maximum profile 13 # we'll assume that this is our piece of paper in the image # 14 We assume this is our piece of paper in the image 15 (_, cnts, _) = cv2.findContours(edged.copy(), cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE) # 16 seeking the maximum area 17 c = max(cnts, key = cv2.contourArea) 18 19 # compute the bounding box of the of the paper region and return it 20 # paper calculated area bounding box and returns it 21 # cv2.minAreaRect () c representative point set, returns rect [0] is the smallest circumscribed rectangle center coordinates, 22 # rect [1] [0] is width, rect [1] [1] is a height, rect [2] is the angle 23 return cv2.minAreaRect(c) 24 25 # distance calculation function 26 def distance_to_camera(knownWidth, focalLength, perWidth): 27 # compute and return the distance from the maker to the camera 28 # calculates and returns the distance from the target to the camera 29 return (knownWidth * focalLength) / perWidth 30 31 # initialize the known distance from the camera to the object, which 32 # in this case is 24 inches # 33 initializes a known distance from the camera to the object, in this case 24 inches 34 KNOWN_DISTANCE = 24.0 35 36 # initialize the known object width, which in this case, the piece of 37 # paper is 11 inches wide 38 # initialization known object width, in this case, the paper 11 inches wide. 39 # A4 paper length and width (unit: inches) 40 KNOWN_WIDTH = 11.69 41 KNOWN_HEIGHT = 8.27 42 43 # initialize the list of images that we'll be using # 44 we will initialize the image list to be used 45 IMAGE_PATHS = ["Picture1.jpg", "Picture2.jpg", "Picture3.jpg"] 46 47 # load the furst image that contains an object that is KNOWN TO BE 2 feet 48 # from our camera, then find the paper marker in the image, and initialize 49 # the focal length # 50 includes a first load images from our camera two feet of the object, and then find the paper mark in the image, and initialize the focal length # 51 is read first figure, the focal length of the camera is calculated by a known distance 52 image = cv2.imread ( "E: \\ lena.jpg") # should be taken with the camera in FIG. 53 marker = find_marker(image) 54 focalLength = (marker[1][0] * KNOWN_DISTANCE) / KNOWN_WIDTH # D’ = (W x F) / P 55 # 56 acquired through the camera calibration of the focal length of the pixel 57 #focalLength = 811.82 58 print('focalLength = ',focalLength) 59 Open the Camera # 60 61 camera = cv2.VideoCapture(0) 62 63 while camera.isOpened(): 64 # get a frame 65 (grabbed, frame) = camera.read() 66 marker = find_marker(frame) 67 if marker == 0: 68 print(marker) 69 continue 70 inches = distance_to_camera(KNOWN_WIDTH, focalLength, marker[1][0]) 71 72 # draw a bounding box around the image and display it # 73 drawing a bounding box around the image and display it 74 box = cv2.boxPoints(marker) 75 box = np.int0(box) 76 cv2.drawContours(frame, [box], -1, (0, 255, 0), 2) 77 78 # inches converted to cm 79 cv2.putText(frame, "%.2fcm" % (inches *30.48/ 12), 80 (frame.shape[1] - 200, frame.shape[0] - 20), cv2.FONT_HERSHEY_SIMPLEX, 81 2.0, (0, 255, 0), 3) 82 83 # show a frame 84 cv2.imshow("capture", frame) 85 if cv2.waitKey(1) & 0xFF == ord('q'): 86 break 87 camera.release() 88 cv2.destroyAllWindows()
4. monocular ranging Python code (pure code)
1 import numpy as np 2 import cv2 3 def find_marker(image): 4 gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) 5 gray = cv2.GaussianBlur(gray, (5, 5), 0) 6 edged = cv2.Canny(gray, 35, 125) 7 8 (_, cnts, _) = cv2.findContours(edged.copy(), cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE) 9 c = max(cnts, key = cv2.contourArea) 10 return cv2.minAreaRect(c) 11 12 def distance_to_camera(knownWidth, focalLength, perWidth): 13 return (knownWidth * focalLength) / perWidth 14 15 KNOWN_DISTANCE = 24.0 16 KNOWN_WIDTH = 11.69 17 KNOWN_HEIGHT = 8.27 18 IMAGE_PATHS = ["Picture1.jpg", "Picture2.jpg", "Picture3.jpg"] 19 image = cv2.imread("E:\\lena.jpg") 20 marker = find_marker(image) 21 focalLength = (marker[1][0] * KNOWN_DISTANCE) / KNOWN_WIDTH 22 23 print('focalLength = ',focalLength) 24 camera = cv2.VideoCapture(0) 25 while camera.isOpened(): 26 (grabbed, frame) = camera.read() 27 marker = find_marker(frame) 28 if marker == 0: 29 print(marker) 30 continue 31 inches = distance_to_camera(KNOWN_WIDTH, focalLength, marker[1][0]) 32 box = cv2.boxPoints(marker) 33 box = np.int0(box) 34 cv2.drawContours(frame, [box], -1, (0, 255, 0), 2) 35 cv2.putText(frame, "%.2fcm" % (inches *30.48/ 12), 36 (frame.shape[1] - 200, frame.shape[0] - 20), cv2.FONT_HERSHEY_SIMPLEX, 37 2.0, (0, 255, 0), 3) 38 cv2.imshow("capture", frame) 39 if cv2.waitKey(1) & 0xFF == ord('q'): 40 break 41 camera.release() 42 cv2.destroyAllWindows()