Skip to content

Commit 83e524d

Browse files
committed
*fix bug in Base optimization of function BgrToLab (difference with OpenCV).
1 parent f56e46d commit 83e524d

File tree

6 files changed

+66
-16
lines changed

6 files changed

+66
-16
lines changed

src/Simd/SimdAvx512bwSynetConvolution16bNhwcDeptwise.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ namespace Simd
7272
size_t sizeF = AlignLo(size, F), size2F = AlignLo(size, 2 * F), size4F = AlignLo(size, 4 * F), size8F = AlignLo(size, 8 * F);
7373
size_t dstW = p.dstW, dstW2 = AlignLo(dstW, 2), dstW4 = AlignLo(dstW, 4);
7474
__mmask16 tail = TailMask16(size - sizeF);
75-
__m512 d00, d01, d02, d03, d10, d11, d12, d13, d20, d21, d22, d23, d30, d31, d32, d33, w0, s0, s1;
75+
__m512 d00, d01, d02, d03, d10, d11, d12, d13, d20, d21, d22, d23, d30, d31, d32, d33, w0;
7676

7777
for (size_t dy = 0; dy < p.dstH; ++dy)
7878
{

src/Simd/SimdBaseBgrToLab.cpp

Lines changed: 36 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,18 +24,26 @@
2424
#include "Simd/SimdBgrToLab.h"
2525
#include "Simd/SimdBase.h"
2626
#include "Simd/SimdMath.h"
27+
#include "Simd/SimdLog.h"
2728

2829
namespace Simd
2930
{
3031
namespace Base
3132
{
33+
#define SIMD_BGR_TO_LAB_OPENCV_COMPATIBILITY
34+
3235
namespace LabV1
3336
{
3437
inline double FromRaw(uint64_t raw)
3538
{
3639
return *((double*)&raw);
3740
}
3841

42+
SIMD_INLINE float mullAdd(float a, float b, float c)
43+
{
44+
return a * b + c;
45+
}
46+
3947
#define CV_DESCALE(x,n) (((x) + (1 << ((n)-1))) >> (n))
4048
const int xyz_shift = 12;
4149

@@ -65,7 +73,6 @@ namespace Simd
6573
};
6674

6775
enum { LAB_CBRT_TAB_SIZE = 1024, GAMMA_TAB_SIZE = 1024 };
68-
static const float LabCbrtTabScale = float(LAB_CBRT_TAB_SIZE * 2) / float(3);
6976

7077
static const float GammaTabScale((int)GAMMA_TAB_SIZE);
7178

@@ -88,8 +95,6 @@ namespace Simd
8895
static const float lthresh = float(216) / float(24389); // 0.008856f = (6/29)^3
8996
static const float lscale = float(841) / float(108); // 7.787f = (29/3)^3/(29*4)
9097
static const float lbias = float(16) / float(116);
91-
//static const softfloat f255(255);
92-
9398

9499
static inline float applyGamma(float x)
95100
{
@@ -119,9 +124,14 @@ namespace Simd
119124
for (int i = 0; i < LAB_CBRT_TAB_SIZE_B; i++)
120125
{
121126
float x = cbTabScale * float(i);
122-
LabCbrtTab_b[i] = (uint16_t)(Round(lshift2 * (x < lthresh ? (x * lscale + lbias) : cbrt(x))));
127+
LabCbrtTab_b[i] = (uint16_t)(Round(lshift2 * (x < lthresh ? mullAdd(x, lscale, lbias) : cbrt(x))));
123128
}
124-
129+
#if defined(SIMD_BGR_TO_LAB_OPENCV_COMPATIBILITY)
130+
if(LabCbrtTab_b[324] == 17746)
131+
LabCbrtTab_b[324] = 17745;
132+
if (LabCbrtTab_b[49] == 9455)
133+
LabCbrtTab_b[49] = 9454;
134+
#endif
125135
initialized = true;
126136
}
127137
}
@@ -175,6 +185,27 @@ namespace Simd
175185
int a = CV_DESCALE(500 * (fX - fY) + 128 * (1 << lab_shift2), lab_shift2);
176186
int b = CV_DESCALE(200 * (fY - fZ) + 128 * (1 << lab_shift2), lab_shift2);
177187

188+
#if defined(_WIN32) && 0
189+
if (L == 45 && a == 165)
190+
{
191+
std::cout << i << " old Lab = {" << L << ", " << a << ", " << b << "}";
192+
193+
int R = tab[src[0]], G = tab[src[1]], B = tab[src[2]];
194+
int fX = LabCbrtTab_b[CV_DESCALE(R * C0 + G * C1 + B * C2, lab_shift)];
195+
int fY = LabCbrtTab_b[CV_DESCALE(R * C3 + G * C4 + B * C5, lab_shift)] - 1;
196+
int fZ = LabCbrtTab_b[CV_DESCALE(R * C6 + G * C7 + B * C8, lab_shift)];
197+
198+
int L = CV_DESCALE(Lscale * fY + Lshift, lab_shift2);
199+
int a = CV_DESCALE(500 * (fX - fY) + 128 * (1 << lab_shift2), lab_shift2);
200+
int b = CV_DESCALE(200 * (fY - fZ) + 128 * (1 << lab_shift2), lab_shift2);
201+
202+
std::cout << " new Lab = {" << L << ", " << a << ", " << b << "}";
203+
std::cout << " index " << CV_DESCALE(R * C3 + G * C4 + B * C5, lab_shift);
204+
std::cout << " value " << LabCbrtTab_b[CV_DESCALE(R * C3 + G * C4 + B * C5, lab_shift)];
205+
std::cout << std::endl;
206+
exit(0);
207+
}
208+
#endif
178209
dst[0] = Base::RestrictRange(L);
179210
dst[1] = Base::RestrictRange(a);
180211
dst[2] = Base::RestrictRange(b);

src/Simd/SimdLib.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1159,7 +1159,7 @@ namespace Simd
11591159
*/
11601160
template<template<class> class A> SIMD_INLINE void BgrToLab(const View<A>& bgr, View<A>& lab)
11611161
{
1162-
assert(EqualSize(bgr, rgb) && bgr.format == View<A>::Bgr24 && lab.format == View<A>::Lab24);
1162+
assert(EqualSize(bgr, lab) && bgr.format == View<A>::Bgr24 && lab.format == View<A>::Lab24);
11631163

11641164
SimdBgrToLab(bgr.data, bgr.stride, bgr.width, bgr.height, lab.data, lab.stride);
11651165
}

src/Simd/SimdView.hpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1238,7 +1238,17 @@ namespace Simd
12381238
case Int32: return CV_32SC1;
12391239
case Float: return CV_32FC1;
12401240
case Double: return CV_64FC1;
1241+
case BayerGrbg: return CV_8UC1;
1242+
case BayerGbrg: return CV_8UC1;
1243+
case BayerRggb: return CV_8UC1;
1244+
case BayerBggr: return CV_8UC1;
12411245
case Hsv24: return CV_8UC3;
1246+
case Hsl24: return CV_8UC3;
1247+
case Rgb24: return CV_8UC3;
1248+
case Rgba32: return CV_8UC4;
1249+
case Uyvy16: return CV_8UC2;
1250+
case Argb32: return CV_8UC4;
1251+
case Lab24: return CV_8UC3;
12421252
default: assert(0); return 0;
12431253
}
12441254
}

