Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
vector<aruco::Marker> findMarkersMultiCrop(aruco::MarkerDetector & markerDetector, const cv::Mat & image, int cropIterations, float overlap) {
auto imageWidth = image.cols;
auto imageHeight = image.rows;
map<int, aruco::Marker> markersInAllCrops;
for (int cropIteration = 0; cropIteration < cropIterations; cropIteration++) {
auto stepRatio = 1.0f / pow(2, cropIteration);
#ifdef CROPDEBUG
cout << cropIteration << ", " << stepRatio << endl;
#endif
int stepWidth = imageWidth * stepRatio;
int stepHeight = imageHeight * stepRatio;
if (stepWidth == 0 || stepHeight == 0) {
#ifdef CROPDEBUG
cout << "Skipping crop tier" << endl;
#endif
continue;
}
int overlapWidth = stepWidth * overlap;
int overlapHeight = stepHeight * overlap;
for (int x = 0; x <= imageWidth - stepWidth; x += stepWidth) {
for (int y = 0; y <= imageHeight - stepHeight; y += stepHeight) {
//calc clamped window within image
int x_clamped = max(x - overlapWidth, 0);
int y_clamped = max(y - overlapHeight, 0);
int width_clamped = stepWidth + overlapWidth;
int height_clamped = stepHeight + overlapHeight;
width_clamped = min(width_clamped, imageWidth - x);
height_clamped = min(height_clamped, imageHeight - y);
//check we have an image
if (width_clamped == 0 || height_clamped == 0) {
#ifdef CROPDEBUG
cout << "Skipping crop section" << endl;
#endif
continue;
}
cv::Rect roi(x_clamped, y_clamped, width_clamped, height_clamped);
#ifdef CROPDEBUG
cout << roi << "[" << imageWidth << "x" << imageHeight << "]" << endl;
#endif
cv::Mat cropped = image(roi);
//perform detection
auto markersInCrop = markerDetector.detect(cropped);
#ifdef CROPDEBUG
//preview result
cv::imshow("cropped", drawMarkers(cropped, markersInCrop));
cv::waitKey(0);
#endif
//translate into image coords
for (auto & markerInCrop : markersInCrop) {
for (auto & point : markerInCrop) {
point.x += roi.x;
point.y += roi.y;
}
}
//save into markers
for (const auto & marker : markersInCrop) {
//this overwrites any previous finds
markersInAllCrops[marker.id] = marker;
}
}
}
}
//assemble vector of markers
vector<aruco::Marker> markerVector;
for (const auto & markerIt : markersInAllCrops) {
markerVector.push_back(markerIt.second);
}
return markerVector;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.