c++调用OAK相机实现rgb彩色图和depth深度图的移动测距

基本思想:学习oak双目相机实现rgb彩色图和depth深度图的移动测距

前言:

设备型号:OAK-D-Pro

一、环境配置参考

台式机ubuntu系统调用OAK相机_MFT小白的博客-CSDN博客

二、python版本

python调用OAK相机实现人脸检测的简单demo_MFT小白的博客-CSDN博客

三、测试

其中 utility 文件夹为oak官网 depthai_cpp_example 文件夹中

获取地址:GitHub - richard-xx/depthai_cpp_example

git clone https://github.com/richard-xx/depthai_cpp_example.git
cmakelist.txt
cmake_minimum_required(VERSION 3.16)
project(test_depthai)

set(CMAKE_CXX_STANDARD 14)
find_package(OpenCV REQUIRED)
find_package(depthai CONFIG REQUIRED)

include_directories(${CMAKE_CURRENT_LIST_DIR}/include)
include_directories(${CMAKE_CURRENT_LIST_DIR}/include/utility)

add_executable(test_depthai main.cpp)

target_link_libraries(test_depthai ${OpenCV_LIBS} depthai::opencv)
main.cpp 
#include <iostream>
#include "depthai/depthai.hpp"
#include "utility.hpp"

#define Rgb 0
#define spatial_location_calculator 1


static constexpr float stepSize = 0.05f;

static std::atomic<bool> newConfig{false};


#if Rgb
int main() {

    dai::Pipeline pipeline;
    auto camRgb = pipeline.create<dai::node::ColorCamera>();
    auto xoutVideo = pipeline.create<dai::node::XLinkOut>();

    xoutVideo->setStreamName("video");

    camRgb->setBoardSocket(dai::CameraBoardSocket::RGB);
    camRgb->setResolution(dai::ColorCameraProperties::SensorResolution::THE_1080_P);
    camRgb->setVideoSize(1920, 1080);

    camRgb->video.link(xoutVideo->input);

    xoutVideo->input.setBlocking(false);
    xoutVideo->input.setQueueSize(1);

    dai::Device device(pipeline);

    auto video = device.getOutputQueue("video");

    while (true) {
        auto videoIn = video->get<dai::ImgFrame>();

        cv::Mat frame = videoIn->getCvFrame();

        cv::imshow("video", frame);
        int key = cv::waitKey(1);
        if (key == 'q' || key = 27) {
            return 0;
        }
    }

    return 0;
}


