26#include <opencv2/imgproc/imgproc.hpp>
34 "blob1, etc for parameters and serial messages",
151 public jevois::Parameter<numtrack>
160 itsArUco = addSubComponent<ArUco>(
"aruco");
169 numtrack::freeze(
true);
171 for (
size_t i = 0; i < numtrack::get(); ++i)
172 itsBlobs.push_back(addSubComponent<BlobDetector>(
"blob" + std::to_string(i)));
200 auto c = b->detect(itsImgHsv);
201 std::lock_guard<std::mutex> _(itsBlobMtx);
202 itsContours[b->instanceName()] = std::move(c);
212 try { f.get(); }
catch (...) {
LERROR(
"Ooops, some blob detector threw -- IGNORED"); }
217 for (
auto const & c : cc.second)
224 void detectArUco(cv::Mat cvimg, std::vector<int> & ids, std::vector<std::vector<cv::Point2f>> & corners,
225 std::vector<cv::Vec3d> & rvecs, std::vector<cv::Vec3d> & tvecs,
228 itsArUco->detectMarkers(cvimg, ids, corners);
230 if (
itsArUco->dopose::get() && ids.empty() ==
false)
231 itsArUco->estimatePoseSingleMarkers(corners, rvecs, tvecs);
234 if (outimg)
itsArUco->drawDetections(*outimg, 3,
h+2, ids, corners, rvecs, tvecs);
247 cv::cvtColor(imgbgr,
itsImgHsv, cv::COLOR_BGR2HSV);
259 std::vector<int> ids; std::vector<std::vector<cv::Point2f> > corners; std::vector<cv::Vec3d> rvecs, tvecs;
263 itsArUco->sendSerial(
this, ids, corners, w,
h, rvecs, tvecs);
278 inimg.
require(
"input", w,
h, V4L2_PIX_FMT_YUYV);
285 outimg = outframe.get();
294 cv::cvtColor(imgbgr,
itsImgHsv, cv::COLOR_BGR2HSV);
309 std::vector<int> ids; std::vector<std::vector<cv::Point2f> > corners; std::vector<cv::Vec3d> rvecs, tvecs;
310 detectArUco(cvimg, ids, corners, rvecs, tvecs,
h, &outimg);
313 itsArUco->sendSerial(
this, ids, corners, w,
h, rvecs, tvecs);
324 int color = (cc.first.back() -
'0') * 123;
325 cv::drawContours(outuc2, cc.second, -1, color, 2, 8);
326 for (
auto const & cont : cc.second)
328 cv::Moments moment = cv::moments(cont);
329 double const area = moment.m00;
330 int const x = int(moment.m10 /
area + 0.4999);
331 int const y = int(moment.m01 /
area + 0.4999);
338 std::string str =
"Detected ";
339 for (
auto const & cc :
itsContours) str += std::to_string(cc.second.size()) +
' ';
343 std::string
const & fpscpu = timer.
stop();
356 std::vector<std::shared_ptr<BlobDetector> >
itsBlobs;
358 std::map<std::string, std::vector<std::vector<cv::Point>>>
itsContours;
JEVOIS_REGISTER_MODULE(ArUcoBlob)
double area(const std::vector< Point2D< T > > &polygon, const bool getsigned=false)
What is the area of a polygon?
Combined ArUco marker + multiple color-based object detection.
virtual void process(jevois::InputFrame &&inframe) override
Processing function, no USB video output.
std::vector< std::future< void > > itsBlobFuts
virtual ~ArUcoBlob()
Virtual destructor for safe inheritance.
void postInit() override
Post-init: instantiate the blob detectors.
std::map< std::string, std::vector< std::vector< cv::Point > > > itsContours
void sendBlobs(unsigned int w, unsigned int h)
Gather our blob threads and send/draw the results.
ArUcoBlob(std::string const &instance)
Constructor.
std::shared_ptr< ArUco > itsArUco
void detectBlobs()
Detect blobs in parallel threads.
JEVOIS_DECLARE_PARAMETER(numtrack, size_t, "Number of parallel blob trackers to run. They will be named blob0, " "blob1, etc for parameters and serial messages", 3, ParamCateg)
Parameter.
std::vector< std::shared_ptr< BlobDetector > > itsBlobs
void preUninit() override
void detectArUco(cv::Mat cvimg, std::vector< int > &ids, std::vector< std::vector< cv::Point2f > > &corners, std::vector< cv::Vec3d > &rvecs, std::vector< cv::Vec3d > &tvecs, unsigned int h, jevois::RawImage *outimg=nullptr)
Detect ArUcos.
virtual void process(jevois::InputFrame &&inframe, jevois::OutputFrame &&outframe) override
Processing function, with USB video output.
void removeSubComponent(std::shared_ptr< Comp > &component)
void require(char const *info, unsigned int w, unsigned int h, unsigned int f) const
StdModule(std::string const &instance)
void sendSerialContour2D(unsigned int camw, unsigned int camh, std::vector< cv::Point_< T > > points, std::string const &id="", std::string const &extra="")
std::string const & stop(double *seconds)
void paste(RawImage const &src, RawImage &dest, int dx, int dy)
cv::Mat cvImage(RawImage const &src)
void writeText(RawImage &img, std::string const &txt, int x, int y, unsigned int col, Font font=Font6x10)
cv::Mat convertToCvGray(RawImage const &src)
void drawFilledRect(RawImage &img, int x, int y, unsigned int w, unsigned int h, unsigned int col)
cv::Mat convertToCvBGR(RawImage const &src)
void drawCircle(RawImage &img, int x, int y, unsigned int rad, unsigned int thick, unsigned int col)
std::future< std::invoke_result_t< std::decay_t< Function >, std::decay_t< Args >... > > async(Function &&f, Args &&... args)
unsigned short constexpr White