37 mDetectionInProgress =
false;
53 kpmDeleteHandle(&mKpmHandle);
56 ar2DeleteHandle(&mAr2Handle);
59 arParamLTFree(&mArCamParam);
64 boost::unique_lock<boost::recursive_mutex> lu(mUpdateMutex);
68 mMarkerList.insert(nftmarker);
75 boost::unique_lock<boost::recursive_mutex> lu(mUpdateMutex);
77 std::set<ArTkMarker*>::iterator imarker = mMarkerList.find(
static_cast<ArTkMarker*
> (marker));
78 if (imarker != mMarkerList.end())
79 mMarkerList.erase(imarker);
92 arParamLTFree(&mArCamParam);
96 int maxsize = std::max(camparam.
camParam.CamSize.width, camparam.
camParam.CamSize.height);
98 mScale = 640.0f / (float)maxsize;
119 cparam.xsize = (int)((
float)camparam.
camParam.CamSize.width * mScale);
120 cparam.ysize = (int)((
float)camparam.
camParam.CamSize.height * mScale);
122 cparam.mat[0][0] = camparam.
camParam.CameraMatrix.at<
float>(0, 0) * mScale;
123 cparam.mat[0][1] = camparam.
camParam.CameraMatrix.at<
float>(0, 1);
124 cparam.mat[0][2] = camparam.
camParam.CameraMatrix.at<
float>(0, 2) * mScale;
125 cparam.mat[0][3] = 0.0;
126 cparam.mat[1][0] = camparam.
camParam.CameraMatrix.at<
float>(1, 0);
127 cparam.mat[1][1] = camparam.
camParam.CameraMatrix.at<
float>(1, 1) * mScale;
128 cparam.mat[1][2] = camparam.
camParam.CameraMatrix.at<
float>(1, 2) * mScale;
129 cparam.mat[1][3] = 0.0;
130 cparam.mat[2][0] = camparam.
camParam.CameraMatrix.at<
float>(2, 0);
131 cparam.mat[2][1] = camparam.
camParam.CameraMatrix.at<
float>(2, 1);
132 cparam.mat[2][2] = camparam.
camParam.CameraMatrix.at<
float>(2, 2);
133 cparam.mat[2][3] = 0.0;
135 cparam.dist_function_version = 4;
136 cparam.dist_factor[0] = 0.0;
137 cparam.dist_factor[1] = 0.0;
138 cparam.dist_factor[2] = 0.0;
139 cparam.dist_factor[3] = 0.0;
140 cparam.dist_factor[4] = cparam.xsize;
141 cparam.dist_factor[5] = cparam.xsize;
142 cparam.dist_factor[6] = cparam.ysize;
143 cparam.dist_factor[7] = cparam.ysize;
144 cparam.dist_factor[8] = 1.0;
148 mArCamParam = arParamLTCreate(&cparam, AR_PARAM_LT_DEFAULT_OFFSET);
151void ArTkDetector::InitDetector()
153 boost::unique_lock<boost::recursive_mutex> ld(mUpdateMutex);
155 kpmDeleteHandle(&mKpmHandle);
158 ar2DeleteHandle(&mAr2Handle);
161 mKpmHandle = kpmCreateHandle(mArCamParam);
164 kpmSetDetectedFeatureMax(mKpmHandle, 250);
167 mAr2Handle = ar2CreateHandle(mArCamParam, AR_PIXEL_FORMAT_MONO, -1);
169 ar2SetTrackingThresh(mAr2Handle, 5.0);
170 ar2SetSimThresh(mAr2Handle, 0.5);
171 ar2SetSearchFeatureNum(mAr2Handle, 64);
172 ar2SetSearchSize(mAr2Handle, 8);
173 ar2SetTemplateSize1(mAr2Handle, 4);
174 ar2SetTemplateSize2(mAr2Handle, 6);
177 KpmRefDataSet *refDataSet = NULL;
180 std::set<ArTkMarker*>::iterator ifttmarker;
181 for (ifttmarker = mMarkerList.begin(); ifttmarker != mMarkerList.end(); ++ifttmarker)
183 if (!(*ifttmarker)->IsTrained())
187 KpmRefDataSet *refDataSet2 = (*ifttmarker)->GetDataSet();
188 if (refDataSet2 == NULL)
191 (*ifttmarker)->SetPageNum(pageCount);
192 kpmChangePageNoOfRefDataSet(refDataSet2, KpmChangePageNoAllPages, pageCount);
193 kpmMergeRefDataSet(&refDataSet, &refDataSet2);
197 if (refDataSet != NULL)
199 kpmSetRefDataSet(mKpmHandle, refDataSet);
200 kpmDeleteRefDataSet(&refDataSet);
206void ArTkDetector::NFTDetect()
208 KpmResult* kpmResult = NULL;
209 int kpmResultNum = 0;
212 boost::mutex::scoped_lock ld(mDetectMutex);
213 if (mDetectorFrame.empty() || mDetectorFrame.data == 0)
216 kpmSetMatchingSkipPage(mKpmHandle, mSkippage, mNumSkip);
217 kpmMatching(mKpmHandle, mDetectorFrame.data);
218 kpmGetResult(mKpmHandle, &kpmResult, &kpmResultNum);
221 boost::unique_lock<boost::recursive_mutex> ld(mUpdateMutex);
222 std::set<ArTkMarker*>::iterator imarker;
224 for (
int i = 0; i < kpmResultNum; i++)
226 if (kpmResult[i].camPoseF != 0)
230 for (imarker = mMarkerList.begin(); imarker != mMarkerList.end(); ++imarker)
232 if (!(*imarker)->IsTracked() && (*imarker)->IsTrained() && ((*imarker)->GetPageNum() == kpmResult[i].pageNo))
233 (*imarker)->StartTracking(mDetectorColorFrame, kpmResult[i].camPose, mCamparam, mReverse);
236 mDetectionInProgress =
false;
242 boost::thread detectorThread;
251 bool needDetect =
false;
258 if ((mArCamParam == 0) || (mArCamParam && (((
int)((
float)mLastData.
arCamParam.
camParam.CamSize.width * mScale)) != mArCamParam->param.xsize || ((
int)((
float)mLastData.
arCamParam.
camParam.CamSize.height * mScale)) != mArCamParam->param.ysize)))
262 if (mIsDirty && !mMarkerList.empty())
264 boost::mutex::scoped_lock ld(mDetectMutex);
266 if (detectorThread.joinable())
267 detectorThread.join();
273 nsize.width = (int)((
float)mLastData.
gray.cols * mScale);
274 nsize.height = (int)((
float)mLastData.
gray.rows * mScale);
277 cv::resize(mLastData.
gray, mLastData.
gray, nsize, 0, 0, cv::INTER_LINEAR);
279 cv::equalizeHist(mLastData.
gray, mLastData.
gray);
281 boost::unique_lock<boost::recursive_mutex> lu(mUpdateMutex);
282 std::set<ArTkMarker*>::iterator imarker;
284 for (imarker = mMarkerList.begin(); imarker != mMarkerList.end(); ++imarker)
286 if (!(*imarker)->IsTracked())
291 for (imarker = mMarkerList.begin(); imarker != mMarkerList.end(); ++imarker)
293 if ((*imarker)->IsTracked())
296 (*imarker)->RegisterCurrentFrame(mLastData.
gray, nsize, mScale);
300 if (needDetect && !mDetectionInProgress && !mLastData.
image.empty())
302 boost::mutex::scoped_lock ld(mDetectMutex);
304 if (detectorThread.joinable())
305 detectorThread.join();
307 mDetectionInProgress =
true;
308 mLastData.
gray.copyTo(mDetectorFrame);
313 for (imarker = mMarkerList.begin(); imarker != mMarkerList.end(); ++imarker)
315 if ((*imarker)->IsTracked())
317 std::vector<cv::Point> poly;
318 for (i = 0; i < (*imarker)->size(); i++)
320 cv::Point pt1(
static_cast<int>((*imarker)->at(i).x * mScale),
static_cast<int>((*imarker)->at(i).y * mScale));
323 cv::fillConvexPoly(mDetectorFrame, poly, cv::Scalar(0));
324 mSkippage[mNumSkip] = (*imarker)->GetPageNum();
332 mLastData.
image.copyTo(mDetectorColorFrame);
333 detectorThread = boost::thread(&ArTkDetector::NFTDetect,
this);
341 boost::this_thread::sleep_for(boost::chrono::milliseconds(1));
345 if (detectorThread.joinable())
346 detectorThread.join();
aruco::CameraParameters camParam
void GetLastData(LASTDATA &data)
static ArManager * GetInstance()
void RemoveMarker(ArMarker *marker)
void AddMarker(ArMarker *marker)
This class represents a marker. It is a vector of the fours corners ot the marker.
void StartThreading(std::function< void()> threadFunction)