Skip to content

Commit 7ae1946

Browse files
committed
Merge pull request opencv#10171 from ElenaGvozdeva:Threshold
2 parents fe95d5a + 73ac532 commit 7ae1946

File tree

5 files changed

+74
-9
lines changed

5 files changed

+74
-9
lines changed

modules/imgproc/include/opencv2/imgproc.hpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2805,7 +2805,8 @@ The function can process the image in-place.
28052805
@param src Source 8-bit single-channel image.
28062806
@param dst Destination image of the same size and the same type as src.
28072807
@param maxValue Non-zero value assigned to the pixels for which the condition is satisfied
2808-
@param adaptiveMethod Adaptive thresholding algorithm to use, see cv::AdaptiveThresholdTypes
2808+
@param adaptiveMethod Adaptive thresholding algorithm to use, see cv::AdaptiveThresholdTypes.
2809+
The BORDER_REPLICATE | BORDER_ISOLATED is used to process boundaries.
28092810
@param thresholdType Thresholding type that must be either THRESH_BINARY or THRESH_BINARY_INV,
28102811
see cv::ThresholdTypes.
28112812
@param blockSize Size of a pixel neighborhood that is used to calculate a threshold value for the

modules/imgproc/include/opencv2/imgproc/hal/interface.h

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,26 @@
2121
#define CV_HAL_MORPH_DILATE 1
2222
//! @}
2323

24+
//! @name Threshold types
25+
//! @sa cv::ThresholdTypes
26+
//! @{
27+
#define CV_HAL_THRESH_BINARY 0
28+
#define CV_HAL_THRESH_BINARY_INV 1
29+
#define CV_HAL_THRESH_TRUNC 2
30+
#define CV_HAL_THRESH_TOZERO 3
31+
#define CV_HAL_THRESH_TOZERO_INV 4
32+
#define CV_HAL_THRESH_MASK 7
33+
#define CV_HAL_THRESH_OTSU 8
34+
#define CV_HAL_THRESH_TRIANGLE 16
35+
//! @}
36+
37+
//! @name Adaptive threshold algorithm
38+
//! @sa cv::AdaptiveThresholdTypes
39+
//! @{
40+
#define CV_HAL_ADAPTIVE_THRESH_MEAN_C 0
41+
#define CV_HAL_ADAPTIVE_THRESH_GAUSSIAN_C 1
42+
//! @}
43+
2444
//! @}
2545

2646
#endif

modules/imgproc/perf/perf_threshold.cpp

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -60,28 +60,31 @@ PERF_TEST_P(Size_Only, threshold_otsu, testing::Values(TYPICAL_MAT_SIZES))
6060
CV_ENUM(AdaptThreshType, THRESH_BINARY, THRESH_BINARY_INV)
6161
CV_ENUM(AdaptThreshMethod, ADAPTIVE_THRESH_MEAN_C, ADAPTIVE_THRESH_GAUSSIAN_C)
6262

63-
typedef std::tr1::tuple<Size, AdaptThreshType, AdaptThreshMethod, int> Size_AdaptThreshType_AdaptThreshMethod_BlockSize_t;
64-
typedef perf::TestBaseWithParam<Size_AdaptThreshType_AdaptThreshMethod_BlockSize_t> Size_AdaptThreshType_AdaptThreshMethod_BlockSize;
63+
typedef std::tr1::tuple<Size, AdaptThreshType, AdaptThreshMethod, int, double> Size_AdaptThreshType_AdaptThreshMethod_BlockSize_Delta_t;
64+
typedef perf::TestBaseWithParam<Size_AdaptThreshType_AdaptThreshMethod_BlockSize_Delta_t> Size_AdaptThreshType_AdaptThreshMethod_BlockSize_Delta;
6565

