mirror of
https://github.com/zebrajr/opencv.git
synced 2025-12-06 12:19:50 +01: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:
parent
ab77e1cfc8
commit
3cbb4acd2d
|
|
@ -329,7 +329,8 @@ enum ThresholdTypes {
|
|||
THRESH_TOZERO_INV = 4, //!< \f[\texttt{dst} (x,y) = \fork{0}{if \(\texttt{src}(x,y) > \texttt{thresh}\)}{\texttt{src}(x,y)}{otherwise}\f]
|
||||
THRESH_MASK = 7,
|
||||
THRESH_OTSU = 8, //!< flag, use Otsu algorithm to choose the optimal threshold value
|
||||
THRESH_TRIANGLE = 16 //!< flag, use Triangle algorithm to choose the optimal threshold value
|
||||
THRESH_TRIANGLE = 16, //!< flag, use Triangle algorithm to choose the optimal threshold value
|
||||
THRESH_DRYRUN = 128 //!< flag, compute threshold only (useful for OTSU/TRIANGLE) but does not actually run thresholding
|
||||
};
|
||||
|
||||
//! adaptive threshold algorithm
|
||||
|
|
|
|||
|
|
@ -610,9 +610,11 @@ enum
|
|||
CV_THRESH_MASK =7,
|
||||
CV_THRESH_OTSU =8, /**< use Otsu algorithm to choose the optimal threshold value;
|
||||
combine the flag with one of the above CV_THRESH_* values */
|
||||
CV_THRESH_TRIANGLE =16 /**< use Triangle algorithm to choose the optimal threshold value;
|
||||
CV_THRESH_TRIANGLE =16, /**< use Triangle algorithm to choose the optimal threshold value;
|
||||
combine the flag with one of the above CV_THRESH_* values, but not
|
||||
with CV_THRESH_OTSU */
|
||||
CV_THRESH_DRYRUN =128 /**< compute threshold only (useful for OTSU/TRIANGLE) but does not
|
||||
actually run thresholding */
|
||||
};
|
||||
|
||||
/** Adaptive threshold methods */
|
||||
|
|
|
|||
|
|
@ -1404,8 +1404,11 @@ 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 ||
|
||||
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;
|
||||
|
|
@ -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();
|
||||
|
||||
if (!isDisabled)
|
||||
_dst.create( src.size(), src.type() );
|
||||
Mat dst = _dst.getMat();
|
||||
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));
|
||||
|
|
|
|||
|
|
@ -386,6 +386,40 @@ OCL_TEST_P(Threshold, Mat)
|
|||
}
|
||||
}
|
||||
|
||||
struct Threshold_Dryrun :
|
||||
public ImgprocTestBase
|
||||
{
|
||||
int thresholdType;
|
||||
|
||||
virtual void SetUp()
|
||||
{
|
||||
type = GET_PARAM(0);
|
||||
thresholdType = GET_PARAM(2);
|
||||
useRoi = GET_PARAM(3);
|
||||
}
|
||||
};
|
||||
|
||||
OCL_TEST_P(Threshold_Dryrun, Mat)
|
||||
{
|
||||
for (int j = 0; j < test_loop_times; j++)
|
||||
{
|
||||
random_roi();
|
||||
|
||||
double maxVal = randomDouble(20.0, 127.0);
|
||||
double thresh = randomDouble(0.0, maxVal);
|
||||
|
||||
const int _thresholdType = thresholdType | THRESH_DRYRUN;
|
||||
|
||||
src_roi.copyTo(dst_roi);
|
||||
usrc_roi.copyTo(udst_roi);
|
||||
|
||||
OCL_OFF(cv::threshold(src_roi, dst_roi, thresh, maxVal, _thresholdType));
|
||||
OCL_ON(cv::threshold(usrc_roi, udst_roi, thresh, maxVal, _thresholdType));
|
||||
|
||||
OCL_EXPECT_MATS_NEAR(dst, 0);
|
||||
}
|
||||
}
|
||||
|
||||
/////////////////////////////////////////// CLAHE //////////////////////////////////////////////////
|
||||
|
||||
PARAM_TEST_CASE(CLAHETest, Size, double, bool)
|
||||
|
|
@ -483,6 +517,16 @@ OCL_INSTANTIATE_TEST_CASE_P(Imgproc, Threshold, Combine(
|
|||
ThreshOp(THRESH_TOZERO), ThreshOp(THRESH_TOZERO_INV)),
|
||||
Bool()));
|
||||
|
||||
OCL_INSTANTIATE_TEST_CASE_P(Imgproc, Threshold_Dryrun, Combine(
|
||||
Values(CV_8UC1, CV_8UC2, CV_8UC3, CV_8UC4,
|
||||
CV_16SC1, CV_16SC2, CV_16SC3, CV_16SC4,
|
||||
CV_32FC1, CV_32FC2, CV_32FC3, CV_32FC4),
|
||||
Values(0),
|
||||
Values(ThreshOp(THRESH_BINARY),
|
||||
ThreshOp(THRESH_BINARY_INV), ThreshOp(THRESH_TRUNC),
|
||||
ThreshOp(THRESH_TOZERO), ThreshOp(THRESH_TOZERO_INV)),
|
||||
Bool()));
|
||||
|
||||
OCL_INSTANTIATE_TEST_CASE_P(Imgproc, CLAHETest, Combine(
|
||||
Values(Size(4, 4), Size(32, 8), Size(8, 64)),
|
||||
Values(0.0, 10.0, 62.0, 300.0),
|
||||
|
|
|
|||
|
|
@ -502,6 +502,25 @@ BIGDATA_TEST(Imgproc_Threshold, huge)
|
|||
ASSERT_EQ((uint64)nz, n / 2);
|
||||
}
|
||||
|
||||
TEST(Imgproc_Threshold, threshold_dryrun)
|
||||
{
|
||||
Size sz(16, 16);
|
||||
Mat input_original(sz, CV_8U, Scalar::all(2));
|
||||
Mat input = input_original.clone();
|
||||
std::vector<int> threshTypes = {THRESH_BINARY, THRESH_BINARY_INV, THRESH_TRUNC, THRESH_TOZERO, THRESH_TOZERO_INV};
|
||||
std::vector<int> threshFlags = {0, THRESH_OTSU, THRESH_TRIANGLE};
|
||||
for(int threshType : threshTypes)
|
||||
{
|
||||
for(int threshFlag : threshFlags)
|
||||
{
|
||||
const int _threshType = threshType | threshFlag | THRESH_DRYRUN;
|
||||
cv::threshold(input, input, 2.0, 0.0, _threshType);
|
||||
EXPECT_MAT_NEAR(input, input_original, 0);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
TEST(Imgproc_Threshold, regression_THRESH_TOZERO_IPP_16085)
|
||||
{
|
||||
Size sz(16, 16);
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user