In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article will explain in detail how C++ uses opencv to achieve monocular ranging. The editor thinks it is very practical, so I share it with you as a reference. I hope you can get something after reading this article.
With nothing to do, I made a simple monocular ranging with C++. The cv used in the algorithm comes with it, just change the parameters. It realizes three functions: reading photo ranging, reading notebook camera ranging and reading video ranging.
Why not use binocular ranging? Because there is no money to buy a camera.
Principle: the principle of similar triangle, in the case of known focal length, detect the pixel width of the measured object in the picture to judge the distance between the measured object and the camera, at the same time, you can also get the approximate length and width of the measured object. Note: the camera should be parallel to the measured object. If it is not parallel to the side, the connection between the camera and the object will have an angle a with the horizontal direction, and the size of the measured object in the picture will be affected, thus affecting the measurement effect.
Error analysis: there are many reasons for the poor measurement effect, for example, the camera and the tested object are not at the same height, the focal length of the camera is not accurate, and the focal length of the camera captured by different videos is different, so the focal length of the camera needs to be calibrated before measurement, and the traditional calibration method is used here.
Steps:
The focal length is calibrated by ①.
② performs Gaussian filtering on the image (smoothing operation, filtering operation, denoising operation)
③ edge detection
④ draws the detected outline and returns the smallest circumscribed rectangle (can be set to draw the full outline or the largest outline)
⑤ calculation distance
The detection of the camera only needs to analyze the pictures captured by the camera frame by frame, which is equivalent to the analysis of photos, and video is also the same reason. Here I set a variable that holds the minimum distance and the maximum distance, of course, this can only be used as a reference value is not accurate, because the camera and video ranging are not parallel to the measured object.
Demonstration of the effect:
The effect is unstable, for a single picture, sometimes the error is only a few centimeters, sometimes the error is tens of centimeters or even more than one hundred centimeters. The distance measured is the distance from the smallest rectangle to the camera detected in the picture, so it is easy to identify the rectangular picture with clean background, and the recognition is very regular, but for some pictures with cluttered background, the recognition effect is not satisfactory. Here is a comparison of two different pictures.
The corresponding image processing effect is as follows (the actual error is only 1 cm):
For a tree on the balcony:
I don't know how far away I am from this tree. 163cm has been measured, but it must have exceeded this distance.
Code (remember to give a wave of likes and comments after taking the code):
No sub-file preparation, the configuration of opencv can be used directly, the effect is not guaranteed, it is a pleasure.
# include # include using namespace cv;using namespace std / / 2903.85 2438.65 2904.1#define KNOWN_DISTANCE 70.866 / / known distance # define KNOWN_WIDTH 0.787 / / known width 0.787 11.69#define KNOWN_HEIGHT 0.787 / / known height 0.787 8.27 double MaxDistance= DBL_MIN / need to be obtained in advance, # define KNOWN_FOCAL_LENGTH 3000 / / known focal length double MaxDistance= DBL_MIN obtained through the calibration step / / maximum distance double MinDistance= DBL_MAX;// minimum distance void GetPicture (Mat& SrcImage, int choice); void GetCamera (int choice); void GetVideo (int choice); double GetTheDistanceToCamera (double KnownWidth, double FocalLength, double PerWidth); double CalculateFocalDistance (Mat& Image); RotatedRect FindMarker (Mat& SrcImage); A4 Paper size: 210mm×297 mm (16 open paper) int main (int argv, char* argc []) {int choice = 0; cout
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.