66-
PERF_TEST_P(Size_AdaptThreshType_AdaptThreshMethod_BlockSize, adaptiveThreshold,
66+
PERF_TEST_P(Size_AdaptThreshType_AdaptThreshMethod_BlockSize_Delta, adaptiveThreshold,
6767
testing::Combine(
6868
testing::Values(TYPICAL_MAT_SIZES),
6969
AdaptThreshType::all(),
7070
AdaptThreshMethod::all(),
71-
testing::Values(3, 5)
71+
testing::Values(3, 5),
72+
testing::Values(0.0, 10.0)
7273
)
7374
)
7475
{
7576
Size sz = get<0>(GetParam());
7677
AdaptThreshType adaptThreshType = get<1>(GetParam());
7778
AdaptThreshMethod adaptThreshMethod = get<2>(GetParam());
7879
int blockSize = get<3>(GetParam());
80+
double C = get<4>(GetParam());
7981

8082
double maxValue = theRNG().uniform(1, 254);
81-
double C = 10.0;
8283

8384
int type = CV_8UC1;
84-
Mat src(sz, type);
85+
86+
Mat src_full(cv::Size(sz.width + 2, sz.height + 2), type);
87+
Mat src = src_full(cv::Rect(1, 1, sz.width, sz.height));
8588
Mat dst(sz, type);
8689

8790
declare.in(src, WARMUP_RNG).out(dst);

modules/imgproc/src/hal_replacement.hpp

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -630,6 +630,40 @@ inline int hal_ni_medianBlur(const uchar* src_data, size_t src_step, uchar* dst_
630630
#define cv_hal_medianBlur hal_ni_medianBlur
631631
//! @endcond
632632

633+
/**
634+
@brief Calculates adaptive threshold
635+
@param src_data,src_step Source image
636+
@param dst_data,dst_step Destination image
637+
@param width,height Source image dimensions
638+
@param maxValue Value assigned to the pixels for which the condition is satisfied
639+
@param adaptiveMethod Adaptive thresholding algorithm
640+
@param thresholdType Thresholding type
641+
@param blockSize Size of a pixel neighborhood that is used to calculate a threshold value
642+
@param C Constant subtracted from the mean or weighted mean
643+
*/
644+
inline int hal_ni_adaptiveThreshold(const uchar* src_data, size_t src_step, uchar* dst_data, size_t dst_step, int width, int height, double maxValue, int adaptiveMethod, int thresholdType, int blockSize, double C) { return CV_HAL_ERROR_NOT_IMPLEMENTED; }
645+
646+
//! @cond IGNORED
647+
#define cv_hal_adaptiveThreshold hal_ni_adaptiveThreshold
648+
//! @endcond
649+
650+
/**
651+
@brief Calculates fixed-level threshold to each array element
652+
@param src_data,src_step Source image
653+
@param dst_data,dst_step Destination image
654+
@param width,height Source image dimensions
655+
@param depth Depths of source and destination image
656+
@param cn Number of channels
657+
@param thresh Threshold value
658+
@param maxValue Value assigned to the pixels for which the condition is satisfied
659+
@param thresholdType Thresholding type
660+
*/
661+
inline int hal_ni_threshold(const uchar* src_data, size_t src_step, uchar* dst_data, size_t dst_step, int width, int height, int depth, int cn, double thresh, double maxValue, int thresholdType) { return CV_HAL_ERROR_NOT_IMPLEMENTED; }
662+
663+
//! @cond IGNORED
664+
#define cv_hal_threshold hal_ni_threshold
665+
//! @endcond
666+
633667
//! @}
634668

635669
#if defined __GNUC__

modules/imgproc/src/thresh.cpp

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1214,6 +1214,10 @@ class ThresholdRunner : public ParallelLoopBody
12141214
Mat srcStripe = src.rowRange(row0, row1);
12151215
Mat dstStripe = dst.rowRange(row0, row1);
12161216

1217+
CALL_HAL(threshold, cv_hal_threshold, srcStripe.data, srcStripe.step, dstStripe.data, dstStripe.step,
1218+
srcStripe.cols, srcStripe.rows, srcStripe.depth(), srcStripe.channels(),
1219+
thresh, maxval, thresholdType);
1220+
12171221
if (srcStripe.depth() == CV_8U)
12181222
{
12191223
thresh_8u( srcStripe, dstStripe, (uchar)thresh, (uchar)maxval, thresholdType );
@@ -1530,20 +1534,23 @@ void cv::adaptiveThreshold( InputArray _src, OutputArray _dst, double maxValue,
15301534
return;
15311535
}
15321536

1537+
CALL_HAL(adaptiveThreshold, cv_hal_adaptiveThreshold, src.data, src.step, dst.data, dst.step, src.cols, src.rows,
1538+
maxValue, method, type, blockSize, delta);
1539+
15331540
Mat mean;
15341541

15351542
if( src.data != dst.data )
15361543
mean = dst;
15371544

15381545
if (method == ADAPTIVE_THRESH_MEAN_C)
15391546
boxFilter( src, mean, src.type(), Size(blockSize, blockSize),
1540-
Point(-1,-1), true, BORDER_REPLICATE );
1547+
Point(-1,-1), true, BORDER_REPLICATE|BORDER_ISOLATED );
15411548
else if (method == ADAPTIVE_THRESH_GAUSSIAN_C)
15421549
{
15431550
Mat srcfloat,meanfloat;
15441551
src.convertTo(srcfloat,CV_32F);
15451552
meanfloat=srcfloat;
1546-
GaussianBlur(srcfloat, meanfloat, Size(blockSize, blockSize), 0, 0, BORDER_REPLICATE);
1553+
GaussianBlur(srcfloat, meanfloat, Size(blockSize, blockSize), 0, 0, BORDER_REPLICATE|BORDER_ISOLATED);
15471554
meanfloat.convertTo(mean, src.type());
15481555
}
15491556
else

0 commit comments

Comments
 (0)