Skip to content

Commit 0287fa2

Browse files
Anna KhakimovaSajjad Ali
Anna Khakimova
authored and
Sajjad Ali
committed
Merge pull request opencv#18516 from anna-khakimova:ak/bgr2rgb
GAPI: Addition new Color conversion kernels to CPU backend. * Add BGR2RGB kernel to CPU backend * Add BGR2I420 and RGB2I420 kernels to CPU backend * Add I4202BGR and I4202RGB kernels to CPU backend
1 parent 49582e1 commit 0287fa2

File tree

6 files changed

+329
-0
lines changed

6 files changed

+329
-0
lines changed

modules/gapi/include/opencv2/gapi/imgproc.hpp

+120
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,12 @@ namespace imgproc {
124124
}
125125
};
126126

127+
G_TYPED_KERNEL(GBGR2RGB, <GMat(GMat)>, "org.opencv.imgproc.colorconvert.bgr2rgb") {
128+
static GMatDesc outMeta(GMatDesc in) {
129+
return in; // type still remains CV_8UC3;
130+
}
131+
};
132+
127133
G_TYPED_KERNEL(GRGB2YUV, <GMat(GMat)>, "org.opencv.imgproc.colorconvert.rgb2yuv") {
128134
static GMatDesc outMeta(GMatDesc in) {
129135
return in; // type still remains CV_8UC3;
@@ -136,6 +142,42 @@ namespace imgproc {
136142
}
137143
};
138144

145+
G_TYPED_KERNEL(GBGR2I420, <GMat(GMat)>, "org.opencv.imgproc.colorconvert.bgr2i420") {
146+
static GMatDesc outMeta(GMatDesc in) {
147+
GAPI_Assert(in.depth == CV_8U);
148+
GAPI_Assert(in.chan == 3);
149+
GAPI_Assert(in.size.height % 2 == 0);
150+
return in.withType(in.depth, 1).withSize(Size(in.size.width, in.size.height * 3 / 2));
151+
}
152+
};
153+
154+
G_TYPED_KERNEL(GRGB2I420, <GMat(GMat)>, "org.opencv.imgproc.colorconvert.rgb2i420") {
155+
static GMatDesc outMeta(GMatDesc in) {
156+
GAPI_Assert(in.depth == CV_8U);
157+
GAPI_Assert(in.chan == 3);
158+
GAPI_Assert(in.size.height % 2 == 0);
159+
return in.withType(in.depth, 1).withSize(Size(in.size.width, in.size.height * 3 / 2));
160+
}
161+
};
162+
163+
G_TYPED_KERNEL(GI4202BGR, <GMat(GMat)>, "org.opencv.imgproc.colorconvert.i4202bgr") {
164+
static GMatDesc outMeta(GMatDesc in) {
165+
GAPI_Assert(in.depth == CV_8U);
166+
GAPI_Assert(in.chan == 1);
167+
GAPI_Assert(in.size.height % 3 == 0);
168+
return in.withType(in.depth, 3).withSize(Size(in.size.width, in.size.height * 2 / 3));
169+
}
170+
};
171+
172+
G_TYPED_KERNEL(GI4202RGB, <GMat(GMat)>, "org.opencv.imgproc.colorconvert.i4202rgb") {
173+
static GMatDesc outMeta(GMatDesc in) {
174+
GAPI_Assert(in.depth == CV_8U);
175+
GAPI_Assert(in.chan == 1);
176+
GAPI_Assert(in.size.height % 3 == 0);
177+
return in.withType(in.depth, 3).withSize(Size(in.size.width, in.size.height * 2 / 3));
178+
}
179+
};
180+
139181
G_TYPED_KERNEL(GNV12toRGB, <GMat(GMat, GMat)>, "org.opencv.imgproc.colorconvert.nv12torgb") {
140182
static GMatDesc outMeta(GMatDesc in_y, GMatDesc in_uv) {
141183
GAPI_Assert(in_y.chan == 1);
@@ -812,6 +854,20 @@ The algorithm normalizes the brightness and increases the contrast of the image.
812854
*/
813855
GAPI_EXPORTS GMat equalizeHist(const GMat& src);
814856

857+
/** @brief Converts an image from BGR color space to RGB color space.
858+
859+
The function converts an input image from BGR color space to RGB.
860+
The conventional ranges for B, G, and R channel values are 0 to 255.
861+
862+
Output image is 8-bit unsigned 3-channel image @ref CV_8UC3.
863+
864+
@note Function textual ID is "org.opencv.imgproc.colorconvert.bgr2rgb"
865+
866+
@param src input image: 8-bit unsigned 3-channel image @ref CV_8UC3.
867+
@sa RGB2BGR
868+
*/
869+
GAPI_EXPORTS GMat BGR2RGB(const GMat& src);
870+
815871
//! @} gapi_filters
816872

817873
//! @addtogroup gapi_colorconvert
@@ -871,6 +927,70 @@ Output image must be 8-bit unsigned 3-channel image @ref CV_8UC3.
871927
*/
872928
GAPI_EXPORTS GMat RGB2YUV(const GMat& src);
873929

930+
/** @brief Converts an image from BGR color space to I420 color space.
931+
932+
The function converts an input image from BGR color space to I420.
933+
The conventional ranges for R, G, and B channel values are 0 to 255.
934+
935+
Output image must be 8-bit unsigned 1-channel image. @ref CV_8UC1.
936+
Width of I420 output image must be the same as width of input image.
937+
Height of I420 output image must be equal 3/2 from height of input image.
938+
939+
@note Function textual ID is "org.opencv.imgproc.colorconvert.bgr2i420"
940+
941+
@param src input image: 8-bit unsigned 3-channel image @ref CV_8UC3.
942+
@sa I4202BGR
943+
*/
944+
GAPI_EXPORTS GMat BGR2I420(const GMat& src);
945+
946+
/** @brief Converts an image from RGB color space to I420 color space.
947+
948+
The function converts an input image from RGB color space to I420.
949+
The conventional ranges for R, G, and B channel values are 0 to 255.
950+
951+
Output image must be 8-bit unsigned 1-channel image. @ref CV_8UC1.
952+
Width of I420 output image must be the same as width of input image.
953+
Height of I420 output image must be equal 3/2 from height of input image.
954+
955+
@note Function textual ID is "org.opencv.imgproc.colorconvert.rgb2i420"
956+
957+
@param src input image: 8-bit unsigned 3-channel image @ref CV_8UC3.
958+
@sa I4202RGB
959+
*/
960+
GAPI_EXPORTS GMat RGB2I420(const GMat& src);
961+
962+
/** @brief Converts an image from I420 color space to BGR color space.
963+
964+
The function converts an input image from I420 color space to BGR.
965+
The conventional ranges for B, G, and R channel values are 0 to 255.
966+
967+
Output image must be 8-bit unsigned 3-channel image. @ref CV_8UC3.
968+
Width of BGR output image must be the same as width of input image.
969+
Height of BGR output image must be equal 2/3 from height of input image.
970+
971+
@note Function textual ID is "org.opencv.imgproc.colorconvert.i4202bgr"
972+
973+
@param src input image: 8-bit unsigned 1-channel image @ref CV_8UC1.
974+
@sa BGR2I420
975+
*/
976+
GAPI_EXPORTS GMat I4202BGR(const GMat& src);
977+
978+
/** @brief Converts an image from I420 color space to BGR color space.
979+
980+
The function converts an input image from I420 color space to BGR.
981+
The conventional ranges for B, G, and R channel values are 0 to 255.
982+
983+
Output image must be 8-bit unsigned 3-channel image. @ref CV_8UC3.
984+
Width of RGB output image must be the same as width of input image.
985+
Height of RGB output image must be equal 2/3 from height of input image.
986+
987+
@note Function textual ID is "org.opencv.imgproc.colorconvert.i4202rgb"
988+
989+
@param src input image: 8-bit unsigned 1-channel image @ref CV_8UC1.
990+
@sa RGB2I420
991+
*/
992+
GAPI_EXPORTS GMat I4202RGB(const GMat& src);
993+
874994
/** @brief Converts an image from BGR color space to LUV color space.
875995
876996
The function converts an input image from BGR color space to LUV.

modules/gapi/src/api/kernels_imgproc.cpp

+25
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,11 @@ cv::GArray<cv::Point2f> goodFeaturesToTrack(const GMat& image, int maxCorners, d
115115
useHarrisDetector, k);
116116
}
117117

118+
GMat BGR2RGB(const GMat& src)
119+
{
120+
return imgproc::GBGR2RGB::on(src);
121+
}
122+
118123
GMat RGB2Gray(const GMat& src)
119124
{
120125
return imgproc::GRGB2Gray::on(src);
@@ -160,6 +165,26 @@ GMat YUV2RGB(const GMat& src)
160165
return imgproc::GYUV2RGB::on(src);
161166
}
162167

168+
GMat BGR2I420(const GMat& src)
169+
{
170+
return imgproc::GBGR2I420::on(src);
171+
}
172+
173+
GMat RGB2I420(const GMat& src)
174+
{
175+
return imgproc::GRGB2I420::on(src);
176+
}
177+
178+
GMat I4202BGR(const GMat& src)
179+
{
180+
return imgproc::GI4202BGR::on(src);
181+
}
182+
183+
GMat I4202RGB(const GMat& src)
184+
{
185+
return imgproc::GI4202RGB::on(src);
186+
}
187+
163188
GMat NV12toRGB(const GMat& src_y, const GMat& src_uv)
164189
{
165190
return imgproc::GNV12toRGB::on(src_y, src_uv);

modules/gapi/src/backends/cpu/gcpuimgproc.cpp

+45
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,46 @@ GAPI_OCV_KERNEL(GCPUGoodFeatures, cv::gapi::imgproc::GGoodFeatures)
211211
}
212212
};
213213

214+
GAPI_OCV_KERNEL(GCPUBGR2RGB, cv::gapi::imgproc::GBGR2RGB)
215+
{
216+
static void run(const cv::Mat& in, cv::Mat &out)
217+
{
218+
cv::cvtColor(in, out, cv::COLOR_BGR2RGB);
219+
}
220+
};
221+
222+
GAPI_OCV_KERNEL(GCPUBGR2I420, cv::gapi::imgproc::GBGR2I420)
223+
{
224+
static void run(const cv::Mat& in, cv::Mat &out)
225+
{
226+
cv::cvtColor(in, out, cv::COLOR_BGR2YUV_I420);
227+
}
228+
};
229+
230+
GAPI_OCV_KERNEL(GCPURGB2I420, cv::gapi::imgproc::GRGB2I420)
231+
{
232+
static void run(const cv::Mat& in, cv::Mat &out)
233+
{
234+
cv::cvtColor(in, out, cv::COLOR_RGB2YUV_I420);
235+
}
236+
};
237+
238+
GAPI_OCV_KERNEL(GCPUI4202BGR, cv::gapi::imgproc::GI4202BGR)
239+
{
240+
static void run(const cv::Mat& in, cv::Mat &out)
241+
{
242+
cv::cvtColor(in, out, cv::COLOR_YUV2BGR_I420);
243+
}
244+
};
245+
246+
GAPI_OCV_KERNEL(GCPUI4202RGB, cv::gapi::imgproc::GI4202RGB)
247+
{
248+
static void run(const cv::Mat& in, cv::Mat &out)
249+
{
250+
cv::cvtColor(in, out, cv::COLOR_YUV2RGB_I420);
251+
}
252+
};
253+
214254
GAPI_OCV_KERNEL(GCPURGB2YUV, cv::gapi::imgproc::GRGB2YUV)
215255
{
216256
static void run(const cv::Mat& in, cv::Mat &out)
@@ -445,8 +485,13 @@ cv::gapi::GKernelPackage cv::gapi::imgproc::cpu::kernels()
445485
, GCPUCanny
446486
, GCPUGoodFeatures
447487
, GCPUEqualizeHist
488+
, GCPUBGR2RGB
448489
, GCPURGB2YUV
449490
, GCPUYUV2RGB
491+
, GCPUBGR2I420
492+
, GCPURGB2I420
493+
, GCPUI4202BGR
494+
, GCPUI4202RGB
450495
, GCPUNV12toRGB
451496
, GCPUNV12toBGR
452497
, GCPURGB2Lab

modules/gapi/test/common/gapi_imgproc_tests.hpp

+5
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,14 @@ GAPI_TEST_FIXTURE_SPEC_PARAMS(GoodFeaturesTest,
6464
double,int,bool),
6565
8, cmpF, fileName, type, maxCorners, qualityLevel, minDistance,
6666
blockSize, useHarrisDetector)
67+
GAPI_TEST_FIXTURE(BGR2RGBTest, initMatrixRandN, FIXTURE_API(CompareMats), 1, cmpF)
6768
GAPI_TEST_FIXTURE(RGB2GrayTest, initMatrixRandN, FIXTURE_API(CompareMats), 1, cmpF)
6869
GAPI_TEST_FIXTURE(BGR2GrayTest, initMatrixRandN, FIXTURE_API(CompareMats), 1, cmpF)
6970
GAPI_TEST_FIXTURE(RGB2YUVTest, initMatrixRandN, FIXTURE_API(CompareMats), 1, cmpF)
71+
GAPI_TEST_FIXTURE(BGR2I420Test, initMatrixRandN, FIXTURE_API(CompareMats), 1, cmpF)
72+
GAPI_TEST_FIXTURE(RGB2I420Test, initMatrixRandN, FIXTURE_API(CompareMats), 1, cmpF)
73+
GAPI_TEST_FIXTURE(I4202BGRTest, initMatrixRandN, FIXTURE_API(CompareMats), 1, cmpF)
74+
GAPI_TEST_FIXTURE(I4202RGBTest, initMatrixRandN, FIXTURE_API(CompareMats), 1, cmpF)
7075
GAPI_TEST_FIXTURE(YUV2RGBTest, initMatrixRandN, FIXTURE_API(CompareMats), 1, cmpF)
7176
GAPI_TEST_FIXTURE(YUV2GrayTest, initMatrixRandN, FIXTURE_API(CompareMats), 1, cmpF)
7277
GAPI_TEST_FIXTURE(NV12toRGBTest, initMatrixRandN, FIXTURE_API(CompareMats), 1, cmpF)

modules/gapi/test/common/gapi_imgproc_tests_inl.hpp

+95
Original file line numberDiff line numberDiff line change
@@ -447,6 +447,25 @@ TEST_P(GoodFeaturesTest, AccuracyTest)
447447
}
448448
}
449449

450+
TEST_P(BGR2RGBTest, AccuracyTest)
451+
{
452+
// G-API code //////////////////////////////////////////////////////////////
453+
cv::GMat in;
454+
auto out = cv::gapi::BGR2RGB(in);
455+
456+
cv::GComputation c(in, out);
457+
c.apply(in_mat1, out_mat_gapi, getCompileArgs());
458+
// OpenCV code /////////////////////////////////////////////////////////////
459+
{
460+
cv::cvtColor(in_mat1, out_mat_ocv, cv::COLOR_BGR2RGB);
461+
}
462+
// Comparison //////////////////////////////////////////////////////////////
463+
{
464+
EXPECT_TRUE(cmpF(out_mat_gapi, out_mat_ocv));
465+
EXPECT_EQ(out_mat_gapi.size(), sz);
466+
}
467+
}
468+
450469
TEST_P(RGB2GrayTest, AccuracyTest)
451470
{
452471
// G-API code //////////////////////////////////////////////////////////////
@@ -523,6 +542,82 @@ TEST_P(YUV2RGBTest, AccuracyTest)
523542
}
524543
}
525544

545+
TEST_P(BGR2I420Test, AccuracyTest)
546+
{
547+
// G-API code //////////////////////////////////////////////////////////////
548+
cv::GMat in;
549+
auto out = cv::gapi::BGR2I420(in);
550+
551+
cv::GComputation c(in, out);
552+
c.apply(in_mat1, out_mat_gapi, getCompileArgs());
553+
// OpenCV code /////////////////////////////////////////////////////////////
554+
{
555+
cv::cvtColor(in_mat1, out_mat_ocv, cv::COLOR_BGR2YUV_I420);
556+
}
557+
// Comparison //////////////////////////////////////////////////////////////
558+
{
559+
EXPECT_TRUE(cmpF(out_mat_gapi, out_mat_ocv));
560+
EXPECT_EQ(out_mat_gapi.size(), Size(sz.width, sz.height * 3 / 2));
561+
}
562+
}
563+
564+
TEST_P(RGB2I420Test, AccuracyTest)
565+
{
566+
// G-API code //////////////////////////////////////////////////////////////
567+
cv::GMat in;
568+
auto out = cv::gapi::RGB2I420(in);
569+
570+
cv::GComputation c(in, out);
571+
c.apply(in_mat1, out_mat_gapi, getCompileArgs());
572+
// OpenCV code /////////////////////////////////////////////////////////////
573+
{
574+
cv::cvtColor(in_mat1, out_mat_ocv, cv::COLOR_RGB2YUV_I420);
575+
}
576+
// Comparison //////////////////////////////////////////////////////////////
577+
{
578+
EXPECT_TRUE(cmpF(out_mat_gapi, out_mat_ocv));
579+
EXPECT_EQ(out_mat_gapi.size(), Size(sz.width, sz.height * 3 / 2));
580+
}
581+
}
582+
583+
TEST_P(I4202BGRTest, AccuracyTest)
584+
{
585+
// G-API code //////////////////////////////////////////////////////////////
586+
cv::GMat in;
587+
auto out = cv::gapi::I4202BGR(in);
588+
589+
cv::GComputation c(in, out);
590+
c.apply(in_mat1, out_mat_gapi, getCompileArgs());
591+
// OpenCV code /////////////////////////////////////////////////////////////
592+
{
593+
cv::cvtColor(in_mat1, out_mat_ocv, cv::COLOR_YUV2BGR_I420);
594+
}
595+
// Comparison //////////////////////////////////////////////////////////////
596+
{
597+
EXPECT_TRUE(cmpF(out_mat_gapi, out_mat_ocv));
598+
EXPECT_EQ(out_mat_gapi.size(), Size(sz.width, sz.height * 2 / 3));
599+
}
600+
}
601+
602+
TEST_P(I4202RGBTest, AccuracyTest)
603+
{
604+
// G-API code //////////////////////////////////////////////////////////////
605+
cv::GMat in;
606+
auto out = cv::gapi::I4202RGB(in);
607+
608+
cv::GComputation c(in, out);
609+
c.apply(in_mat1, out_mat_gapi, getCompileArgs());
610+
// OpenCV code /////////////////////////////////////////////////////////////
611+
{
612+
cv::cvtColor(in_mat1, out_mat_ocv, cv::COLOR_YUV2RGB_I420);
613+
}
614+
// Comparison //////////////////////////////////////////////////////////////
615+
{
616+
EXPECT_TRUE(cmpF(out_mat_gapi, out_mat_ocv));
617+
EXPECT_EQ(out_mat_gapi.size(), Size(sz.width, sz.height * 2 / 3));
618+
}
619+
}
620+
526621
TEST_P(NV12toRGBTest, AccuracyTest)
527622
{
528623
// G-API code //////////////////////////////////////////////////////////////

0 commit comments

Comments
 (0)