根据三角形相似原理计算相机焦距,公式为:F = (P * D) / W
其中:
F: 待求的相机的焦距
P: 图像中目标的宽度,单位像素
D: 真实目标与相机的距离,单位厘米
W: 真实目标的宽度,单位厘米
计算焦距前,首先要有一幅带目标的图像,这里以人脸为例,记下采集此幅图像时,相机与人脸的实际距离D,真实人脸的宽度W,通过HaarCascade计算人脸在图像中的宽度P。
获取相机焦距后,再根据公式:D' = (F * W) / P 即可计算出此目标与相机的实时距离。
测试代码如下:
namespace {bool calculate_image_face_width(cv::CascadeClassifier& face_cascade, const char* image_name, int& P)
{cv::Mat bgr = cv::imread(image_name, 1);if (!bgr.data) {std::cerr << "Error: fail to imread: " << image_name << "\n";return false;}cv::Mat gray;cv::cvtColor(bgr, gray, cv::COLOR_BGR2GRAY);cv::equalizeHist(gray, gray);std::vector<cv::Rect> faces;face_cascade.detectMultiScale(gray, faces);//for (auto i = 0; i < faces.size(); ++i)// cv::rectangle(bgr, faces[i], cv::Scalar(255, 0, 0), 1);//cv::imwrite("../../../data/result.jpg", bgr);if (faces.size() != 1) {std::cerr << "Error: faces size: " << faces.size() << "\n";return false;}P = faces[0].width;return true;
}inline int calculate_focal_length(int P, int D, int W)
{return ((P * D) / W);
}inline int calculate_distance(int F, int W, int P)
{return ((F * W) / P);
}} // namespaceint test_monocular_ranging_face_triangle_similarity()
{
#ifdef _MSC_VERconstexpr char* file_name{ "../../../data/haarcascade_frontalface_alt.xml" };constexpr char* image_name{ "../../../data/images/face/1.jpg" };
#elseconstexpr char* file_name{ "data/haarcascade_frontalface_alt.xml" };constexpr char* image_name{ "data/images/face/1.jpg" };
#endifcv::CascadeClassifier face_cascade;if (!face_cascade.load(file_name)) {std::cerr << "Error: fail to load file:" << file_name << "\n";return -1;}auto P{ 0 };if (!calculate_image_face_width(face_cascade, image_name, P)) {std::cerr << "Error: fail to get_image_face_width\n";return -1;}std::cout << "the width of the face in the image: " << P << " pixels\n";constexpr int D{ 60 }, W{ 18 }; // cmconst auto F = calculate_focal_length(P, D, W);std::cout << "focal length: " << F << "\n";cv::VideoCapture cap(1); // usb cameraif (!cap.isOpened()) {std::cerr << "Error: fail to open capture\n";return -1;}cv::Mat gray;constexpr char* winn_ame{ "Monocular Ranging" };cv::namedWindow(winn_ame, 1);const std::string text{ "Distance = " };for (;;) {cv::Mat frame;cap >> frame; // get a new frame from cameracv::cvtColor(frame, gray, cv::COLOR_BGR2GRAY);cv::equalizeHist(gray, gray);std::vector<cv::Rect> faces;face_cascade.detectMultiScale(gray, faces);for (auto i = 0; i < faces.size(); ++i) {cv::rectangle(frame, faces[i], cv::Scalar(255,0,0), 1);P = faces[i].width;auto D2 = calculate_distance(F, W, P) / 100.; // mauto tmp = std::to_string(D2);auto pos = tmp.find(".");if (pos != std::string::npos)tmp = tmp.substr(0, pos+3);std::string content = text + tmp + " m";cv::putText(frame, content, cv::Point(faces[i].x, faces[i].y - 25), cv::FONT_HERSHEY_SIMPLEX, 1, cv::Scalar(0, 0, 255), 1);}cv::imshow(winn_ame, frame);if (cv::waitKey(30) >= 0)break;}return 0;
}
说明:
(1).通过OpenCV的cv::CascadeClassifier检测人脸;
(2).函数calculate_image_face_width用于计算人脸在图像中的宽度;
(3).函数calculate_focal_length用于计算相机焦距;
(4).函数calculate_distance用于计算人脸与相机的距离。
执行结果截图如下所示:原始图像来自于网络
GitHub:https://github.com/fengbingchun/NN_Test