#elif spatial_location_calculator
int main() {
    using namespace std;

    // Create pipeline
    dai::Pipeline pipeline;
    dai::Device device;

    // Define sources and outputs
    auto camRgb = pipeline.create<dai::node::ColorCamera>();
    auto monoLeft = pipeline.create<dai::node::MonoCamera>();
    auto monoRight = pipeline.create<dai::node::MonoCamera>();
    auto stereo = pipeline.create<dai::node::StereoDepth>();
    auto spatialDataCaulator = pipeline.create<dai::node::SpatialLocationCalculator>();

    // Properties
    camRgb->setBoardSocket(dai::CameraBoardSocket::RGB);
    camRgb->setResolution(dai::ColorCameraProperties::SensorResolution::THE_1080_P);
    camRgb->setPreviewSize(1920, 1080);

    monoLeft->setBoardSocket(dai::CameraBoardSocket::LEFT);
    monoLeft->setResolution(dai::MonoCameraProperties::SensorResolution::THE_720_P);
    monoRight->setBoardSocket(dai::CameraBoardSocket::RIGHT);
    monoRight->setResolution(dai::MonoCameraProperties::SensorResolution::THE_720_P);

    try {
        auto calibData = device.readCalibration2();
        auto lensPosition = calibData.getLensPosition(dai::CameraBoardSocket::RGB);
        if (lensPosition) {
            camRgb->initialControl.setManualFocus(lensPosition);
        }
    }   catch (const std::exception &ex) {
            std::cout << ex.what() << std::endl;
            return 1;
    }

    bool lrcheck = true;
    bool subpixel = true;

    stereo->setDefaultProfilePreset(dai::node::StereoDepth::PresetMode::HIGH_ACCURACY);
    stereo->setLeftRightCheck(lrcheck);
    stereo->setSubpixel(subpixel);
    stereo->setDepthAlign(dai::CameraBoardSocket::RGB);

    // config
    dai::Point2f topLeft(0.4f, 0.4f);
    dai::Point2f bottomRight(0.6f, 0.6f);

    dai::SpatialLocationCalculatorConfigData config;
    config.depthThresholds.lowerThreshold = 100;
    config.depthThresholds.upperThreshold = 5000;
    config.roi = dai::Rect(topLeft, bottomRight);

    spatialDataCaulator->initialConfig.addROI(config);
    spatialDataCaulator->inputConfig.setWaitForMessage(false);


    auto xoutRgb = pipeline.create<dai::node::XLinkOut>();
    auto xoutDepth = pipeline.create<dai::node::XLinkOut>();
    auto xouSpatialData = pipeline.create<dai::node::XLinkOut>();
    auto xinspatialCalcConfig = pipeline.create<dai::node::XLinkIn>();

    xoutRgb->setStreamName("rgb");
    xoutDepth->setStreamName("depth");
    xouSpatialData->setStreamName("spatialData");
    xinspatialCalcConfig->setStreamName("spatialCalcConfig");


    // Linking
    camRgb->video.link(xoutRgb->input);
    monoLeft->out.link(stereo->left);
    monoRight->out.link(stereo->right);

    stereo->depth.link(spatialDataCaulator->inputDepth);
    spatialDataCaulator->passthroughDepth.link(xoutDepth->input);


    spatialDataCaulator->out.link(xouSpatialData->input);
    xinspatialCalcConfig->out.link(spatialDataCaulator->inputConfig);

    // Connect to device and start pipeline
    device.startPipeline(pipeline);


    // Output queue will be used to get the depth frames from the outputs defined above
    auto rgbQueue = device.getOutputQueue("rgb",8,false);
    auto depthQueue = device.getOutputQueue("depth", 8, false);
    auto spatialCalcQueue = device.getOutputQueue("spatialData", 8, false);
    auto spatialCalcConfigInQueue = device.getInputQueue("spatialCalcConfig");


    auto color = cv::Scalar(255,255,255);

    std::cout << "Use WASD keys to move ROI!" << std::endl;

    while (true) {
        auto inRgb = rgbQueue->get<dai::ImgFrame>();
        auto inDepth = depthQueue->get<dai::ImgFrame>();

        cv::Mat rgbFrame = inRgb->getCvFrame();
        cv::Mat depthFrame = inDepth->getCvFrame();

        cv::Mat depthFrameColor;
        cv::normalize(depthFrame, depthFrameColor, 255, 0, cv::NORM_INF, CV_8UC1);
        cv::equalizeHist(depthFrameColor, depthFrameColor);
        cv::applyColorMap(depthFrameColor, depthFrameColor, cv::COLORMAP_JET);

        auto spatialData = spatialCalcQueue->get<dai::SpatialLocationCalculatorData>()->getSpatialLocations();
        for(auto depthData : spatialData) {
            auto roi = depthData.config.roi;
            roi = roi.denormalize(depthFrameColor.cols, depthFrameColor.rows);
            auto xmin = (int)roi.topLeft().x;
            auto ymin = (int)roi.topLeft().y;
            auto xmax = (int)roi.bottomRight().x;
            auto ymax = (int)roi.bottomRight().y;

            auto depthMin = depthData.depthMin;
            auto depthMax = depthData.depthMax;

            cv::rectangle(depthFrameColor, cv::Rect(cv::Point(xmin, ymin), cv::Point(xmax, ymax)), color, cv::FONT_HERSHEY_SIMPLEX);
            std::stringstream depthX;
            depthX << "X: " << (int)depthData.spatialCoordinates.x << " mm";
            cv::putText(depthFrameColor, depthX.str(), cv::Point(xmin + 10, ymin + 20), cv::FONT_HERSHEY_TRIPLEX, 0.5, color);
            std::stringstream depthY;
            depthY << "Y: " << (int)depthData.spatialCoordinates.y << " mm";
            cv::putText(depthFrameColor, depthY.str(), cv::Point(xmin + 10, ymin + 35), cv::FONT_HERSHEY_TRIPLEX, 0.5, color);
            std::stringstream depthZ;
            depthZ << "Z: " << (int)depthData.spatialCoordinates.z << " mm";
            cv::putText(depthFrameColor, depthZ.str(), cv::Point(xmin + 10, ymin + 50), cv::FONT_HERSHEY_TRIPLEX, 0.5, color);

            cv::rectangle(rgbFrame, cv::Rect(cv::Point(xmin, ymin), cv::Point(xmax, ymax)), color, cv::FONT_HERSHEY_SIMPLEX);
            std::stringstream depthX_;
            depthX_ << "X: " << (int)depthData.spatialCoordinates.x << " mm";
            cv::putText(rgbFrame, depthX_.str(), cv::Point(xmin + 10, ymin + 20), cv::FONT_HERSHEY_TRIPLEX, 0.5, color);
            std::stringstream depthY_;
            depthY_ << "Y: " << (int)depthData.spatialCoordinates.y << " mm";
            cv::putText(rgbFrame, depthY_.str(), cv::Point(xmin + 10, ymin + 35), cv::FONT_HERSHEY_TRIPLEX, 0.5, color);
            std::stringstream depthZ_;
            depthZ_ << "Z: " << (int)depthData.spatialCoordinates.z << " mm";
            cv::putText(rgbFrame, depthZ_.str(), cv::Point(xmin + 10, ymin + 50), cv::FONT_HERSHEY_TRIPLEX, 0.5, color);
        }

        // Show the frame
        cv::imshow("rgb", rgbFrame);
        cv::imshow("depth", depthFrameColor);

        int key = cv::waitKey(1);
        switch(key) {
            case 'q':
                return 0;
            case 'w':
                if (topLeft.y - stepSize >= 0) {
                    topLeft.y -= stepSize;
                    bottomRight.y -= stepSize;
                    newConfig = true;
                }
                break;
            case 'a':
                if (topLeft.x - stepSize >= 0) {
                    topLeft.x -= stepSize;
                    bottomRight.x -= stepSize;
                    newConfig = true;
                }
                break;
            case 's':
                if (bottomRight.y + stepSize <= 1) {
                    topLeft.y += stepSize;
                    bottomRight.y += stepSize;
                    newConfig = true;
                }
                break;
            case 'd':
                if (bottomRight.x + stepSize <= 1) {
                    topLeft.x += stepSize;
                    bottomRight.x += stepSize;
                    newConfig = true;
                }
                break;
            default:
                break;
        }

        if(newConfig) {
            config.roi = dai::Rect(topLeft, bottomRight);
            dai::SpatialLocationCalculatorConfig cfg;
            cfg.addROI(config);
            spatialCalcConfigInQueue->send(cfg);
            newConfig = false;
        }
    }

    return 0;
}

