From 306204089f272e6d6c4a86a93b5c72b0773eeef4 Mon Sep 17 00:00:00 2001 From: Alexander Smorkalov Date: Mon, 5 May 2025 20:58:57 +0300 Subject: [PATCH] Reworked HSV color conversion tables initialization for OpenCL branch. --- modules/imgproc/src/color_hsv.dispatch.cpp | 69 ++++++++++++---------- 1 file changed, 39 insertions(+), 30 deletions(-) diff --git a/modules/imgproc/src/color_hsv.dispatch.cpp b/modules/imgproc/src/color_hsv.dispatch.cpp index 8639784927..7ba30cc71e 100644 --- a/modules/imgproc/src/color_hsv.dispatch.cpp +++ b/modules/imgproc/src/color_hsv.dispatch.cpp @@ -257,6 +257,34 @@ bool oclCvtColorBGR2HLS( InputArray _src, OutputArray _dst, int bidx, bool full return h.run(); } +static UMat init_sdiv_table() +{ + std::vector sdiv(256); + + const int hsv_shift = 12; + const int v = 255 << hsv_shift; + + sdiv[0] = 0; + for(int i = 1; i < 256; i++ ) + sdiv[i] = saturate_cast(v/(1.*i)); + + return UMat(sdiv, true); +} + +static UMat init_hdiv_table(int hrange) +{ + std::vector hdiv(256); + + const int hsv_shift = 12; + const int v = hrange << hsv_shift; + + hdiv[0] = 0; + for (int i = 1; i < 256; i++ ) + hdiv[i] = saturate_cast(v/(6.*i)); + + return UMat(hdiv, true); +} + bool oclCvtColorBGR2HSV( InputArray _src, OutputArray _dst, int bidx, bool full ) { OclHelper< Set<3, 4>, Set<3>, Set > h(_src, _dst, 3); @@ -274,41 +302,22 @@ bool oclCvtColorBGR2HSV( InputArray _src, OutputArray _dst, int bidx, bool full if(_src.depth() == CV_8U) { - static UMat sdiv_data; - static UMat hdiv_data180; - static UMat hdiv_data256; - static int sdiv_table[256]; - static int hdiv_table180[256]; - static int hdiv_table256[256]; - static volatile bool initialized180 = false, initialized256 = false; - volatile bool & initialized = hrange == 180 ? initialized180 : initialized256; + static UMat sdiv_data = init_sdiv_table(); + UMat hdiv_data; - if (!initialized) + if (hrange == 180) { - int * const hdiv_table = hrange == 180 ? hdiv_table180 : hdiv_table256, hsv_shift = 12; - UMat & hdiv_data = hrange == 180 ? hdiv_data180 : hdiv_data256; - - sdiv_table[0] = hdiv_table180[0] = hdiv_table256[0] = 0; - - int v = 255 << hsv_shift; - if (!initialized180 && !initialized256) - { - for(int i = 1; i < 256; i++ ) - sdiv_table[i] = saturate_cast(v/(1.*i)); - Mat(1, 256, CV_32SC1, sdiv_table).copyTo(sdiv_data); - } - - v = hrange << hsv_shift; - for (int i = 1; i < 256; i++ ) - hdiv_table[i] = saturate_cast(v/(6.*i)); - - Mat(1, 256, CV_32SC1, hdiv_table).copyTo(hdiv_data); - initialized = true; + static UMat hdiv_data180 = init_hdiv_table(180); + hdiv_data = hdiv_data180; + } + else + { + static UMat hdiv_data256 = init_hdiv_table(256); + hdiv_data = hdiv_data256; } h.setArg(ocl::KernelArg::PtrReadOnly(sdiv_data)); - h.setArg(hrange == 256 ? ocl::KernelArg::PtrReadOnly(hdiv_data256) : - ocl::KernelArg::PtrReadOnly(hdiv_data180)); + h.setArg(ocl::KernelArg::PtrReadOnly(hdiv_data)); } return h.run();