Skip to content

Commit 2b54fa9

Browse files
committed
Move initial track selection (via _resetTracks) inside TrackSelection.
Also fixes: - SIFT only freely available in OpenCV 4.4.0+. - Correct return type in videoImage.jpgread.
1 parent 9689049 commit 2b54fa9

File tree

8 files changed

+43
-46
lines changed

8 files changed

+43
-46
lines changed

Source/ARX/ARVideo/Image/videoImage.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ static int jpegRead(FILE *fp, unsigned char *buf, int bufWidth, int bufHeight, A
133133
cinfo.image_height < 0 || cinfo.image_height > 32767) {
134134
ARLOGe("JPEG image size too large.");
135135
jpeg_destroy_decompress(&cinfo);
136-
return NULL;
136+
return (FALSE);
137137
}
138138

139139
if (pixFormat != bufPixFormat) { // Pixel format conversion required?

Source/ARX/OCVT/OCVConfig.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@
3838

3939
int minRequiredDetectedFeatures = 50; ///< Minimum number of detected features required to consider a target matched.
4040
const int markerTemplateWidth = 15; ///< Width in pixels of image patches used in template matching.
41-
const int k_OCVTOpticalFlowMaxPyrLevel = 3; ///< Maximum number of levels in optical flow image pyramid (0 = base level only).
4241
const cv::Size subPixWinSize(10,10);
4342
const cv::Size winSize(31,31); ///< Window size to use in optical flow search.
4443
cv::TermCriteria termcrit(cv::TermCriteria::COUNT|cv::TermCriteria::EPS,20,0.03);

Source/ARX/OCVT/OCVConfig.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,13 @@
6161
#include <opencv2/calib3d.hpp>
6262
#include <ARX/OCVT/PlanarTracker.h>
6363

64+
/** @file */
65+
66+
/// @def k_OCVTOpticalFlowMaxPyrLevel Maximum number of levels in optical flow image pyramid (0 = base level only).
67+
#define k_OCVTOpticalFlowMaxPyrLevel 3
68+
6469
OCV_EXTERN extern int minRequiredDetectedFeatures; ///< Minimum number of detected features required to consider a target matched.
6570
OCV_EXTERN extern const int markerTemplateWidth; ///< Width in pixels of image patches used in template matching.
66-
OCV_EXTERN extern const int k_OCVTOpticalFlowMaxPyrLevel; ///< Maximum number of levels in optical flow image pyramid (0 = base level only).
6771
OCV_EXTERN extern const cv::Size subPixWinSize;
6872
OCV_EXTERN extern const cv::Size winSize; ///< Window size to use in optical flow search.
6973
OCV_EXTERN extern cv::TermCriteria termcrit;

Source/ARX/OCVT/OCVFeatureDetector.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,12 @@ void OCVFeatureDetector::CreateORBFeatureDetector()
8989

9090
void OCVFeatureDetector::CreateSIFTFeatureDetector()
9191
{
92+
#if CV_VERSION_MAJOR > 4 || (CV_VERSION_MAJOR == 4 && CV_VERSION_MINOR >= 4)
9293
_featureDetector = cv::SIFT::create();
94+
#else
95+
// For versions without SIFT, BRISK is best alternative.
96+
_featureDetector = cv::BRISK::create();
97+
#endif
9398
_matcher = cv::BFMatcher::create();
9499
}
95100

Source/ARX/OCVT/PlanarTracker.cpp

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -209,8 +209,8 @@ class PlanarTracker::PlanarTrackerImpl
209209
//std::cout << "New marker detected" << std::endl;
210210
_trackables[bestMatchIndex]._isDetected = true;
211211
// Since we've just detected the marker, make sure next invocation of
212-
// SelectTrackablePoints() for this marker resets the selection.
213-
_trackables[bestMatchIndex]._resetTracks = true;
212+
// GetInitialFeatures() for this marker makes a new selection.
213+
_trackables[bestMatchIndex]._trackSelection.ResetSelection();
214214
_trackables[bestMatchIndex]._trackSelection.SetHomography(homoInfo.homography);
215215

216216
// Use the homography to form the initial estimate of the bounding box.
@@ -227,17 +227,6 @@ class PlanarTracker::PlanarTrackerImpl
227227
}
228228
}
229229

