此页面由 AI 自动翻译。查看英文原版
DepthAI
软件栈

本页目录

  • 演示
  • 管道
  • 源代码

ImageManip 缩放

Supported on:RVC2RVC4
此示例展示了如何使用 ImageManip 节点来缩放输入图像。 由于宽高比不一致(输入图像:16:9,输出图像:1:1),ResizeMode 会生效,并且输出图像会被拉伸。您可以在此处找到有关缩放模式的更多信息。

演示

此示例需要 DepthAI v3 API,请参阅 安装说明

管道

源代码

Python

Python
GitHub
1import depthai as dai
2import cv2
3
4pipeline = dai.Pipeline()
5
6camRgb = pipeline.create(dai.node.Camera).build(dai.CameraBoardSocket.CAM_A)
7manip = pipeline.create(dai.node.ImageManip)
8
9manip.initialConfig.setOutputSize(300, 300, dai.ImageManipConfig.ResizeMode.STRETCH)
10
11camOut = camRgb.requestOutput((1920, 1080))
12camOut.link(manip.inputImage)
13
14manipQ = manip.out.createOutputQueue()
15camQ = camOut.createOutputQueue()
16
17pipeline.start()
18
19while True:
20    if manipQ.has():
21        cv2.imshow("Manip frame", manipQ.get().getCvFrame())
22    if camQ.has():
23        cv2.imshow("Camera frame", camQ.get().getCvFrame())
24    key = cv2.waitKey(1)
25    if key == ord('q'):
26        break

C++

1#include <atomic>
2#include <csignal>
3
4#include "depthai/depthai.hpp"
5#include "depthai/pipeline/datatype/ImgFrame.hpp"
6#include "depthai/pipeline/node/Camera.hpp"
7#include "depthai/pipeline/node/ImageManip.hpp"
8
9std::atomic<bool> quitEvent(false);
10
11void signalHandler(int) {
12    quitEvent = true;
13}
14
15int main(int argc, char** argv) {
16    signal(SIGTERM, signalHandler);
17    signal(SIGINT, signalHandler);
18
19    std::shared_ptr<dai::Device> device = nullptr;
20    if(argc <= 1) {
21        device = std::make_shared<dai::Device>();
22    } else {
23        device = std::make_shared<dai::Device>(argv[1]);
24    }
25    dai::Pipeline pipeline(device);
26
27    auto camRgb = pipeline.create<dai::node::Camera>()->build(dai::CameraBoardSocket::CAM_A);
28    auto manip = pipeline.create<dai::node::ImageManip>();
29
30    // Resize to 400x400 and avoid stretching by cropping from the center
31    manip->initialConfig->setOutputSize(400, 400, dai::ImageManipConfig::ResizeMode::CENTER_CROP);
32    // Set output frame type
33    manip->initialConfig->setFrameType(dai::ImgFrame::Type::RGB888i);
34
35    camRgb->requestOutput((std::make_pair(1920, 1080)))->link(manip->inputImage);
36    auto outputQueue = manip->out.createOutputQueue();
37
38    pipeline.start();
39    while(pipeline.isRunning() && !quitEvent) {
40        auto imgFrame = outputQueue->get<dai::ImgFrame>();
41        cv::imshow("Resized Frame", imgFrame->getCvFrame());
42        int key = cv::waitKey(1);
43        if(key == 'q') {
44            break;
45        }
46    }
47
48    pipeline.stop();
49    pipeline.wait();
50
51    return 0;
52}

需要帮助?

请前往 Discussion Forum 获取技术支持或提出您可能有的任何其他问题。