#endif
效果图:

参考文章:

1. https://github.com/richard-xx/depthai_cpp_example/blob/main/examples/SpatialDetection/spatial_location_calculator.cpp

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: OAK-D-Pro 是一款功能强大的智能深度相机,具有高度的智能化和灵活性。它搭载了 OpenCV、OpenVINO、TensorFlow 等多种先进的 AI 框架和算法,可以用于人脸识别、姿势识别、手势识别、物体检测、场景分析等多种应用场景。 OAK-D-Pro 还支持多种连接方式,包括 USB、HDMI、以太网等,方便与不同的设备进行联接。同时,它也具有较高的处理性能和低功耗,可以在较长时间内进行运算。 总之,OAK-D-Pro 是一款非常优秀的智能深度相机,可以广泛应用于各种 AI 领域,具有很高的性价比。 ### 回答2: 智能深度相机OAK-D Pro是一款功能强大的相机,具有出色的效果和性能。 首先,OAK-D Pro采用了先进的深度感知技术,能够实时获取环境中的深度信息。通过双目立体视觉和深度学习算法的结合,它能够高精度地感知和测量物体的三维空间位置和形状。这使得OAK-D Pro在目标检测、位姿估计、实时地生成等任务中表现出色。 其次,OAK-D Pro具备较高的像分辨率和像质量。它搭载了高清摄像头,能够捕捉到更多细节,并保持像的清晰度和真实感。这使得其在计算机视觉应用中可以更准确地分析和处理像。 此外,OAK-D Pro还具备强大的计算能力和算法处理能力。其内置的高性能处理器和深度学习推理芯片可以快速高效地处理和分析像数据。这使得OAK-D Pro能够在实时应用场景中保持稳定且高效的性能表现。 最后,OAK-D Pro还支持多种接口和数据输出方式,可以与其他设备和平台进行无缝集成。这为用户提供了更大的灵活性,可以在各种应用中充分发挥OAK-D Pro的效能。 综上所述,智能深度相机OAK-D Pro具有优秀的深度感知能力、高清影像质量、强大的计算能力和多样化的数据输出接口。其出色的效果使得它在计算机视觉和人工智能领域有着广泛的应用前景。 ### 回答3: 智能深度相机OAK-D Pro是一款高性能的相机产品,具有出色的效果。它采用了深度学习算法,可以实现更精确的深度感知,并与计算机视觉技术相结合,实现高质量的像和视频输出。 OAK-D Pro的主要特点之一是其高分辨率和高帧率。它配备了一个强大的传感器,能够捕捉到更多细节,并以每秒30帧的速度进行像和视频的采集。这意味着用户可以获得更清晰、更流畅的像和视频体验。 此外,OAK-D Pro还具备智能边缘计算的能力。它内置了强大的处理器和神经网络,能够在设备上进行实时的像和视频分析,无需依赖云端的计算资源。这种边缘计算的方式大大提高了响应速度,并能够实现更高效的应用,比如目标检测、人脸识别等。 此外,OAK-D Pro还支持深度数据的获取和处理。它能够测量出场景中每个像素的距离,并生成精确的深度图像。这为各种应用提供了更多可能性,比如虚拟现实、增强现实等。 综上所述,智能深度相机OAK-D Pro具有出色的性能和效果。它不仅能够提供高质量的像和视频输出,还能够进行智能边缘计算和深度数据处理,满足用户在各种应用场景中的需求。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值