From e4f207c4b4ce769445bd3384abd86c3d61be9141 Mon Sep 17 00:00:00 2001 From: atinfinity Date: Tue, 17 May 2016 22:56:03 +0900 Subject: [PATCH 1/4] Changed cv::threshold() to support CV_64F --- modules/imgproc/src/thresh.cpp | 85 ++++++++++++++++++++++++++++++++++ 1 file changed, 85 insertions(+) diff --git a/modules/imgproc/src/thresh.cpp b/modules/imgproc/src/thresh.cpp index 0b61347fd..92d630348 100644 --- a/modules/imgproc/src/thresh.cpp +++ b/modules/imgproc/src/thresh.cpp @@ -904,6 +904,85 @@ thresh_32f( const Mat& _src, Mat& _dst, float thresh, float maxval, int type ) } } +static void +thresh_64f(const Mat& _src, Mat& _dst, double thresh, double maxval, int type) +{ + int i, j; + Size roi = _src.size(); + roi.width *= _src.channels(); + const double* src = _src.ptr(); + double* dst = _dst.ptr(); + size_t src_step = _src.step / sizeof(src[0]); + size_t dst_step = _dst.step / sizeof(dst[0]); + + if (_src.isContinuous() && _dst.isContinuous()) + { + roi.width *= roi.height; + roi.height = 1; + } + + switch (type) + { + case THRESH_BINARY: + for (i = 0; i < roi.height; i++, src += src_step, dst += dst_step) + { + j = 0; + + for (; j < roi.width; j++) + dst[j] = src[j] > thresh ? maxval : 0; + } + break; + + case THRESH_BINARY_INV: + for (i = 0; i < roi.height; i++, src += src_step, dst += dst_step) + { + j = 0; + + for (; j < roi.width; j++) + dst[j] = src[j] <= thresh ? maxval : 0; + } + break; + + case THRESH_TRUNC: + for (i = 0; i < roi.height; i++, src += src_step, dst += dst_step) + { + j = 0; + + for (; j < roi.width; j++) + dst[j] = std::min(src[j], thresh); + } + break; + + case THRESH_TOZERO: + for (i = 0; i < roi.height; i++, src += src_step, dst += dst_step) + { + j = 0; + + for (; j < roi.width; j++) + { + double v = src[j]; + dst[j] = v > thresh ? v : 0; + } + } + break; + + case THRESH_TOZERO_INV: + for (i = 0; i < roi.height; i++, src += src_step, dst += dst_step) + { + j = 0; + + for (; j < roi.width; j++) + { + double v = src[j]; + dst[j] = v <= thresh ? v : 0; + } + } + break; + default: + return CV_Error(CV_StsBadArg, ""); + } +} + #ifdef HAVE_IPP static bool ipp_getThreshVal_Otsu_8u( const unsigned char* _src, int step, Size size, unsigned char &thresh) { @@ -1129,6 +1208,10 @@ public: { thresh_32f( srcStripe, dstStripe, (float)thresh, (float)maxval, thresholdType ); } + else if( srcStripe.depth() == CV_64F ) + { + thresh_64f(srcStripe, dstStripe, (double)thresh, (double)maxval, thresholdType); + } } private: @@ -1269,6 +1352,8 @@ double cv::threshold( InputArray _src, OutputArray _dst, double thresh, double m } else if( src.depth() == CV_32F ) ; + else if( src.depth() == CV_64F ) + ; else CV_Error( CV_StsUnsupportedFormat, "" ); From 6930325847f25e840ab70ec5cc2fc0e318ac1a8f Mon Sep 17 00:00:00 2001 From: atinfinity Date: Tue, 17 May 2016 22:57:05 +0900 Subject: [PATCH 2/4] Added test case of cv::threshold(CV_64F) --- modules/imgproc/test/test_thresh.cpp | 62 ++++++++++++++++++++++++---- 1 file changed, 53 insertions(+), 9 deletions(-) diff --git a/modules/imgproc/test/test_thresh.cpp b/modules/imgproc/test/test_thresh.cpp index f59fec19e..529bff70d 100644 --- a/modules/imgproc/test/test_thresh.cpp +++ b/modules/imgproc/test/test_thresh.cpp @@ -56,8 +56,8 @@ protected: void prepare_to_validation( int ); int thresh_type; - float thresh_val; - float max_val; + double thresh_val; + double max_val; }; @@ -120,7 +120,7 @@ void CV_ThreshTest::run_func() static void test_threshold( const Mat& _src, Mat& _dst, - float thresh, float maxval, int thresh_type ) + double thresh, double maxval, int thresh_type ) { int i, j; int depth = _src.depth(), cn = _src.channels(); @@ -144,7 +144,7 @@ static void test_threshold( const Mat& _src, Mat& _dst, imaxval = cvRound(maxval); } - assert( depth == CV_8U || depth == CV_16S || depth == CV_32F ); + assert( depth == CV_8U || depth == CV_16S || depth == CV_32F || depth == CV_64F ); switch( thresh_type ) { @@ -165,13 +165,20 @@ static void test_threshold( const Mat& _src, Mat& _dst, for( j = 0; j < width_n; j++ ) dst[j] = (short)(src[j] > ithresh ? imaxval : 0); } - else + else if( depth == CV_32F ) { const float* src = _src.ptr(i); float* dst = _dst.ptr(i); for( j = 0; j < width_n; j++ ) dst[j] = src[j] > thresh ? maxval : 0.f; } + else + { + const double* src = _src.ptr(i); + double* dst = _dst.ptr(i); + for( j = 0; j < width_n; j++ ) + dst[j] = src[j] > thresh ? maxval : 0.0; + } } break; case CV_THRESH_BINARY_INV: @@ -191,13 +198,20 @@ static void test_threshold( const Mat& _src, Mat& _dst, for( j = 0; j < width_n; j++ ) dst[j] = (short)(src[j] > ithresh ? 0 : imaxval); } - else + else if( depth == CV_32F ) { const float* src = _src.ptr(i); float* dst = _dst.ptr(i); for( j = 0; j < width_n; j++ ) dst[j] = src[j] > thresh ? 0.f : maxval; } + else + { + const double* src = _src.ptr(i); + double* dst = _dst.ptr(i); + for( j = 0; j < width_n; j++ ) + dst[j] = src[j] > thresh ? 0.0 : maxval; + } } break; case CV_THRESH_TRUNC: @@ -223,7 +237,7 @@ static void test_threshold( const Mat& _src, Mat& _dst, dst[j] = (short)(s > ithresh ? ithresh2 : s); } } - else + else if( depth == CV_32F ) { const float* src = _src.ptr(i); float* dst = _dst.ptr(i); @@ -233,6 +247,16 @@ static void test_threshold( const Mat& _src, Mat& _dst, dst[j] = s > thresh ? thresh : s; } } + else + { + const double* src = _src.ptr(i); + double* dst = _dst.ptr(i); + for( j = 0; j < width_n; j++ ) + { + double s = src[j]; + dst[j] = s > thresh ? thresh : s; + } + } } break; case CV_THRESH_TOZERO: @@ -258,7 +282,7 @@ static void test_threshold( const Mat& _src, Mat& _dst, dst[j] = (short)(s > ithresh ? s : 0); } } - else + else if( depth == CV_32F ) { const float* src = _src.ptr(i); float* dst = _dst.ptr(i); @@ -268,6 +292,16 @@ static void test_threshold( const Mat& _src, Mat& _dst, dst[j] = s > thresh ? s : 0.f; } } + else + { + const double* src = _src.ptr(i); + double* dst = _dst.ptr(i); + for( j = 0; j < width_n; j++ ) + { + double s = src[j]; + dst[j] = s > thresh ? s : 0.0; + } + } } break; case CV_THRESH_TOZERO_INV: @@ -293,7 +327,7 @@ static void test_threshold( const Mat& _src, Mat& _dst, dst[j] = (short)(s > ithresh ? 0 : s); } } - else + else if (depth == CV_32F) { const float* src = _src.ptr(i); float* dst = _dst.ptr(i); @@ -303,6 +337,16 @@ static void test_threshold( const Mat& _src, Mat& _dst, dst[j] = s > thresh ? 0.f : s; } } + else + { + const double* src = _src.ptr(i); + double* dst = _dst.ptr(i); + for( j = 0; j < width_n; j++ ) + { + double s = src[j]; + dst[j] = s > thresh ? 0.0 : s; + } + } } break; default: From ef0931be4445490ee8fa85b17381da2f0a45dd97 Mon Sep 17 00:00:00 2001 From: atinfinity Date: Wed, 18 May 2016 00:34:51 +0900 Subject: [PATCH 3/4] fixed compilation warning --- modules/imgproc/test/test_thresh.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/modules/imgproc/test/test_thresh.cpp b/modules/imgproc/test/test_thresh.cpp index 529bff70d..b7db66e98 100644 --- a/modules/imgproc/test/test_thresh.cpp +++ b/modules/imgproc/test/test_thresh.cpp @@ -170,7 +170,7 @@ static void test_threshold( const Mat& _src, Mat& _dst, const float* src = _src.ptr(i); float* dst = _dst.ptr(i); for( j = 0; j < width_n; j++ ) - dst[j] = src[j] > thresh ? maxval : 0.f; + dst[j] = (float)(src[j] > thresh ? maxval : 0.f); } else { @@ -203,7 +203,7 @@ static void test_threshold( const Mat& _src, Mat& _dst, const float* src = _src.ptr(i); float* dst = _dst.ptr(i); for( j = 0; j < width_n; j++ ) - dst[j] = src[j] > thresh ? 0.f : maxval; + dst[j] = (float)(src[j] > thresh ? 0.f : maxval); } else { @@ -244,7 +244,7 @@ static void test_threshold( const Mat& _src, Mat& _dst, for( j = 0; j < width_n; j++ ) { float s = src[j]; - dst[j] = s > thresh ? thresh : s; + dst[j] = (float)(s > thresh ? thresh : s); } } else From 1f1464c925e774f89089f89f919680e2e1d6096c Mon Sep 17 00:00:00 2001 From: atinfinity Date: Wed, 18 May 2016 08:32:29 +0900 Subject: [PATCH 4/4] remove unnecessary cast --- modules/imgproc/src/thresh.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/imgproc/src/thresh.cpp b/modules/imgproc/src/thresh.cpp index 92d630348..13f0fa284 100644 --- a/modules/imgproc/src/thresh.cpp +++ b/modules/imgproc/src/thresh.cpp @@ -1210,7 +1210,7 @@ public: } else if( srcStripe.depth() == CV_64F ) { - thresh_64f(srcStripe, dstStripe, (double)thresh, (double)maxval, thresholdType); + thresh_64f(srcStripe, dstStripe, thresh, maxval, thresholdType); } }