21#include <opencv2/objdetect.hpp>
22#include <opencv2/imgproc.hpp>
32 camparams::freeze(
true);
54 camparams::freeze(
false);
60 std::string markerConfigDataFilename;
62 switch (artoolkit::dictionary::get())
64 case artoolkit::Dict::AR_MATRIX_CODE_3x3: markerConfigDataFilename =
"markers0.dat";
break;
65 case artoolkit::Dict::AR_MATRIX_CODE_3x3_HAMMING63: markerConfigDataFilename =
"markers1.dat";
break;
66 case artoolkit::Dict::AR_MATRIX_CODE_3x3_PARITY65: markerConfigDataFilename =
"markers2.dat";
break;
67 default: markerConfigDataFilename =
"markers2.dat";
72 arParamChangeSize(&cparam, w,
h, &cparam);
74 std::string
const CPARA_NAME =
75 absolutePath(camparams::get() + std::to_string(w) +
'x' + std::to_string(
h) +
".dat");
77 if (arParamLoad(
absolutePath(CPARA_NAME).c_str(), 1, &cparam) < 0)
78 LERROR(
"Failed to load camera parameters " << CPARA_NAME <<
" -- IGNORED");
80 if ((
gCparamLT = arParamLTCreate(&cparam, AR_PARAM_LT_DEFAULT_OFFSET)) ==
nullptr)
LFATAL(
"Error in arParamLTCreate");
84 if ((
ar3DHandle = ar3DCreateHandle(&cparam)) ==
nullptr)
LFATAL(
"Error in ar3DCreateHandle");
86 if (arSetPixelFormat(
arHandle, pixformat) < 0)
LFATAL(
"Error in arSetPixelFormat");
88 if ((
arPattHandle = arPattCreateHandle()) ==
nullptr)
LFATAL(
"Error in arPattCreateHandle");
95 arSetPatternDetectionMode(
arHandle, AR_MATRIX_CODE_DETECTION);
97 switch (dictionary::get())
99 case artoolkit::Dict::AR_MATRIX_CODE_3x3:
100 arSetMatrixCodeType(
arHandle, AR_MATRIX_CODE_3x3);
break;
101 case artoolkit::Dict::AR_MATRIX_CODE_3x3_HAMMING63:
102 arSetMatrixCodeType(
arHandle, AR_MATRIX_CODE_3x3_HAMMING63);
break;
103 case artoolkit::Dict::AR_MATRIX_CODE_3x3_PARITY65:
104 arSetMatrixCodeType(
arHandle, AR_MATRIX_CODE_3x3_PARITY65);
break;
105 default: arSetMatrixCodeType(
arHandle, AR_MATRIX_CODE_3x3_PARITY65);
111 AR_LABELING_THRESH_MODE modea;
112 switch (artoolkit::threshmode::get())
114 case artoolkit::DictThreshMode::AR_LABELING_THRESH_MODE_MANUAL:
115 modea = AR_LABELING_THRESH_MODE_MANUAL;
break;
116 case artoolkit::DictThreshMode::AR_LABELING_THRESH_MODE_AUTO_MEDIAN:
117 modea = AR_LABELING_THRESH_MODE_AUTO_MEDIAN;
break;
118 case artoolkit::DictThreshMode::AR_LABELING_THRESH_MODE_AUTO_OTSU:
119 modea = AR_LABELING_THRESH_MODE_AUTO_OTSU;
break;
120 case artoolkit::DictThreshMode::AR_LABELING_THRESH_MODE_AUTO_ADAPTIVE:
121 modea = AR_LABELING_THRESH_MODE_AUTO_ADAPTIVE;
break;
122 case artoolkit::DictThreshMode::AR_LABELING_THRESH_MODE_AUTO_BRACKETING:
123 modea = AR_LABELING_THRESH_MODE_AUTO_BRACKETING;
break;
124 default: modea = AR_LABELING_THRESH_MODE_AUTO_OTSU;
126 arSetLabelingThreshMode(
arHandle, modea);
137 case V4L2_PIX_FMT_YUYV:
manualinit(image.width, image.height, AR_PIXEL_FORMAT_yuvs);
break;
138 case V4L2_PIX_FMT_GREY:
manualinit(image.width, image.height, AR_PIXEL_FORMAT_MONO);
break;
139 case V4L2_PIX_FMT_RGB565:
manualinit(image.width, image.height, AR_PIXEL_FORMAT_RGB_565);
break;
140 case V4L2_PIX_FMT_BGR24:
manualinit(image.width, image.height, AR_PIXEL_FORMAT_BGR);
break;
141 default:
LFATAL(
"Unsupported image format, should be V4L2_PIX_FMT_YUYV, V4L2_PIX_FMT_GREY, "
142 "V4L2_PIX_FMT_RGB565, or V4L2_PIX_FMT_BGR24");
156 switch (image.type())
158 case CV_8UC3:
manualinit(image.cols, image.rows, AR_PIXEL_FORMAT_BGR);
break;
159 case CV_8UC1:
manualinit(image.cols, image.rows, AR_PIXEL_FORMAT_MONO);
break;
160 default:
LFATAL(
"Unsupported image format, should be CV_8UC3 for BGR or CV_8UC1 for gray");
173 if (arDetectMarker(
arHandle,
const_cast<unsigned char *
>(data)) < 0)
174 {
LERROR(
"Error trying to detect markers -- IGNORED");
return; }
176 double const confidence_thresh = artoolkit::confthresh::get();
177 int const numDetected = arGetMarkerNum(
arHandle);
178 int const useContPoseEstimation = artoolkit::contpose::get();
181 ARMarkerInfo * markerInfo = arGetMarker(
arHandle);
189 for (
int j = 0; j < numDetected; ++j)
195 if (markerInfo[j].cf >= confidence_thresh) k = j;
197 else if (markerInfo[j].cf > markerInfo[k].cf) k = j;
206 result.id = markerInfo[k].id;
209 result.p2d[0] = markerInfo[k].pos[0]; result.p2d[1] = markerInfo[k].pos[1];
219 if (err > 1.0)
continue;
221 arUtilMat2QuatPos(
markersSquare[i].trans, result.q, result.pos);
223 for (
int i1 = 0; i1 < 4; ++i1)
225 auto const & v1 = markerInfo[k].vertex[i1];
226 result.corners.push_back(cv::Point(
int(v1[0] + 0.5F),
int(v1[1] + 0.5F)));
243 for (
int i = 0; i < 4; ++i)
245 auto const & v1 = r.corners[i];
246 auto const & v2 = r.corners[(i + 1) % 4];
254 if (txtx >= 0 && txty >= 0)
263 static ImU32
const col = ImColor(255, 128, 128, 255);
267 helper.
drawCircle(r.p2d[0], r.p2d[1], 3.0F, col,
true);
269 std::vector<cv::Point2f> p;
270 for (
int i = 0; i < 4; ++i) p.emplace_back(cv::Point2f(r.corners[i].x, r.corners[i].y));
273 helper.
drawText(r.p2d[0]+5, r.p2d[1]+5, (
"AR=" + std::to_string(r.id)).c_str(), col);
276 helper.
itext(
"Detected " + std::to_string(
itsResults.size()) +
" ARtoolkit markers.");
286 r.width, r.height, 1.0F,
287 r.q[0], r.q[1], r.q[2], r.q[3],
288 "A" + std::to_string(r.id));
#define AR_PATTERN_TYPE_MATRIX
void deleteMarkers(ARMarkerSquare **markersSquare_p, int *markersSquareCount_p, ARPattHandle *arPattHandle)
void newMarkers(const char *markersConfigDataFilePathC, ARPattHandle *arPattHandle, ARMarkerSquare **markersSquare_out, int *markersSquareCount_out, int *patternDetectionMode_out)
std::filesystem::path absolutePath(std::filesystem::path const &path="")
void drawCircle(float x, float y, float r, ImU32 col=IM_COL32(128, 255, 128, 255), bool filled=true)
void drawText(float x, float y, char const *txt, ImU32 col=IM_COL32(128, 255, 128, 255))
void itext(char const *txt, ImU32 const &col=IM_COL32_BLACK_TRANS, int line=-1)
void drawPoly(std::vector< cv::Point > const &pts, ImU32 col=IM_COL32(128, 255, 128, 255), bool filled=true)
void sendSerialStd3D(float x, float y, float z, float w=0.0F, float h=0.0F, float d=0.0F, float q1=0.0F, float q2=0.0F, float q3=0.0f, float q4=0.0F, std::string const &id="", std::string const &extra="")
void sendSerialContour2D(unsigned int camw, unsigned int camh, std::vector< cv::Point_< T > > points, std::string const &id="", std::string const &extra="")
void writeText(RawImage &img, std::string const &txt, int x, int y, unsigned int col, Font font=Font6x10)
void drawLine(RawImage &img, int x1, int y1, int x2, int y2, unsigned int thick, unsigned int col)
void drawCircle(RawImage &img, int x, int y, unsigned int rad, unsigned int thick, unsigned int col)
unsigned short constexpr LightPink
unsigned short constexpr White