rgb_rotate_warp.cpp
Go to the documentation of this file.
1 #include <iostream>
2 
3 #include "depthai/depthai.hpp"
4 #include "utility.hpp"
5 
6 static constexpr auto keyRotateDecr = 'z';
7 static constexpr auto keyRotateIncr = 'x';
8 static constexpr auto keyResizeInc = 'v';
9 static constexpr auto keyWarpTestCycle = 'c';
10 
11 void printControls() {
12  printf("\n=== Controls:\n");
13  printf(" %c -rotated rectangle crop, decrease rate\n", keyRotateDecr);
14  printf(" %c -rotated rectangle crop, increase rate\n", keyRotateIncr);
15  printf(" %c -warp 4-point transform, cycle through modes\n", keyWarpTestCycle);
16  printf(" %c -resize cropped region, or disable resize\n", keyResizeInc);
17  printf(" h -print controls (help)\n");
18 }
19 
20 static constexpr auto ROTATE_RATE_MAX = 5.0f;
21 static constexpr auto ROTATE_RATE_INC = 0.1f;
22 
23 static constexpr auto RESIZE_MAX_W = 800;
24 static constexpr auto RESIZE_MAX_H = 600;
25 static constexpr auto RESIZE_FACTOR_MAX = 5;
26 
27 /* The crop points are specified in clockwise order,
28  * with first point mapped to output top-left, as:
29  * P0 -> P1
30  * ^ v
31  * P3 <- P2
32  */
33 static const dai::Point2f P0 = {0, 0}; // top-left
34 static const dai::Point2f P1 = {1, 0}; // top-right
35 static const dai::Point2f P2 = {1, 1}; // bottom-right
36 static const dai::Point2f P3 = {0, 1}; // bottom-left
38  std::vector<dai::Point2f> points;
40  const char* description;
41 };
42 
43 std::vector<warpFourPointTest> warpList = {
44  //{{{ 0, 0},{ 1, 0},{ 1, 1},{ 0, 1}}, true, "passthrough"},
45  //{{{ 0, 0},{639, 0},{639,479},{ 0,479}}, false,"passthrough (pixels)"},
46  {{P0, P1, P2, P3}, true, "1. passthrough"},
47  {{P3, P0, P1, P2}, true, "2. rotate 90"},
48  {{P2, P3, P0, P1}, true, "3. rotate 180"},
49  {{P1, P2, P3, P0}, true, "4. rotate 270"},
50  {{P1, P0, P3, P2}, true, "5. horizontal mirror"},
51  {{P3, P2, P1, P0}, true, "6. vertical flip"},
52  {{{-0.1f, -0.1f}, {1.1f, -0.1f}, {1.1f, 1.1f}, {-0.1f, 1.1f}}, true, "7. add black borders"},
53  {{{-0.3f, 0}, {1, 0}, {1.3f, 1}, {0, 1}}, true, "8. parallelogram transform"},
54  {{{-0.2f, 0}, {1.8f, 0}, {1, 1}, {0, 1}}, true, "9. trapezoid transform"},
55 };
56 
57 int main() {
58  // Create pipeline
59  dai::Pipeline pipeline;
60 
61  // Define sources and outputs
62  auto camRgb = pipeline.create<dai::node::ColorCamera>();
63  auto manip = pipeline.create<dai::node::ImageManip>();
64 
65  auto camOut = pipeline.create<dai::node::XLinkOut>();
66  auto manipOut = pipeline.create<dai::node::XLinkOut>();
67  auto manipCfg = pipeline.create<dai::node::XLinkIn>();
68 
69  camOut->setStreamName("preview");
70  manipOut->setStreamName("manip");
71  manipCfg->setStreamName("manipCfg");
72 
73  // Properties
74  camRgb->setPreviewSize(640, 480);
76  camRgb->setInterleaved(false);
77  camRgb->setColorOrder(dai::ColorCameraProperties::ColorOrder::BGR);
78  manip->setMaxOutputFrameSize(2000 * 1500 * 3);
79 
80  // Linking
81  camRgb->preview.link(camOut->input);
82  camRgb->preview.link(manip->inputImage);
83  manip->out.link(manipOut->input);
84  manipCfg->out.link(manip->inputConfig);
85 
86  // Connect to device and start pipeline
87  dai::Device device(pipeline);
88 
89  // Create input & output queues
90  auto qPreview = device.getOutputQueue("preview", 8, false);
91  auto qManip = device.getOutputQueue("manip", 8, false);
92  auto qManipCfg = device.getInputQueue("manipCfg");
93 
94  std::vector<decltype(qPreview)> frameQueues{qPreview, qManip};
95 
96  // keep processing data
97  int key = -1;
98  float angleDeg = 0;
99  float rotateRate = 1.0;
100  int resizeFactor = 0;
101  int resizeX = 0;
102  int resizeY = 0;
103  bool testFourPt = false;
104  int warpIdx = -1;
105 
106  printControls();
107 
108  while(key != 'q') {
109  if(key >= 0) {
110  printf("Pressed: %c | ", key);
111  if(key == keyRotateDecr || key == keyRotateIncr) {
112  if(key == keyRotateDecr) {
113  if(rotateRate > -ROTATE_RATE_MAX) rotateRate -= ROTATE_RATE_INC;
114  } else if(key == keyRotateIncr) {
115  if(rotateRate < ROTATE_RATE_MAX) rotateRate += ROTATE_RATE_INC;
116  }
117  testFourPt = false;
118  printf("Crop rotated rectangle, rate: %g degrees", rotateRate);
119  } else if(key == keyResizeInc) {
120  resizeFactor++;
121  if(resizeFactor > RESIZE_FACTOR_MAX) {
122  resizeFactor = 0;
123  printf("Crop region not resized");
124  } else {
125  resizeX = RESIZE_MAX_W / resizeFactor;
126  resizeY = RESIZE_MAX_H / resizeFactor;
127  printf("Crop region resized to: %d x %d", resizeX, resizeY);
128  }
129  } else if(key == keyWarpTestCycle) {
130  resizeFactor = 0; // Disable resizing initially
131  warpIdx = (warpIdx + 1) % warpList.size();
132  printf("Warp 4-point transform: %s", warpList[warpIdx].description);
133  testFourPt = true;
134  } else if(key == 'h') {
135  printControls();
136  }
137  printf("\n");
138  }
139 
140  // Send an updated config with continuous rotate, or after a key press
141  if(key >= 0 || (!testFourPt && std::abs(rotateRate) > 0.0001)) {
143  if(testFourPt) {
144  cfg.setWarpTransformFourPoints(warpList[warpIdx].points, warpList[warpIdx].normalizedCoords);
145  } else {
146  angleDeg += rotateRate;
147  dai::RotatedRect rr = {{320, 240}, // center
148  {640, 480}, //{400, 400}, // size
149  angleDeg};
150  cfg.setCropRotatedRect(rr, false);
151  }
152  if(resizeFactor > 0) {
153  cfg.setResize(resizeX, resizeY);
154  }
155  // cfg.setWarpBorderFillColor(255, 0, 0);
156  // cfg.setWarpBorderReplicatePixels();
157  qManipCfg->send(cfg);
158  }
159 
160  for(const auto& q : frameQueues) {
161  auto img = q->get<dai::ImgFrame>();
162  auto mat = toMat(img->getData(), img->getWidth(), img->getHeight(), 3, 1);
163  cv::imshow(q->getName(), mat);
164  }
165  key = cv::waitKey(1);
166  }
167  return 0;
168 }
dai::node::XLinkOut
XLinkOut node. Sends messages over XLink.
Definition: XLinkOut.hpp:14
RESIZE_MAX_W
static constexpr auto RESIZE_MAX_W
Definition: rgb_rotate_warp.cpp:23
ROTATE_RATE_MAX
static constexpr auto ROTATE_RATE_MAX
Definition: rgb_rotate_warp.cpp:20
dai::Pipeline
Represents the pipeline, set of nodes and connections between them.
Definition: Pipeline.hpp:100
printControls
void printControls()
Definition: rgb_rotate_warp.cpp:11
dai::RotatedRect
RotatedRect structure.
Definition: RotatedRect.hpp:14
dai::node::ColorCamera::inputConfig
Input inputConfig
Definition: ColorCamera.hpp:48
keyResizeInc
static constexpr auto keyResizeInc
Definition: rgb_rotate_warp.cpp:8
main
int main()
Definition: rgb_rotate_warp.cpp:57
dai::node::ColorCamera
ColorCamera node. For use with color sensors.
Definition: ColorCamera.hpp:16
keyRotateDecr
static constexpr auto keyRotateDecr
Definition: rgb_rotate_warp.cpp:6
dai::Point2f
Definition: Point2f.hpp:16
warpFourPointTest::normalizedCoords
bool normalizedCoords
Definition: rgb_rotate_warp.cpp:39
ROTATE_RATE_INC
static constexpr auto ROTATE_RATE_INC
Definition: rgb_rotate_warp.cpp:21
RESIZE_MAX_H
static constexpr auto RESIZE_MAX_H
Definition: rgb_rotate_warp.cpp:24
warpList
std::vector< warpFourPointTest > warpList
Definition: rgb_rotate_warp.cpp:43
warpFourPointTest::description
const char * description
Definition: rgb_rotate_warp.cpp:40
toMat
cv::Mat toMat(const std::vector< uint8_t > &data, int w, int h, int numPlanes, int bpp)
Definition: utility.cpp:45
dai::Device::getOutputQueue
std::shared_ptr< DataOutputQueue > getOutputQueue(const std::string &name)
Definition: Device.cpp:86
P3
static const dai::Point2f P3
Definition: rgb_rotate_warp.cpp:36
dai::ImageManipConfig::setResize
ImageManipConfig & setResize(int w, int h)
Definition: ImageManipConfig.cpp:114
dai::node::XLinkOut::input
Input input
Definition: XLinkOut.hpp:27
depthai.hpp
P2
static const dai::Point2f P2
Definition: rgb_rotate_warp.cpp:35
dai::Pipeline::create
std::shared_ptr< N > create()
Definition: Pipeline.hpp:145
keyWarpTestCycle
static constexpr auto keyWarpTestCycle
Definition: rgb_rotate_warp.cpp:9
dai::ImageManipConfig::setCropRotatedRect
ImageManipConfig & setCropRotatedRect(RotatedRect rr, bool normalizedCoords=true)
Definition: ImageManipConfig.cpp:38
dai::ColorCameraProperties::ColorOrder::BGR
@ BGR
warpFourPointTest::points
std::vector< dai::Point2f > points
Definition: rgb_rotate_warp.cpp:38
keyRotateIncr
static constexpr auto keyRotateIncr
Definition: rgb_rotate_warp.cpp:7
dai::ImgFrame
Definition: ImgFrame.hpp:25
dai::node::ImageManip
ImageManip node. Capability to crop, resize, warp, ... incoming image frames.
Definition: ImageManip.hpp:15
P1
static const dai::Point2f P1
Definition: rgb_rotate_warp.cpp:34
dai::ColorCameraProperties::SensorResolution::THE_1080_P
@ THE_1080_P
1920 × 1080
dai::ImageManipConfig::setWarpTransformFourPoints
ImageManipConfig & setWarpTransformFourPoints(std::vector< Point2f > pt, bool normalizedCoords)
Definition: ImageManipConfig.cpp:48
warpFourPointTest
Definition: rgb_rotate_warp.cpp:37
P0
static const dai::Point2f P0
Definition: rgb_rotate_warp.cpp:33
dai::Device
Definition: Device.hpp:21
dai::node::ColorCamera::preview
Output preview
Definition: ColorCamera.hpp:69
dai::node::XLinkIn
XLinkIn node. Receives messages over XLink.
Definition: XLinkIn.hpp:14
dai::ImageManipConfig
Definition: ImageManipConfig.hpp:23
dai::Device::getInputQueue
std::shared_ptr< DataInputQueue > getInputQueue(const std::string &name)
Definition: Device.cpp:120
dai::node::XLinkOut::setStreamName
void setStreamName(const std::string &name)
Definition: XLinkOut.cpp:13
utility.hpp
RESIZE_FACTOR_MAX
static constexpr auto RESIZE_FACTOR_MAX
Definition: rgb_rotate_warp.cpp:25
dai::ImageManipConfig::get
dai::RawImageManipConfig get() const
Definition: ImageManipConfig.cpp:288


depthai
Author(s): Martin Peterlin
autogenerated on Sat Mar 22 2025 02:58:19