1
0
mirror of https://github.com/opencv/opencv.git synced 2026-07-31 08:13:04 +04:00

Merge pull request #26836 from chacha21:thresholding_compute_threshold_only

Add cv::THRESH_DRYRUN flag to get adaptive threshold values without thresholding #26836

A first proposal for #26777

Adds a `cv::THRESH_DRYRUN` flag to let cv::threshold() compute the threshold (useful for OTSU/TRIANGLE), but without actually running the thresholding. This flags is a proposal instead of a new function cv::computeThreshold()

- [x] I agree to contribute to the project under Apache 2 License.
- [x] 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
- [X] The PR is proposed to the proper branch
- [X] There is a reference to the original bug report and related work
- [X] 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
This commit is contained in:
Pierre Chatelier
2025-01-24 12:25:21 +01:00
committed by GitHub
parent ab77e1cfc8
commit 3cbb4acd2d
5 changed files with 92 additions and 7 deletions
+24 -5
View File
@@ -1404,10 +1404,13 @@ static bool ocl_threshold( InputArray _src, OutputArray _dst, double & thresh, d
int type = _src.type(), depth = CV_MAT_DEPTH(type), cn = CV_MAT_CN(type),
kercn = ocl::predictOptimalVectorWidth(_src, _dst), ktype = CV_MAKE_TYPE(depth, kercn);
bool doubleSupport = ocl::Device::getDefault().doubleFPConfig() > 0;
const bool isDisabled = ((thresh_type & THRESH_DRYRUN) != 0);
thresh_type &= ~THRESH_DRYRUN;
if ( !(thresh_type == THRESH_BINARY || thresh_type == THRESH_BINARY_INV || thresh_type == THRESH_TRUNC ||
thresh_type == THRESH_TOZERO || thresh_type == THRESH_TOZERO_INV) ||
(!doubleSupport && depth == CV_64F))
if ( isDisabled ||
!(thresh_type == THRESH_BINARY || thresh_type == THRESH_BINARY_INV || thresh_type == THRESH_TRUNC ||
thresh_type == THRESH_TOZERO || thresh_type == THRESH_TOZERO_INV) ||
(!doubleSupport && depth == CV_64F))
return false;
const char * const thresholdMap[] = { "THRESH_BINARY", "THRESH_BINARY_INV", "THRESH_TRUNC",
@@ -1544,10 +1547,14 @@ double cv::threshold( InputArray _src, OutputArray _dst, double thresh, double m
CV_OCL_RUN_(_src.dims() <= 2 && _dst.isUMat(),
ocl_threshold(_src, _dst, thresh, maxval, type), thresh)
const bool isDisabled = ((type & THRESH_DRYRUN) != 0);
type &= ~THRESH_DRYRUN;
Mat src = _src.getMat();
_dst.create( src.size(), src.type() );
Mat dst = _dst.getMat();
if (!isDisabled)
_dst.create( src.size(), src.type() );
Mat dst = isDisabled ? cv::Mat() : _dst.getMat();
int automatic_thresh = (type & ~cv::THRESH_MASK);
type &= THRESH_MASK;
@@ -1574,6 +1581,9 @@ double cv::threshold( InputArray _src, OutputArray _dst, double thresh, double m
{
int ithresh = cvFloor(thresh);
thresh = ithresh;
if (isDisabled)
return thresh;
int imaxval = cvRound(maxval);
if( type == THRESH_TRUNC )
imaxval = ithresh;
@@ -1605,6 +1615,9 @@ double cv::threshold( InputArray _src, OutputArray _dst, double thresh, double m
{
int ithresh = cvFloor(thresh);
thresh = ithresh;
if (isDisabled)
return thresh;
int imaxval = cvRound(maxval);
if( type == THRESH_TRUNC )
imaxval = ithresh;
@@ -1632,6 +1645,9 @@ double cv::threshold( InputArray _src, OutputArray _dst, double thresh, double m
{
int ithresh = cvFloor(thresh);
thresh = ithresh;
if (isDisabled)
return thresh;
int imaxval = cvRound(maxval);
if (type == THRESH_TRUNC)
imaxval = ithresh;
@@ -1663,6 +1679,9 @@ double cv::threshold( InputArray _src, OutputArray _dst, double thresh, double m
else
CV_Error( cv::Error::StsUnsupportedFormat, "" );
if (isDisabled)
return thresh;
parallel_for_(Range(0, dst.rows),
ThresholdRunner(src, dst, thresh, maxval, type),
dst.total()/(double)(1<<16));