-
-
Notifications
You must be signed in to change notification settings - Fork 56.2k
imgproc: add minEnclosingConvexPolygon #27369
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: 4.x
Are you sure you want to change the base?
Conversation
OpenCV already proposes:
What is the use case of the new function that is not covered in OpenCV? |
Discussed the solution on the core team meeting. Please go ahead with the PR. |
@note Currently, the Otsu's method is implemented only for CV_8UC1 and CV_16UC1 images, | ||
and the Triangle's method is implemented only for CV_8UC1 images. | ||
@note Currently, the Otsu's and Triangle methods are implemented only for 8-bit single-channel images. | ||
|
||
@param src input array (multiple-channel, CV_8U, CV_16S, CV_16U, CV_32F or CV_64F). | ||
@param src input array (multiple-channel, 8-bit or 32-bit floating point). | ||
@param dst output array of the same size and type and the same number of channels as src. | ||
@param thresh threshold value. | ||
@param maxval maximum value to use with the #THRESH_BINARY and #THRESH_BINARY_INV thresholding | ||
types. | ||
@param type thresholding type (see #ThresholdTypes). | ||
@return the computed threshold value if Otsu's or Triangle methods used. | ||
|
||
@sa thresholdWithMask, adaptiveThreshold, findContours, compare, min, max | ||
@sa adaptiveThreshold, findContours, compare, min, max | ||
*/ | ||
CV_EXPORTS_W double threshold( InputArray src, OutputArray dst, | ||
double thresh, double maxval, int type ); | ||
|
||
/** @brief Same as #threshold, but with an optional mask | ||
|
||
@note If the mask is empty, #thresholdWithMask is equivalent to #threshold. | ||
If the mask is not empty, dst *must* be of the same size and type as src, so that | ||
outliers pixels are left as-is | ||
|
||
@param src input array (multiple-channel, 8-bit or 32-bit floating point). | ||
@param dst output array of the same size and type and the same number of channels as src. | ||
@param mask optional mask (same size as src, 8-bit). | ||
@param thresh threshold value. | ||
@param maxval maximum value to use with the #THRESH_BINARY and #THRESH_BINARY_INV thresholding | ||
types. | ||
@param type thresholding type (see #ThresholdTypes). | ||
@return the computed threshold value if Otsu's or Triangle methods used. | ||
|
||
@sa threshold, adaptiveThreshold, findContours, compare, min, max | ||
*/ | ||
CV_EXPORTS_W double thresholdWithMask( InputArray src, InputOutputArray dst, InputArray mask, | ||
double thresh, double maxval, int type ); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The changes are not relevant. Looks like you need to rebase on top of 4.x and properly fix conflicts.
@@ -4173,9 +4149,7 @@ CV_EXPORTS_W RotatedRect minAreaRect( InputArray points ); | |||
|
|||
/** @brief Finds the four vertices of a rotated rect. Useful to draw the rotated rectangle. | |||
|
|||
The function finds the four vertices of a rotated rectangle. The four vertices are returned | |||
in clockwise order starting from the point with greatest \f$y\f$. If two points have the | |||
same \f$y\f$ coordinate the rightmost is the starting point. This function is useful to draw the |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The same
// | ||
// | ||
// | ||
// License Agreement | ||
// For Open Source Computer Vision Library | ||
// | ||
// Copyright (C) 2000, Intel Corporation, all rights reserved. | ||
// Copyright (C) 2013, OpenCV Foundation, all rights reserved. | ||
// Copyright (C) 2020, Sara Kuhnert, all rights reserved. | ||
// Third party copyrights are property of their respective owners. | ||
// | ||
// Redistribution and use in source and binary forms, with or without modification, | ||
// are permitted provided that the following conditions are met: | ||
// | ||
// * Redistribution's of source code must retain the above copyright notice, | ||
// this list of conditions and the following disclaimer. | ||
// | ||
// * Redistribution's in binary form must reproduce the above copyright notice, | ||
// this list of conditions and the following disclaimer in the documentation | ||
// and/or other materials provided with the distribution. | ||
// | ||
// * The name of the copyright holders may not be used to endorse or promote products | ||
// derived from this software without specific prior written permission. | ||
// | ||
// This software is provided by the copyright holders and contributors "as is" and | ||
// any express or implied warranties, including, but not limited to, the implied | ||
// warranties of merchantability and fitness for a particular purpose are disclaimed. | ||
// In no event shall the Intel Corporation or contributors be liable for any direct, | ||
// indirect, incidental, special, exemplary, or consequential damages | ||
// (including, but not limited to, procurement of substitute goods or services; | ||
// loss of use, data, or profits; or business interruption) however caused | ||
// and on any theory of liability, whether in contract, strict liability, | ||
// or tort (including negligence or otherwise) arising in any way out of | ||
// the use of this software, even if advised of the possibility of such damage. | ||
// | ||
//M*/ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please remove old copyright headers and use only the new one (top 5 lines).
static void findMinEnclosingPolygon(cv::InputArray points, | ||
const int &k, | ||
CV_OUT cv::OutputArray &kgon, | ||
CV_OUT double &area); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
CV_OUT macro is not needed in the implementation.
try | ||
{ | ||
if (ngon.size() < 3) | ||
{ | ||
throw std::invalid_argument( | ||
"ngon must have 3 or more different points enclosing an area" ); | ||
} | ||
if (cv::contourArea(ngon) < EPSILON ) | ||
{ | ||
throw std::invalid_argument( "invalid ngon: all points on line" ); | ||
} | ||
if (k <= 3) | ||
{ | ||
throw std::invalid_argument( "k must be 3 or higher" ); | ||
} | ||
const size_t n = ngon.size(); | ||
if ((const int)n == k) | ||
{ | ||
throw std::runtime_error ("(n = k)"); | ||
} | ||
if ((const int)n < k) | ||
{ | ||
throw std::runtime_error ("(n < k)"); | ||
} | ||
} | ||
catch (std::invalid_argument &message) | ||
{ | ||
std::cout << "invalid argument: " << message.what() << std::endl; | ||
return; | ||
} | ||
catch (std::runtime_error &message) | ||
{ | ||
std::cout << "Warning: no minimum area polygon calculated " << message.what() << std::endl; | ||
cv::Mat(ngon).copyTo(minPolygon); | ||
area = cv::contourArea(minPolygon); | ||
return; | ||
} | ||
catch (...) | ||
{ | ||
std::cout << "input error" << std::endl; | ||
return; | ||
} | ||
|
||
try | ||
{ | ||
std::vector<cv::Point2f> kgon; | ||
findMinAreaPolygon(ngon, kgon, area, k); | ||
cv::Mat(kgon).copyTo(minPolygon); | ||
} | ||
catch (...) | ||
{ | ||
std::cout << "correct input but execution failed" << std::endl; | ||
return; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please do not use try-catch. OpenCV uses CV_Assert, CV_Check for the function input validation and and CV_LOG_DEBUG/CV_LOG_INFO for notifications. Function should not silently ignore obvious invalid inputs like not enough points or wrong data types and ranges.
} | ||
else | ||
{ | ||
throw std::logic_error(""); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OpenCV uses CV_Assert for such purposes.
if(h == 0) | ||
{ | ||
throw std::logic_error(""); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OpenCV uses CV_Assert for such purposes.
area = cv::contourArea(minPolygon); | ||
|
||
return; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It makes sense to return area rather use double& parameter.
Friendly reminder. |
Pull Request Readiness Checklist
Patch to opencv_extra has the same branch name.