src/Test/Test.cpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,10 @@
3636
#include <windows.h>
3737
#endif
3838

39+
#ifdef SIMD_OPENCV_ENABLE
40+
#include <opencv2/core/core.hpp>
41+
#endif
42+
3943
namespace Test
4044
{
4145
typedef bool(*AutoTestPtr)();
@@ -658,7 +662,7 @@ namespace Test
658662
}
659663
else if (arg.find("-tt=") == 0)
660664
{
661-
TEST_THREADS = Simd::Min(FromString<size_t>(arg.substr(4, arg.size() - 4)), (size_t)std::thread::hardware_concurrency());
665+
TEST_THREADS = Simd::Min<int>(FromString<int>(arg.substr(4, arg.size() - 4)), (size_t)std::thread::hardware_concurrency());
662666
}
663667
else if (arg.find("-tr=") == 0)
664668
{
@@ -975,6 +979,9 @@ int main(int argc, char* argv[])
975979
}
976980

977981
::SimdSetThreadNumber(options.workThreads);
982+
#ifdef SIMD_OPENCV_ENABLE
983+
cv::setNumThreads(options.workThreads);
984+
#endif
978985

979986
switch (options.mode)
980987
{

src/Test/TestAnyToAny.cpp

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -548,21 +548,23 @@ namespace Test
548548
View src(W, H, View::Bgr24, NULL, TEST_ALIGN(W));
549549
FillRandom(src);
550550

551-
View dst1(W, H, View::Bgr24, NULL, TEST_ALIGN(W));
552-
View dst2(W, H, View::Bgr24, NULL, TEST_ALIGN(W));
551+
View dst1(W, H, View::Lab24, NULL, TEST_ALIGN(W));
552+
View dst2(W, H, View::Lab24, NULL, TEST_ALIGN(W));
553553

554554
Simd::Fill(dst1, 1);
555555
Simd::Fill(dst2, 2);
556556

557-
src.data[0] = 89;
558-
src.data[1] = 162;
559-
src.data[2] = 252;
557+
TEST_EXECUTE_AT_LEAST_MIN_TIME(TEST_PERFORMANCE_TEST("OpenCV"); cv::cvtColor((cv::Mat)src, (cv::Mat)(dst1.Ref()), cv::COLOR_BGR2Lab, 3));
560558

561-
TEST_EXECUTE_AT_LEAST_MIN_TIME(cv::cvtColor((cv::Mat)src, (cv::Mat)(dst1.Ref()), cv::COLOR_BGR2Lab, 3));
562-
563-
TEST_EXECUTE_AT_LEAST_MIN_TIME(Simd::BgrToLab(src, dst2));
559+
TEST_EXECUTE_AT_LEAST_MIN_TIME(TEST_PERFORMANCE_TEST("Simd"); Simd::BgrToLab(src, dst2));
564560

565561
result = result && Compare(dst1, dst2, 0, true, 64);
562+
563+
#ifdef TEST_PERFORMANCE_TEST_ENABLE
564+
TEST_LOG_SS(Info, PerformanceMeasurerStorage::s_storage.ConsoleReport(false, true));
565+
PerformanceMeasurerStorage::s_storage.Clear();
566+
#endif
567+
566568
#endif
567569
return result;
568570
}

0 commit comments

Comments
 (0)