31 #include <opencv2/imgproc/imgproc.hpp>
45 if (dt !=
"ALL_DICTS" && dt !=
"CUSTOM")
52 for (
auto& dic :
vdic)
65 out.create(nbits_sq, nbits_sq, CV_8UC1);
66 std::bitset<64> bs(code);
68 for (
int r = 0; r < nbits_sq; r++)
70 uchar* pr = out.ptr<uchar>(r);
71 for (
int c = 0; c < nbits_sq; c++)
78 return static_cast<int>(std::bitset<64>(a ^ b).count());
82 std::string& additionalInfo)
84 assert(in.rows == in.cols);
87 if (in.type() == CV_8UC1)
90 cv::cvtColor(in, grey, CV_BGR2GRAY);
92 cv::threshold(grey, grey, 125, 255, cv::THRESH_BINARY | cv::THRESH_OTSU);
95 std::map<uint32_t, std::vector<uint64_t> > nbits_ids;
99 int nbits = bitsids.first;
100 std::vector<uint64_t> ids;
106 nbits_ids[nbits] = ids;
112 if (nbits_ids.size() == 0)
115 for (
auto nbits : nbits_ids)
117 const auto& ids = nbits.second;
122 for (
int rot = 0; rot < 4; rot++)
123 if (dic->is(ids[rot]))
127 marker_id = dic->at(ids[rot]);
128 additionalInfo = dic->getName();
135 int _maxCorrectionAllowed =
138 int min_dist = 100000000;
140 for (
auto ci : dic->getMapCode())
142 for (
int i = 0; i < 4; i++)
145 if (curr_dist < _maxCorrectionAllowed && curr_dist < min_dist)
148 min_dist = curr_dist;
149 marker_id = ci.second;
151 additionalInfo = dic->getName();
166 std::vector<uint64_t>& ids)
168 int bits_noborder =
static_cast<int>(std::sqrt(total_nbits));
169 int bits_withborder = bits_noborder + 2;
172 cv::Mat nonZeros(bits_withborder, bits_withborder, CV_32SC1);
173 cv::Mat nValues(bits_withborder, bits_withborder, CV_32SC1);
174 nonZeros.setTo(cv::Scalar::all(0));
175 nValues.setTo(cv::Scalar::all(0));
176 for (
int y = 0; y < thres_img.rows; y++)
178 const uchar* ptr = thres_img.ptr<uchar>(y);
179 int my = float(bits_withborder) * float(y) / float(thres_img.rows);
180 for (
int x = 0; x < thres_img.cols; x++)
182 int mx = float(bits_withborder) * float(x) / float(thres_img.cols);
184 nonZeros.at<
int>(my, mx)++;
185 nValues.at<
int>(my, mx)++;
188 cv::Mat binaryCode(bits_withborder, bits_withborder, CV_8UC1);
190 for (
int y = 0; y < bits_withborder; y++)
191 for (
int x = 0; x < bits_withborder; x++)
193 if (nonZeros.at<
int>(y, x) > nValues.at<
int>(y, x) / 2)
194 binaryCode.at<uchar>(y, x) = 1;
196 binaryCode.at<uchar>(y, x) = 0;
200 for (
int y = 0; y < bits_withborder; y++)
202 int inc = bits_withborder - 1;
203 if (y == 0 || y == bits_withborder - 1)
205 for (
int x = 0; x < bits_withborder; x += inc)
206 if (binaryCode.at<uchar>(y, x) != 0)
212 cv::Mat _bits(bits_noborder, bits_noborder, CV_8UC1);
213 for (
int y = 0; y < bits_noborder; y++)
214 for (
int x = 0; x < bits_noborder; x++)
215 _bits.at<uchar>(y, x) = binaryCode.at<uchar>(y + 1, x + 1);
289 std::bitset<64> bits;
291 for (
int y = code.rows - 1; y >= 0; y--)
292 for (
int x = code.cols - 1; x >= 0; x--)
293 bits[bidx++] = code.at<uchar>(y, x);
294 return bits.to_ullong();
300 for (
int i = 0; i < in.rows; i++)
302 for (
int j = 0; j < in.cols; j++)
304 out.at<uchar>(i, j) = in.at<uchar>(in.cols - j - 1, i);