Skip to content

Commit 24105f9

Browse files
author
Anna Khakimova
committed
Add BGR2I420 and RGB2I420 kernels to CPU backend
1 parent 2678e31 commit 24105f9

File tree

6 files changed

+127
-1
lines changed

6 files changed

+127
-1
lines changed

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

+43-1
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,24 @@ namespace imgproc {
142142
}
143143
};
144144

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(outSz_w > 0 && outSz_h > 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(outSz_w > 0 && outSz_h > 0);
159+
return in.withType(in.depth, 1).withSize(Size(in.size.width, in.size.height * 3 / 2));
160+
}
161+
};
162+
145163
G_TYPED_KERNEL(GNV12toRGB, <GMat(GMat, GMat)>, "org.opencv.imgproc.colorconvert.nv12torgb") {
146164
static GMatDesc outMeta(GMatDesc in_y, GMatDesc in_uv) {
147165
GAPI_Assert(in_y.chan == 1);
@@ -828,7 +846,7 @@ Output image must be 8-bit unsigned 3-channel image @ref CV_8UC3.
828846
@note Function textual ID is "org.opencv.imgproc.colorconvert.bgr2rgb"
829847
830848
@param src input image: 8-bit unsigned 3-channel image @ref CV_8UC3.
831-
@sa BGR2RGB
849+
@sa RGB2BGR
832850
*/
833851
GAPI_EXPORTS GMat BGR2RGB(const GMat& src);
834852

@@ -891,6 +909,30 @@ Output image must be 8-bit unsigned 3-channel image @ref CV_8UC3.
891909
*/
892910
GAPI_EXPORTS GMat RGB2YUV(const GMat& src);
893911

912+
/** @brief Converts an image from BGR color space to I420 color space.
913+
914+
The function converts an input image from BGR color space to I420.
915+
The conventional ranges for R, G, and B channel values are 0 to 255.
916+
917+
@note Function textual ID is "org.opencv.imgproc.colorconvert.bgr2i420"
918+
919+
@param src input image: 8-bit unsigned 3-channel image @ref CV_8UC3.
920+
@sa I4202BGR
921+
*/
922+
GAPI_EXPORTS GMat BGR2I420(const GMat& src);
923+
924+
/** @brief Converts an image from RGB color space to I420 color space.
925+
926+
The function converts an input image from RGB color space to I420.
927+
The conventional ranges for R, G, and B channel values are 0 to 255.
928+
929+
@note Function textual ID is "org.opencv.imgproc.colorconvert.rgb2i420"
930+
931+
@param src input image: 8-bit unsigned 3-channel image @ref CV_8UC3.
932+
@sa I4202RGB
933+
*/
934+
GAPI_EXPORTS GMat RGB2I420(const GMat& src);
935+
894936
/** @brief Converts an image from BGR color space to LUV color space.
895937
896938
The function converts an input image from BGR color space to LUV.

modules/gapi/src/api/kernels_imgproc.cpp

+10
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,16 @@ GMat YUV2RGB(const GMat& src)
165165
return imgproc::GYUV2RGB::on(src);
166166
}
167167

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+
168178
GMat NV12toRGB(const GMat& src_y, const GMat& src_uv)
169179
{
170180
return imgproc::GNV12toRGB::on(src_y, src_uv);

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

+18
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,22 @@ GAPI_OCV_KERNEL(GCPUBGR2RGB, cv::gapi::imgproc::GBGR2RGB)
219219
}
220220
};
221221

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+
222238
GAPI_OCV_KERNEL(GCPURGB2YUV, cv::gapi::imgproc::GRGB2YUV)
223239
{
224240
static void run(const cv::Mat& in, cv::Mat &out)
@@ -456,6 +472,8 @@ cv::gapi::GKernelPackage cv::gapi::imgproc::cpu::kernels()
456472
, GCPUBGR2RGB
457473
, GCPURGB2YUV
458474
, GCPUYUV2RGB
475+
, GCPUBGR2I420
476+
, GCPURGB2I420
459477
, GCPUNV12toRGB
460478
, GCPUNV12toBGR
461479
, GCPURGB2Lab

modules/gapi/test/common/gapi_imgproc_tests.hpp

+2
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,8 @@ GAPI_TEST_FIXTURE(BGR2RGBTest, initMatrixRandN, FIXTURE_API(CompareMats), 1, cmp
6868
GAPI_TEST_FIXTURE(RGB2GrayTest, initMatrixRandN, FIXTURE_API(CompareMats), 1, cmpF)
6969
GAPI_TEST_FIXTURE(BGR2GrayTest, initMatrixRandN, FIXTURE_API(CompareMats), 1, cmpF)
7070
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)
7173
GAPI_TEST_FIXTURE(YUV2RGBTest, initMatrixRandN, FIXTURE_API(CompareMats), 1, cmpF)
7274
GAPI_TEST_FIXTURE(YUV2GrayTest, initMatrixRandN, FIXTURE_API(CompareMats), 1, cmpF)
7375
GAPI_TEST_FIXTURE(NV12toRGBTest, initMatrixRandN, FIXTURE_API(CompareMats), 1, cmpF)

modules/gapi/test/common/gapi_imgproc_tests_inl.hpp

+38
Original file line numberDiff line numberDiff line change
@@ -542,6 +542,44 @@ TEST_P(YUV2RGBTest, AccuracyTest)
542542
}
543543
}
544544

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(), szSize(sz.width, sz.height * 3 / 2));
580+
}
581+
}
582+
545583
TEST_P(NV12toRGBTest, AccuracyTest)
546584
{
547585
// G-API code //////////////////////////////////////////////////////////////

modules/gapi/test/cpu/gapi_imgproc_tests_cpu.cpp

+16
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,22 @@ INSTANTIATE_TEST_CASE_P(YUV2RGBTestCPU, YUV2RGBTest,
281281
Values(IMGPROC_CPU),
282282
Values(AbsExact().to_compare_obj())));
283283

284+
INSTANTIATE_TEST_CASE_P(BGR2I420TestCPU, BGR2I420Test,
285+
Combine(Values(CV_8UC3),
286+
Values(cv::Size(1280, 720),
287+
cv::Size(640, 480)),
288+
Values(CV_8UC1),
289+
Values(IMGPROC_CPU),
290+
Values(AbsExact().to_compare_obj())));
291+
292+
INSTANTIATE_TEST_CASE_P(RGB2I420TestCPU, RGB2I420Test,
293+
Combine(Values(CV_8UC3),
294+
Values(cv::Size(1280, 720),
295+
cv::Size(640, 480)),
296+
Values(CV_8UC1),
297+
Values(IMGPROC_CPU),
298+
Values(AbsExact().to_compare_obj())));
299+
284300
INSTANTIATE_TEST_CASE_P(NV12toRGBTestCPU, NV12toRGBTest,
285301
Combine(Values(CV_8UC1),
286302
Values(cv::Size(1280, 720),

0 commit comments

Comments
 (0)