230-
std::vector<cv::Point2f> SelectTrackablePoints(int trackableIndex)
231-
{
232-
if (_trackables[trackableIndex]._resetTracks) {
233-
_trackables[trackableIndex]._trackSelection.SelectPoints();
234-
_trackables[trackableIndex]._resetTracks = false;
235-
return _trackables[trackableIndex]._trackSelection.GetSelectedFeatures();
236-
} else {
237-
return _trackables[trackableIndex]._trackSelection.GetTrackedFeatures();
238-
}
239-
}
240-
241230
bool RunOpticalFlow(int trackableId, const std::vector<cv::Point2f>& trackablePoints, const std::vector<cv::Point2f>& trackablePointsWarped)
242231
{
243232
std::vector<cv::Point2f> flowResultPoints, trackablePointsWarpedResult;
@@ -292,7 +281,7 @@ class PlanarTracker::PlanarTrackerImpl
292281
}
293282
}
294283
if (_frameCount > 1) {
295-
_trackables[trackableId]._resetTracks = true;
284+
_trackables[trackableId]._trackSelection.ResetSelection();
296285
}
297286
return true;
298287
}
@@ -537,7 +526,7 @@ class PlanarTracker::PlanarTrackerImpl
537526
//float templateScaleFactor = (float)(1 << templatePyrLevel);
538527
//std::cout << "templatePyrLevel=" << templatePyrLevel << ", templateScaleFactor=" << templateScaleFactor << "." << std::endl;
539528

540-
std::vector<cv::Point2f> trackablePoints = SelectTrackablePoints(i);
529+
std::vector<cv::Point2f> trackablePoints = _trackables[i]._trackSelection.GetInitialFeatures();
541530
std::vector<cv::Point2f> trackablePointsWarped = _trackables[i]._trackSelection.GetTrackedFeaturesWarped();
542531

543532
if (_frameCount > 0 && _prevPyramid.size() > 0) {
@@ -648,7 +637,6 @@ class PlanarTracker::PlanarTrackerImpl
648637
newTrackable._bBox.push_back(cv::Point2f(0, newTrackable._height));
649638
newTrackable._isTracking = false;
650639
newTrackable._isDetected = false;
651-
newTrackable._resetTracks = false;
652640
newTrackable._trackSelection = TrackingPointSelector(newTrackable._cornerPoints, newTrackable._width, newTrackable._height, markerTemplateWidth);
653641
_trackables.push_back(newTrackable);
654642
}
@@ -686,7 +674,6 @@ class PlanarTracker::PlanarTrackerImpl
686674
newTrackable._bBox.push_back(cv::Point2f(0, newTrackable._height));
687675
newTrackable._isTracking = false;
688676
newTrackable._isDetected = false;
689-
newTrackable._resetTracks = false;
690677
newTrackable._trackSelection = TrackingPointSelector(newTrackable._cornerPoints, newTrackable._width, newTrackable._height, markerTemplateWidth);
691678

692679
_trackables.push_back(newTrackable);

Source/ARX/OCVT/TrackableInfo.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,6 @@ class TrackableInfo
6060
/// The four points defining the bound-box of a detected trackable in the video frame.
6161
std::vector<cv::Point2f> _bBoxTransformed;
6262
bool _isTracking, _isDetected;
63-
/// If set to true, TrackSelection.SelectPoints will be invoked in SelectTrackablePoints, selecting new points and resetting their tracking status.
64-
bool _resetTracks;
6563

6664
std::vector<cv::Point2f> _cornerPoints;
6765
TrackingPointSelector _trackSelection;

Source/ARX/OCVT/TrackingPointSelector.cpp

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,12 @@
4040

4141
TrackingPointSelector::TrackingPointSelector()
4242
{
43-
4443
}
4544

