|
| 1 | +#include <atomic> |
| 2 | +#include <cmath> |
| 3 | +#include <csignal> |
| 4 | +#include <iomanip> |
| 5 | +#include <iostream> |
| 6 | +#include <opencv2/opencv.hpp> |
| 7 | +#include <optional> |
| 8 | +#include <sstream> |
| 9 | +#include <string> |
| 10 | +#include <utility> |
| 11 | +#include <vector> |
| 12 | + |
| 13 | +#include "depthai/depthai.hpp" |
| 14 | + |
| 15 | +const std::string DISTORTED_WINDOW = "CAM_A distorted 640x480"; |
| 16 | +const std::string UNDISTORTED_WINDOW = "CAM_A undistorted 1000x400"; |
| 17 | +const std::pair<uint32_t, uint32_t> DISTORTED_SIZE = {640, 480}; |
| 18 | +const std::pair<uint32_t, uint32_t> UNDISTORTED_SIZE = {1000, 400}; |
| 19 | + |
| 20 | +std::atomic<bool> quitEvent(false); |
| 21 | +std::optional<cv::Point> selectedPoint = std::nullopt; |
| 22 | + |
| 23 | +void signalHandler(int) { |
| 24 | + quitEvent = true; |
| 25 | +} |
| 26 | + |
| 27 | +void onLeftClick(int event, int x, int y, int flags, void* param) { |
| 28 | + (void)flags; |
| 29 | + (void)param; |
| 30 | + |
| 31 | + if(event == cv::EVENT_LBUTTONDOWN) { |
| 32 | + selectedPoint = cv::Point(x, y); |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +cv::Mat toColorFrame(const cv::Mat& frame) { |
| 37 | + if(frame.channels() == 3) { |
| 38 | + return frame; |
| 39 | + } |
| 40 | + |
| 41 | + cv::Mat colorFrame; |
| 42 | + cv::cvtColor(frame, colorFrame, cv::COLOR_GRAY2BGR); |
| 43 | + return colorFrame; |
| 44 | +} |
| 45 | + |
| 46 | +void addStatusLines(cv::Mat& frame, const std::vector<std::string>& lines, const cv::Scalar& color = cv::Scalar(255, 0, 255)) { |
| 47 | + for(size_t index = 0; index < lines.size(); ++index) { |
| 48 | + cv::putText(frame, |
| 49 | + lines[index], |
| 50 | + cv::Point(10, 28 + static_cast<int>(index) * 24), |
| 51 | + cv::FONT_HERSHEY_SIMPLEX, |
| 52 | + 0.65, |
| 53 | + color, |
| 54 | + 2, |
| 55 | + cv::LINE_AA); |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | +bool isInsideFrame(const dai::Point2f& point, const cv::Mat& frame) { |
| 60 | + return 0 <= point.x && point.x < frame.cols && 0 <= point.y && point.y < frame.rows; |
| 61 | +} |
| 62 | + |
| 63 | +void drawPoint(cv::Mat& frame, const std::optional<dai::Point2f>& point, const cv::Scalar& color) { |
| 64 | + if(!point.has_value() || !isInsideFrame(*point, frame)) { |
| 65 | + return; |
| 66 | + } |
| 67 | + |
| 68 | + const cv::Point intPoint(static_cast<int>(std::lround(point->x)), static_cast<int>(std::lround(point->y))); |
| 69 | + if(intPoint.x < 0 || intPoint.x >= frame.cols || intPoint.y < 0 || intPoint.y >= frame.rows) { |
| 70 | + return; |
| 71 | + } |
| 72 | + |
| 73 | + cv::drawMarker(frame, intPoint, color, cv::MARKER_CROSS, 16, 2); |
| 74 | + cv::circle(frame, intPoint, 6, color, 1); |
| 75 | +} |
| 76 | + |
| 77 | +std::string formatPointStatus(const std::string& prefix, const dai::Point2f& point) { |
| 78 | + std::ostringstream stream; |
| 79 | + stream << std::fixed << std::setprecision(1) << prefix << ": (" << point.x << ", " << point.y << ")"; |
| 80 | + return stream.str(); |
| 81 | +} |
| 82 | + |
| 83 | +int main() { |
| 84 | + signal(SIGTERM, signalHandler); |
| 85 | + signal(SIGINT, signalHandler); |
| 86 | + |
| 87 | + dai::Pipeline pipeline; |
| 88 | + |
| 89 | + auto camera = pipeline.create<dai::node::Camera>()->build(dai::CameraBoardSocket::CAM_A); |
| 90 | + auto* distorted = camera->requestOutput(DISTORTED_SIZE, std::nullopt, dai::ImgResizeMode::CROP, std::nullopt, false); |
| 91 | + auto* undistorted = camera->requestOutput(UNDISTORTED_SIZE, std::nullopt, dai::ImgResizeMode::CROP, std::nullopt, true); |
| 92 | + |
| 93 | + auto distQueue = distorted->createOutputQueue(); |
| 94 | + auto undistQueue = undistorted->createOutputQueue(); |
| 95 | + |
| 96 | + cv::namedWindow(DISTORTED_WINDOW); |
| 97 | + cv::namedWindow(UNDISTORTED_WINDOW); |
| 98 | + cv::setMouseCallback(DISTORTED_WINDOW, onLeftClick); |
| 99 | + |
| 100 | + std::cout << "Left click in the distorted CAM_A window to remap a point to the undistorted output." << std::endl; |
| 101 | + std::cout << "Press 'c' to clear the point and 'q' to quit." << std::endl; |
| 102 | + |
| 103 | + pipeline.start(); |
| 104 | + while(pipeline.isRunning() && !quitEvent) { |
| 105 | + auto distortedFrame = distQueue->get<dai::ImgFrame>(); |
| 106 | + auto undistortedFrame = undistQueue->get<dai::ImgFrame>(); |
| 107 | + |
| 108 | + if(distortedFrame == nullptr || undistortedFrame == nullptr) { |
| 109 | + continue; |
| 110 | + } |
| 111 | + |
| 112 | + if(!distortedFrame->validateTransformations() || !undistortedFrame->validateTransformations()) { |
| 113 | + std::cerr << "Invalid transformations!" << std::endl; |
| 114 | + continue; |
| 115 | + } |
| 116 | + |
| 117 | + auto& distortedTransform = distortedFrame->getTransformation(); |
| 118 | + auto& undistortedTransform = undistortedFrame->getTransformation(); |
| 119 | + |
| 120 | + cv::Mat distortedView = toColorFrame(distortedFrame->getCvFrame()); |
| 121 | + cv::Mat undistortedView = toColorFrame(undistortedFrame->getCvFrame()); |
| 122 | + |
| 123 | + std::optional<dai::Point2f> sourcePoint = std::nullopt; |
| 124 | + std::optional<dai::Point2f> remappedPoint = std::nullopt; |
| 125 | + std::string sourceStatus = "Click in this window to select a point"; |
| 126 | + std::string targetStatus = "Waiting for a selected point"; |
| 127 | + |
| 128 | + if(selectedPoint.has_value()) { |
| 129 | + sourcePoint = dai::Point2f(static_cast<float>(selectedPoint->x), static_cast<float>(selectedPoint->y)); |
| 130 | + remappedPoint = distortedTransform.remapPointTo(undistortedTransform, *sourcePoint); |
| 131 | + sourceStatus = formatPointStatus("Source", *sourcePoint); |
| 132 | + targetStatus = formatPointStatus("Remapped", *remappedPoint); |
| 133 | + if(!isInsideFrame(*remappedPoint, undistortedView)) { |
| 134 | + targetStatus += " outside target frame"; |
| 135 | + } |
| 136 | + } |
| 137 | + |
| 138 | + drawPoint(distortedView, sourcePoint, cv::Scalar(0, 255, 0)); |
| 139 | + drawPoint(undistortedView, remappedPoint, cv::Scalar(0, 255, 255)); |
| 140 | + |
| 141 | + addStatusLines(distortedView, {"Distorted CAM_A output", sourceStatus, "Press c to clear, q to quit"}); |
| 142 | + addStatusLines(undistortedView, |
| 143 | + {"Undistorted CAM_A output", |
| 144 | + targetStatus, |
| 145 | + "Target size: " + std::to_string(UNDISTORTED_SIZE.first) + "x" + std::to_string(UNDISTORTED_SIZE.second)}); |
| 146 | + |
| 147 | + cv::imshow(DISTORTED_WINDOW, distortedView); |
| 148 | + cv::imshow(UNDISTORTED_WINDOW, undistortedView); |
| 149 | + |
| 150 | + const int key = cv::waitKey(1); |
| 151 | + if(key == 'q') { |
| 152 | + pipeline.stop(); |
| 153 | + break; |
| 154 | + } |
| 155 | + if(key == 'c') { |
| 156 | + selectedPoint = std::nullopt; |
| 157 | + } |
| 158 | + } |
| 159 | + |
| 160 | + pipeline.stop(); |
| 161 | + pipeline.wait(); |
| 162 | + return 0; |
| 163 | +} |
0 commit comments