8000 imgproc: add minEnclosingConvexPolygon by SaraKuhnert · Pull Request #27369 · opencv/opencv · GitHub
[go: up one dir, main page]

Skip to content

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

Open
wants to merge 2 commits into
base: 4.x
Choose a base branch
from

Conversation

SaraKuhnert
Copy link

Pull Request Readiness Checklist

  • I agree to contribute to the project under Apache 2 License.
  • To the best of my knowledge, the proposed patch is not based on a code under GPL or another license that is incompatible with OpenCV
  • The PR is proposed to the proper branch
  • There is a reference to the original bug report and related work
  • There is accuracy test, performance test and test data in opencv_extra repository, if applicable
    Patch to opencv_extra has the same branch name.
  • The feature is well documented and sample code can be built with the project CMake

@asmorkalov
Copy link
Contributor

@asmorkalov
Copy link
Contributor

Discussed the solution on the core team meeting. Please go ahead with the PR.

@asmorkalov asmorkalov requested review from vpisarev June 18, 2025 06:43
Comment on lines -3073 to -3107
@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 );
Copy link
Contributor

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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The same

Comment on lines +27 to +62
//
//
//
// 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*/
Copy link
Contributor

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).

Comment on lines +86 to +89
static void findMinEnclosingPolygon(cv::InputArray points,
const int &k,
CV_OUT cv::OutputArray &kgon,
CV_OUT double &area);
Copy link
Contributor

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.

Comment on lines +1074 to +1127
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;
}
Copy link
Contributor

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("");
Copy link
Contributor

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.

Comment on lines +957 to +960
if(h == 0)
{
throw std::logic_error("");
}
Copy link
Contributor

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.

Comment on lines +1163 to +1165
area = cv::contourArea(minPolygon);

return;
Copy link
Contributor

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.

@asmorkalov
Copy link
Contributor

Friendly reminder.

@asmorkalov asmorkalov added this to the 4.13.0 milestone Jun 27, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants
0