46-
TrackingPointSelector::TrackingPointSelector(std::vector<cv::Point2f> pts, int width, int height, int markerTemplateWidth)
45+
TrackingPointSelector::TrackingPointSelector(std::vector<cv::Point2f> pts, int width, int height, int markerTemplateWidth) :
46+
_reset(false),
47+
_pts(pts)
4748
{
48-
_pts = pts;
4949
DistributeBins(width, height, markerTemplateWidth);
5050
}
5151

@@ -107,12 +107,16 @@ void TrackingPointSelector::UpdatePointStatus(std::vector<uchar> status)
107107
}
108108
}
109109

110-
/**
111-
@brief Selects a random template from each bin for tracking.
112-
Should be called when either a marker has just been detected.
113-
*/
114-
void TrackingPointSelector::SelectPoints()
110+
void TrackingPointSelector::ResetSelection()
111+
{
112+
_reset = true;
113+
}
114+
115+
std::vector<cv::Point2f> TrackingPointSelector::GetInitialFeatures()
115116
{
117+
if (!_reset) return GetTrackedFeatures();
118+
_reset = false;
119+
116120
// Reset state of all points to not selected and not tracking.
117121
_selectedPts.clear();
118122
for (auto &bin : trackingPointBin) {
@@ -122,6 +126,8 @@ void TrackingPointSelector::SelectPoints()
122126
}
123127
}
124128

129+
// Selects a random template from each bin for tracking.
130+
std::vector<cv::Point2f> ret;
125131
for (auto &bin : trackingPointBin) {
126132
size_t pointCount = bin.second.size();
127133
if (pointCount > 0) { // If there are points in the bin.
@@ -130,19 +136,11 @@ void TrackingPointSelector::SelectPoints()
130136
bin.second[tIndex].SetSelected(true);
131137
bin.second[tIndex].SetTracking(true);
132138
_selectedPts.push_back(bin.second[tIndex]);
139+
140+
ret.push_back(bin.second[tIndex].pt);
133141
}
134142
}
135-
}
136-
137-
std::vector<cv::Point2f> TrackingPointSelector::GetSelectedFeatures()
138-
{
139-
std::vector<cv::Point2f> selectedPoints;
140-
for (std::vector<TrackedPoint>::iterator it = _selectedPts.begin(); it != _selectedPts.end(); ++it) {
141-
if (it->IsSelected()) {
142-
selectedPoints.push_back(it->pt);
143-
}
144-
}
145-
return selectedPoints;
143+
return ret;
146144
}
147145

148146
std::vector<cv::Point2f> TrackingPointSelector::GetTrackedFeatures()

Source/ARX/OCVT/TrackingPointSelector.h

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -61,10 +61,16 @@ class TrackingPointSelector
6161

6262
void UpdatePointStatus(std::vector<uchar> status);
6363

64-
/// Reset selected points and then randomly select one point from each bin.
65-
void SelectPoints();
64+
/**
65+
@brief Signal that the next call to GetInitialFeatures should return a new selection.
66+
*/
67+
void ResetSelection();
6668

67-
std::vector<cv::Point2f> GetSelectedFeatures();
69+
/**
70+
@brief If reset, then selects an initial random template from each bin for tracking,
71+
and returns this set. If not reset then returns the same set as GetTrackedFeatures.
72+
*/
73+
std::vector<cv::Point2f> GetInitialFeatures();
6874

6975
std::vector<cv::Point2f> GetTrackedFeatures();
7076

@@ -78,10 +84,10 @@ class TrackingPointSelector
7884
OCV_EXTERN void CleanUp();
7985

8086
private:
81-
std::vector<TrackedPoint> _selectedPts;
87+
bool _reset;
8288
std::vector<cv::Point2f> _pts;
8389
std::map<int, std::vector<TrackedPoint> > trackingPointBin;
8490
cv::Mat _homography;
85-
91+
std::vector<TrackedPoint> _selectedPts;
8692
};
8793
#endif //TRACKINGPOINTSELECTOR

0 commit comments

Comments
 (0)