Unverified Commit 8fe96743 authored by Anatoliy Talamanov's avatar Anatoliy Talamanov Committed by GitHub

Merge pull request #16768 from TolyaTalamanov:at/add-warp-affine

G-API: Implement WarpAffine

* Add WarpAffine

* Ban BORDER_TRANSPARENT

* Fix doc
parent 2160f9b2
......@@ -485,6 +485,14 @@ namespace core {
return (ddepth < 0 ? in : in.withDepth(ddepth));
}
};
G_TYPED_KERNEL(GWarpAffine, <GMat(GMat, const Mat&, Size, int, int, const cv::Scalar&)>, "org.opencv.core.warpAffine") {
static GMatDesc outMeta(GMatDesc in, const Mat&, Size dsize, int, int border_mode, const cv::Scalar&) {
GAPI_Assert(border_mode != cv::BORDER_TRANSPARENT &&
"cv::BORDER_TRANSPARENT mode isn't support in G-API");
return in.withType(in.depth, in.chan).withSize(dsize);
}
};
}
//! @addtogroup gapi_math
......@@ -1653,6 +1661,31 @@ number of channels as src and the depth =ddepth.
*/
GAPI_EXPORTS GMat normalize(const GMat& src, double alpha, double beta,
int norm_type, int ddepth = -1);
/** @brief Applies an affine transformation to an image.
The function warpAffine transforms the source image using the specified matrix:
\f[\texttt{dst} (x,y) = \texttt{src} ( \texttt{M} _{11} x + \texttt{M} _{12} y + \texttt{M} _{13}, \texttt{M} _{21} x + \texttt{M} _{22} y + \texttt{M} _{23})\f]
when the flag #WARP_INVERSE_MAP is set. Otherwise, the transformation is first inverted
with #invertAffineTransform and then put in the formula above instead of M. The function cannot
operate in-place.
@param src input image.
@param M \f$2\times 3\f$ transformation matrix.
@param dsize size of the output image.
@param flags combination of interpolation methods (see #InterpolationFlags) and the optional
flag #WARP_INVERSE_MAP that means that M is the inverse transformation (
\f$\texttt{dst}\rightarrow\texttt{src}\f$ ).
@param borderMode pixel extrapolation method (see #BorderTypes);
borderMode=#BORDER_TRANSPARENT isn't supported
@param borderValue value used in case of a constant border; by default, it is 0.
@sa warpPerspective, resize, remap, getRectSubPix, transform
*/
GAPI_EXPORTS GMat warpAffine(const GMat& src, const Mat& M, const Size& dsize, int flags = cv::INTER_LINEAR,
int borderMode = cv::BORDER_CONSTANT, const Scalar& borderValue = Scalar());
//! @} gapi_transform
} //namespace gapi
......
......@@ -371,5 +371,11 @@ GMat normalize(const GMat& _src, double a, double b,
return core::GNormalize::on(_src, a, b, norm_type, ddepth);
}
GMat warpAffine(const GMat& src, const Mat& M, const Size& dsize, int flags,
int borderMode, const Scalar& borderValue)
{
return core::GWarpAffine::on(src, M, dsize, flags, borderMode, borderValue);
}
} //namespace gapi
} //namespace cv
......@@ -558,6 +558,16 @@ GAPI_OCV_KERNEL(GCPUNormalize, cv::gapi::core::GNormalize)
}
};
GAPI_OCV_KERNEL(GCPUWarpAffine, cv::gapi::core::GWarpAffine)
{
static void run(const cv::Mat& src, const cv::Mat& M, const cv::Size& dsize,
int flags, int borderMode, const cv::Scalar& borderValue, cv::Mat& out)
{
cv::warpAffine(src, out, M, dsize, flags, borderMode, borderValue);
}
};
cv::gapi::GKernelPackage cv::gapi::core::cpu::kernels()
{
static auto pkg = cv::gapi::kernels
......@@ -626,6 +636,7 @@ cv::gapi::GKernelPackage cv::gapi::core::cpu::kernels()
, GCPUConvertTo
, GCPUSqrt
, GCPUNormalize
, GCPUWarpAffine
>();
return pkg;
}
......@@ -141,6 +141,9 @@ struct BackendOutputAllocationTest : TestWithParamBase<>
// FIXME: move all tests from this fixture to the base class once all issues are resolved
struct BackendOutputAllocationLargeSizeWithCorrectSubmatrixTest : BackendOutputAllocationTest {};
GAPI_TEST_FIXTURE(ReInitOutTest, initNothing, <cv::Size>, 1, out_sz)
GAPI_TEST_FIXTURE(WarpAffineTest, initMatrixRandU, FIXTURE_API(CompareMats, double , double, int, int, cv::Scalar), 6,
cmpF, angle, scale, flags, border_mode, border_value)
} // opencv_test
#endif //OPENCV_GAPI_CORE_TESTS_HPP
......@@ -1266,6 +1266,27 @@ TEST_P(SqrtTest, AccuracyTest)
}
}
TEST_P(WarpAffineTest, AccuracyTest)
{
cv::Point center{in_mat1.size() / 2};
cv::Mat warp_mat = cv::getRotationMatrix2D(center, angle, scale);
// G-API code //////////////////////////////////////////////////////////////
cv::GMat in;
auto out = cv::gapi::warpAffine(in, warp_mat, in_mat1.size(), flags, border_mode, border_value);
cv::GComputation c(in, out);
c.apply(in_mat1, out_mat_gapi, getCompileArgs());
// OpenCV code /////////////////////////////////////////////////////////////
cv::warpAffine(in_mat1, out_mat_ocv, warp_mat, in_mat1.size(), flags, border_mode, border_value);
// Comparison //////////////////////////////////////////////////////////////
{
EXPECT_TRUE(cmpF(out_mat_gapi, out_mat_ocv));
}
}
TEST_P(NormalizeTest, Test)
{
initMatrixRandN(type, sz, CV_MAKETYPE(ddepth, CV_MAT_CN(type)));
......
......@@ -435,6 +435,19 @@ INSTANTIATE_TEST_CASE_P(ConcatHorVecTestCPU, ConcatHorVecTest,
Values(-1),
Values(CORE_CPU)));
INSTANTIATE_TEST_CASE_P(WarpAffineTestCPU, WarpAffineTest,
Combine(Values(CV_8UC1, CV_8UC3),
Values(cv::Size(1280, 720),
cv::Size(640, 480)),
Values(-1),
Values(CORE_CPU),
Values(AbsExact().to_compare_obj()),
Values(-50.0, 90.0),
Values(0.6),
Values(cv::INTER_LINEAR),
Values(cv::BORDER_CONSTANT),
Values(cv::Scalar())));
INSTANTIATE_TEST_CASE_P(NormalizeTestCPU, NormalizeTest,
Combine(Values(CV_8UC1, CV_8UC3, CV_16UC1, CV_16SC1, CV_32FC1),
Values(cv::Size(1280, 720),
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment