30#include <unordered_map>
34#include <opencv2/core.hpp>
35#include <opencv2/imgcodecs.hpp>
36#include <opencv2/objdetect.hpp>
37#include <wpi/apriltag/AprilTag.hpp>
38#include <wpi/cs/CvSource.hpp>
39#include <wpi/units/length.hpp>
46 return (T(0) < val) - (val < T(0));
51namespace VideoSimUtil {
57static constexpr wpi::units::meter_t
fieldWidth{8.0137_m};
60 wpi::util::RawFrame frame;
61 wpi::apriltag::AprilTag::Generate36h11AprilTagImage(&frame,
id);
62 cv::Mat markerImage{frame.height, frame.width, CV_8UC1, frame.data,
63 static_cast<size_t>(frame.stride)};
64 cv::Mat markerClone = markerImage.clone();
69 std::unordered_map<int, cv::Mat> retVal{};
88 std::vector<cv::Point2f> retVal{};
89 retVal.emplace_back(cv::Point2f{-0.5f, size.height - 0.5f});
90 retVal.emplace_back(cv::Point2f{size.width - 0.5f, size.height - 0.5f});
91 retVal.emplace_back(cv::Point2f{size.width - 0.5f, -0.5f});
92 retVal.emplace_back(cv::Point2f{-0.5f, -0.5f});
103 cv::Rect2f roi36h11{cv::Point2f{1, 1}, cv::Point2f{8, 8}};
106 roi36h11.width *= scale;
107 roi36h11.height *= scale;
109 for (
size_t i = 0; i < pts.size(); i++) {
110 cv::Point2f pt = pts[i];
111 pts[i] = cv::Point2f{roi36h11.tl().x + pt.x, roi36h11.tl().y + pt.y};
134 video.SetFPS(prop.
GetFPS().to<
int>());
150 int tagId,
const std::vector<cv::Point2f>& dstPoints,
bool antialiasing,
151 cv::Mat& destination) {
157 std::vector<cv::Point2f> tagImageCorners{
GetImageCorners(tagImage.size())};
158 std::vector<cv::Point2f> dstPointMat = dstPoints;
159 cv::Rect boundingRect = cv::boundingRect(dstPointMat);
160 cv::Mat perspecTrf = cv::getPerspectiveTransform(tagPoints, dstPointMat);
161 std::vector<cv::Point2f> extremeCorners{};
162 cv::perspectiveTransform(tagImageCorners, extremeCorners, perspecTrf);
163 boundingRect = cv::boundingRect(extremeCorners);
165 double warpedContourArea = cv::contourArea(extremeCorners);
166 double warpedTagUpscale =
167 std::sqrt(warpedContourArea) / std::sqrt(tagImage.size().area());
168 int warpStrat = cv::INTER_NEAREST;
170 int supersampling = 6;
171 supersampling =
static_cast<int>(std::ceil(supersampling / warpedTagUpscale));
172 supersampling = std::max(std::min(supersampling, 10), 1);
174 cv::Mat scaledTagImage{};
175 if (warpedTagUpscale > 2.0) {
176 warpStrat = cv::INTER_LINEAR;
177 int scaleFactor =
static_cast<int>(warpedTagUpscale / 3.0) + 2;
178 scaleFactor = std::max(std::min(scaleFactor, 40), 1);
179 scaleFactor *= supersampling;
180 cv::resize(tagImage, scaledTagImage, cv::Size{}, scaleFactor, scaleFactor,
184 scaledTagImage = tagImage;
189 boundingRect.width += 2;
190 boundingRect.height += 2;
191 if (boundingRect.x < 0) {
192 boundingRect.width += boundingRect.x;
195 if (boundingRect.y < 0) {
196 boundingRect.height += boundingRect.y;
200 std::min(destination.size().width - boundingRect.x, boundingRect.width);
201 boundingRect.height =
202 std::min(destination.size().height - boundingRect.y, boundingRect.height);
203 if (boundingRect.width <= 0 || boundingRect.height <= 0) {
207 std::vector<cv::Point2f> scaledDstPts{};
208 if (supersampling > 1) {
209 cv::multiply(dstPointMat,
210 cv::Scalar{
static_cast<double>(supersampling),
211 static_cast<double>(supersampling)},
213 boundingRect.x *= supersampling;
214 boundingRect.y *= supersampling;
215 boundingRect.width *= supersampling;
216 boundingRect.height *= supersampling;
218 scaledDstPts = dstPointMat;
221 cv::subtract(scaledDstPts,
222 cv::Scalar{
static_cast<double>(boundingRect.tl().x),
223 static_cast<double>(boundingRect.tl().y)},
225 perspecTrf = cv::getPerspectiveTransform(tagPoints, scaledDstPts);
228 cv::warpPerspective(scaledTagImage, tempRoi, perspecTrf, boundingRect.size(),
231 if (supersampling > 1) {
232 boundingRect.x /= supersampling;
233 boundingRect.y /= supersampling;
234 boundingRect.width /= supersampling;
235 boundingRect.height /= supersampling;
236 cv::resize(tempRoi, tempRoi, boundingRect.size(), 0, 0, cv::INTER_AREA);
239 cv::Mat tempMask{cv::Mat::zeros(tempRoi.size(), CV_8UC1)};
240 cv::subtract(extremeCorners,
241 cv::Scalar{
static_cast<float>(boundingRect.tl().x),
242 static_cast<float>(boundingRect.tl().y)},
244 cv::Point2f tempCenter{};
246 std::accumulate(extremeCorners.begin(), extremeCorners.end(), 0.0,
247 [extremeCorners](
float acc,
const cv::Point2f& p2) {
248 return acc + p2.x / extremeCorners.size();
251 std::accumulate(extremeCorners.begin(), extremeCorners.end(), 0.0,
252 [extremeCorners](
float acc,
const cv::Point2f& p2) {
253 return acc + p2.y / extremeCorners.size();
256 for (
auto& corner : extremeCorners) {
257 float xDiff = corner.x - tempCenter.x;
258 float yDiff = corner.y - tempCenter.y;
261 corner = cv::Point2f{tempCenter.x + xDiff, tempCenter.y + yDiff};
264 std::vector<cv::Point> extremeCornerInt{extremeCorners.begin(),
265 extremeCorners.end()};
266 cv::fillConvexPoly(tempMask, extremeCornerInt, cv::Scalar{255});
268 cv::copyTo(tempRoi, destination(boundingRect), tempMask);
280 const cv::Mat& destination) {
281 double scaleX = destination.size().width / 640.0;
282 double scaleY = destination.size().height / 480.0;
283 double minScale = std::min(scaleX, scaleY);
284 return std::max(thickness480p * minScale, 1.0);
298 const std::vector<cv::Point2f>& dstPoints,
const cv::Scalar& color,
299 cv::Mat& destination) {
301 cv::ellipse(destination, rect, color, -1, cv::LINE_AA);
304static void DrawPoly(
const std::vector<cv::Point2f>& dstPoints,
int thickness,
305 const cv::Scalar& color,
bool isClosed,
306 cv::Mat& destination) {
307 std::vector<cv::Point> intDstPoints{dstPoints.begin(), dstPoints.end()};
308 std::vector<std::vector<cv::Point>> listOfListOfPoints;
309 listOfListOfPoints.emplace_back(intDstPoints);
311 cv::polylines(destination, listOfListOfPoints, isClosed, color, thickness,
314 cv::fillPoly(destination, listOfListOfPoints, color, cv::LINE_AA);
329 int id,
const std::vector<cv::Point2f>& dstPoints, cv::Mat& destination) {
331 DrawPoly(dstPoints,
static_cast<int>(thickness), cv::Scalar{0, 0, 255},
true,
333 cv::Rect2d rect{cv::boundingRect(dstPoints)};
334 cv::Point2d textPt{rect.x + rect.width, rect.y};
335 textPt.x += thickness;
336 textPt.y += thickness;
337 cv::putText(destination, std::to_string(
id), textPt, cv::FONT_HERSHEY_PLAIN,
338 1.5 * thickness, cv::Scalar{0, 200, 0},
339 static_cast<int>(thickness), cv::LINE_AA);
347 std::vector<std::vector<wpi::math::Translation3d>> list;
349 const wpi::units::meter_t sideHt = 19.5_in;
350 const wpi::units::meter_t driveHt = 35_in;
351 const wpi::units::meter_t topHt = 78_in;
354 list.emplace_back(std::vector<wpi::math::Translation3d>{
355 wpi::math::Translation3d{0_m, 0_m, 0_m},
358 wpi::math::Translation3d{0_m,
fieldWidth, 0_m},
359 wpi::math::Translation3d{0_m, 0_m, 0_m}});
362 list.emplace_back(std::vector<wpi::math::Translation3d>{
363 wpi::math::Translation3d{0_m, 0_m, 0_m},
364 wpi::math::Translation3d{0_m, 0_m, sideHt},
365 wpi::math::Translation3d{
fieldLength, 0_m, sideHt},
369 list.emplace_back(std::vector<wpi::math::Translation3d>{
370 wpi::math::Translation3d{
fieldLength, 0_m, sideHt},
375 list.emplace_back(std::vector<wpi::math::Translation3d>{
376 wpi::math::Translation3d{
fieldLength, 0_m, driveHt},
380 list.emplace_back(std::vector<wpi::math::Translation3d>{
381 wpi::math::Translation3d{0_m,
fieldWidth, 0_m},
382 wpi::math::Translation3d{0_m,
fieldWidth, sideHt},
387 list.emplace_back(std::vector<wpi::math::Translation3d>{
388 wpi::math::Translation3d{0_m, 0_m, sideHt},
389 wpi::math::Translation3d{0_m, 0_m, topHt},
390 wpi::math::Translation3d{0_m,
fieldWidth, topHt},
391 wpi::math::Translation3d{0_m,
fieldWidth, sideHt},
393 list.emplace_back(std::vector<wpi::math::Translation3d>{
394 wpi::math::Translation3d{0_m, 0_m, driveHt},
395 wpi::math::Translation3d{0_m,
fieldWidth, driveHt}});
411 std::vector<std::vector<wpi::math::Translation3d>> list;
412 const wpi::units::meter_t subLength =
fieldLength / subdivisions;
413 const wpi::units::meter_t subWidth =
fieldWidth / subdivisions;
415 for (
int i = 0; i < subdivisions; i++) {
416 list.emplace_back(std::vector<wpi::math::Translation3d>{
417 wpi::math::Translation3d{0_m, subWidth * (i + 1), 0_m},
418 wpi::math::Translation3d{
fieldLength, subWidth * (i + 1), 0_m}});
419 list.emplace_back(std::vector<wpi::math::Translation3d>{
420 wpi::math::Translation3d{subLength * (i + 1), 0_m, 0_m},
421 wpi::math::Translation3d{subLength * (i + 1),
fieldWidth, 0_m}});
445 const std::vector<wpi::math::Translation3d>& trls,
double resolution,
446 bool isClosed, cv::Mat& destination) {
447 resolution = std::hypot(destination.size().height, destination.size().width) *
449 std::vector<wpi::math::Translation3d> pts{trls};
451 pts.emplace_back(pts[0]);
453 std::vector<std::vector<cv::Point2f>> polyPointList{};
455 for (
size_t i = 0; i < pts.size() - 1; i++) {
456 wpi::math::Translation3d pta = pts[i];
457 wpi::math::Translation3d ptb = pts[i + 1];
459 std::pair<std::optional<double>, std::optional<double>> inter =
465 double inter1 = inter.first.value();
466 double inter2 = inter.second.value();
467 wpi::math::Translation3d baseDelta = ptb - pta;
468 wpi::math::Translation3d old_pta = pta;
470 pta = old_pta + baseDelta * inter1;
473 ptb = old_pta + baseDelta * inter2;
475 baseDelta = ptb - pta;
479 cv::Point2d pxa = poly[0];
480 cv::Point2d pxb = poly[1];
482 double pxDist = std::hypot(pxb.x - pxa.x, pxb.y - pxa.y);
483 int subdivisions =
static_cast<int>(pxDist / resolution);
484 wpi::math::Translation3d subDelta = baseDelta / (subdivisions + 1);
485 std::vector<wpi::math::Translation3d> subPts{};
486 for (
int j = 0; j < subdivisions; j++) {
487 subPts.emplace_back(pta + (subDelta * (j + 1)));
489 if (subPts.size() > 0) {
492 poly.insert(poly.begin() + 1, toAdd.begin(), toAdd.end());
495 polyPointList.emplace_back(poly);
498 return polyPointList;
522 double resolution,
double wallThickness,
const cv::Scalar& wallColor,
523 int floorSubdivisions,
double floorThickness,
const cv::Scalar& floorColor,
524 cv::Mat& destination) {
528 for (
const auto& poly : polys) {
532 floorColor,
false, destination);
538 for (
const auto& poly : polys) {
542 wallColor,
false, destination);
Calibration and performance values for this camera.
Definition SimCameraProperties.h:56
Eigen::Matrix< double, 8, 1 > GetDistCoeffs() const
Returns the camera calibration's distortion coefficients, in OPENCV8 form.
Definition SimCameraProperties.h:157
wpi::units::hertz_t GetFPS() const
Gets the FPS of the simulated camera.
Definition SimCameraProperties.h:164
int GetResHeight() const
Gets the height of the simulated camera image.
Definition SimCameraProperties.h:136
int GetResWidth() const
Gets the width of the simulated camera image.
Definition SimCameraProperties.h:129
std::pair< std::optional< double >, std::optional< double > > GetVisibleLine(const RotTrlTransform3d &camRt, const wpi::math::Translation3d &a, const wpi::math::Translation3d &b) const
Determines where the line segment defined by the two given translations intersects the camera's frust...
Eigen::Matrix< double, 3, 3 > GetIntrinsics() const
Definition SimCameraProperties.h:149
Definition VideoSimUtil.h:43
int sgn(T val)
Definition VideoSimUtil.h:45
static cv::RotatedRect GetMinAreaRect(const std::vector< cv::Point2f > &points)
Definition OpenCVHelp.h:55
static std::vector< cv::Point2f > ProjectPoints(const Eigen::Matrix< double, 3, 3 > &cameraMatrix, const Eigen::Matrix< double, 8, 1 > &distCoeffs, const RotTrlTransform3d &camRt, const std::vector< wpi::math::Translation3d > &objectTranslations)
Definition OpenCVHelp.h:148
static std::vector< cv::Point2f > Get36h11MarkerPts()
Gets the points representing the marker(black square) corners.
Definition VideoSimUtil.h:121
static void DrawFieldWireFrame(const RotTrlTransform3d &camRt, const SimCameraProperties &prop, double resolution, double wallThickness, const cv::Scalar &wallColor, int floorSubdivisions, double floorThickness, const cv::Scalar &floorColor, cv::Mat &destination)
Draw a wireframe of the field to the given image.
Definition VideoSimUtil.h:520
static cv::Mat Get36h11TagImage(int id)
Definition VideoSimUtil.h:59
static void DrawTagDetection(int id, const std::vector< cv::Point2f > &dstPoints, cv::Mat &destination)
Draws a contour around the given points and text of the id onto the destination image.
Definition VideoSimUtil.h:328
static std::vector< std::vector< wpi::math::Translation3d > > GetFieldFloorLines(int subdivisions)
The translations used to draw the field floor subdivisions (not the floor outline).
Definition VideoSimUtil.h:409
static std::unordered_map< int, cv::Mat > LoadAprilTagImages()
Definition VideoSimUtil.h:68
static void Warp165h5TagImage(int tagId, const std::vector< cv::Point2f > &dstPoints, bool antialiasing, cv::Mat &destination)
Warps the image of a specific 36h11 AprilTag onto the destination image at the given points.
Definition VideoSimUtil.h:149
static std::vector< std::vector< wpi::math::Translation3d > > GetFieldWallLines()
The translations used to draw the field side walls and driver station walls.
Definition VideoSimUtil.h:346
static std::vector< cv::Point2f > GetImageCorners(const cv::Size &size)
Gets the points representing the corners of this image.
Definition VideoSimUtil.h:87
static double GetScaledThickness(double thickness480p, const cv::Mat &destination)
Given a line thickness in a 640x480 image, try to scale to the given destination image resolution.
Definition VideoSimUtil.h:279
static void DrawPoly(const std::vector< cv::Point2f > &dstPoints, int thickness, const cv::Scalar &color, bool isClosed, cv::Mat &destination)
Definition VideoSimUtil.h:304
static constexpr wpi::units::meter_t fieldLength
Definition VideoSimUtil.h:56
static const std::vector< cv::Point2f > kTag36h11MarkPts
Definition VideoSimUtil.h:127
static void DrawInscribedEllipse(const std::vector< cv::Point2f > &dstPoints, const cv::Scalar &color, cv::Mat &destination)
Draw a filled ellipse in the destination image.
Definition VideoSimUtil.h:297
static constexpr int kNumTags36h11
Definition VideoSimUtil.h:54
static std::vector< std::vector< cv::Point2f > > PolyFrom3dLines(const RotTrlTransform3d &camRt, const SimCameraProperties &prop, const std::vector< wpi::math::Translation3d > &trls, double resolution, bool isClosed, cv::Mat &destination)
Convert 3D lines represented by the given series of translations into a polygon(s) in the camera's im...
Definition VideoSimUtil.h:443
static void UpdateVideoProp(wpi::cs::CvSource &video, const SimCameraProperties &prop)
Updates the properties of this cs::CvSource video stream with the given camera properties.
Definition VideoSimUtil.h:131
static const std::unordered_map< int, cv::Mat > kTag36h11Images
Definition VideoSimUtil.h:125
static constexpr wpi::units::meter_t fieldWidth
Definition VideoSimUtil.h:57
Definition VisionEstimation.h:30