Merge pull request #25045 from AleksandrPanov:aruco_fix_unnecessary_copying

fix unnecessary copying in ArucoDetector
This commit is contained in:
Alexander Smorkalov 2024-02-19 09:53:12 +03:00 committed by GitHub
commit 318b7ec3e6
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 12 additions and 15 deletions

View File

@ -145,10 +145,8 @@ static void _findMarkerContours(const Mat &in, vector<vector<Point2f> > &candida
minPerimeterPixels = 4*minSize;
}
Mat contoursImg;
in.copyTo(contoursImg);
vector<vector<Point> > contours;
findContours(contoursImg, contours, RETR_LIST, CHAIN_APPROX_NONE);
findContours(in, contours, RETR_LIST, CHAIN_APPROX_NONE);
// now filter list of contours
for(unsigned int i = 0; i < contours.size(); i++) {
// check perimeter
@ -161,8 +159,7 @@ static void _findMarkerContours(const Mat &in, vector<vector<Point2f> > &candida
if(approxCurve.size() != 4 || !isContourConvex(approxCurve)) continue;
// check min distance between corners
double minDistSq =
max(contoursImg.cols, contoursImg.rows) * max(contoursImg.cols, contoursImg.rows);
double minDistSq = max(in.cols, in.rows) * max(in.cols, in.rows);
for(int j = 0; j < 4; j++) {
double d = (double)(approxCurve[j].x - approxCurve[(j + 1) % 4].x) *
(double)(approxCurve[j].x - approxCurve[(j + 1) % 4].x) +
@ -177,9 +174,9 @@ static void _findMarkerContours(const Mat &in, vector<vector<Point2f> > &candida
bool tooNearBorder = false;
for(int j = 0; j < 4; j++) {
if(approxCurve[j].x < minDistanceToBorder || approxCurve[j].y < minDistanceToBorder ||
approxCurve[j].x > contoursImg.cols - 1 - minDistanceToBorder ||
approxCurve[j].y > contoursImg.rows - 1 - minDistanceToBorder)
tooNearBorder = true;
approxCurve[j].x > in.cols - 1 - minDistanceToBorder ||
approxCurve[j].y > in.rows - 1 - minDistanceToBorder)
tooNearBorder = true;
}
if(tooNearBorder) continue;
@ -883,7 +880,7 @@ void ArucoDetector::detectMarkers(InputArray _image, OutputArrayOfArrays _corner
detectorParams.minMarkerLengthRatioOriginalImg == 0.0));
Mat grey;
_convertToGrey(_image.getMat(), grey);
_convertToGrey(_image, grey);
// Aruco3 functionality is the extension of Aruco.
// The description can be found in:

View File

@ -38,12 +38,12 @@ void _copyVector2Output(vector<vector<Point2f> > &vec, OutputArrayOfArrays out,
}
}
void _convertToGrey(InputArray _in, OutputArray _out) {
CV_Assert(_in.type() == CV_8UC1 || _in.type() == CV_8UC3);
if(_in.type() == CV_8UC3)
void _convertToGrey(InputArray _in, Mat& _out) {
CV_Assert(_in.type() == CV_8UC1 || _in.type() == CV_8UC3 || _in.type() == CV_8UC4);
if(_in.type() != CV_8UC1)
cvtColor(_in, _out, COLOR_BGR2GRAY);
else
_in.copyTo(_out);
_out = _in.getMat();
}
}

View File

@ -16,9 +16,9 @@ namespace aruco {
void _copyVector2Output(std::vector<std::vector<Point2f> > &vec, OutputArrayOfArrays out, const float scale = 1.f);
/**
* @brief Convert input image to gray if it is a 3-channels image
* @brief Convert input image to gray if it is a BGR or BGRA image
*/
void _convertToGrey(InputArray _in, OutputArray _out);
void _convertToGrey(InputArray _in, Mat& _out);
template<typename T>
inline bool readParameter(const std::string& name, T& parameter, const FileNode& node)