!12493 Added GetAffineTransform operator

From: @shenwei41
Reviewed-by: 
Signed-off-by:
This commit is contained in:
mindspore-ci-bot 2021-02-23 09:54:04 +08:00 committed by Gitee
commit 086374f306
4 changed files with 106 additions and 8 deletions

View File

@ -1433,5 +1433,89 @@ bool GetPerspectiveTransform(std::vector<Point> src_point, std::vector<Point> ds
return true;
}
bool GetAffineTransformImpl(LiteMat src, LiteMat dst) {
int m = src.height_;
int n = dst.width_;
for (int i = 0; i < m; i++) {
int k = i;
for (int j = i + 1; j < m; j++) {
if (std::abs(src.ptr<double>(j)[i]) > std::abs(src.ptr<double>(k)[i])) {
k = j;
}
}
if (std::abs(src.ptr<double>(k)[i]) < DBL_EPSILON * 100) {
return false;
}
if (k != i) {
for (int j = i; j < m; j++) {
std::swap(src.ptr<double>(i)[j], src.ptr<double>(k)[j]);
}
if (dst.data_ptr_) {
for (int j = 0; j < n; j++) {
std::swap(dst.ptr<double>(i)[j], dst.ptr<double>(k)[j]);
}
}
}
double d = -1 / src.ptr<double>(i)[i];
for (int j = i + 1; j < m; j++) {
double alpha = src.ptr<double>(j)[i] * d;
for (k = i + 1; k < m; k++) {
src.ptr<double>(j)[k] += alpha * src.ptr<double>(i)[k];
}
if (dst.data_ptr_) {
for (k = 0; k < n; k++) {
dst.ptr<double>(j)[k] += alpha * dst.ptr<double>(i)[k];
}
}
}
}
if (dst.data_ptr_) {
for (int i = m - 1; i >= 0; i--) {
for (int j = 0; j < n; j++) {
double s = dst.ptr<double>(i)[j];
for (int k = i + 1; k < m; k++) {
s -= src.ptr<double>(i)[k] * dst.ptr<double>(k)[j];
}
dst.ptr<double>(i)[j] = s / src.ptr<double>(i)[i];
}
}
}
return true;
}
bool GetAffineTransform(std::vector<Point> src_point, std::vector<Point> dst_point, LiteMat &M) {
double m[6 * 6];
double n[6];
LiteMat src1(6, 6, m, LDataType(LDataType::DOUBLE));
LiteMat src2(1, 6, n, LDataType(LDataType::DOUBLE));
for (int i = 0; i < 3; i++) {
int j = i * 12;
int k = i * 12 + 6;
m[j] = m[k + 3] = src_point[i].x;
m[j + 1] = m[k + 4] = src_point[i].y;
m[j + 2] = m[k + 5] = 1;
m[j + 3] = m[j + 4] = m[j + 5] = 0;
m[k] = m[k + 1] = m[k + 2] = 0;
n[i * 2] = dst_point[i].x;
n[i * 2 + 1] = dst_point[i].y;
}
GetAffineTransformImpl(src1, src2);
M.Init(3, 2, LDataType(LDataType::DOUBLE));
for (int i = 0; i < M.height_; i++) {
for (int j = 0; j < M.width_; j++) {
M.ptr<double>(i)[j] = src2.ptr<double>(i * M.width_ + j)[0];
}
}
return true;
}
} // namespace dataset
} // namespace mindspore

View File

@ -107,6 +107,9 @@ bool GetRotationMatrix2D(float x, float y, double angle, double scale, LiteMat &
/// \brief Perspective transformation
bool GetPerspectiveTransform(std::vector<Point> src_point, std::vector<Point> dst_point, LiteMat &M);
/// \brief Affine transformation
bool GetAffineTransform(std::vector<Point> src_point, std::vector<Point> dst_point, LiteMat &M);
/// \brief Matrix transpose
bool Transpose(LiteMat &src, LiteMat &dst);

View File

@ -697,7 +697,7 @@ class Dataset:
python_multiprocessing, cache, callbacks)
@check_filter
def filter(self, predicate, input_columns=None, num_parallel_workers=1):
def filter(self, predicate, input_columns=None, num_parallel_workers=None):
"""
Filter dataset by predicate.

View File

@ -1241,9 +1241,8 @@ TEST_F(MindDataImageProcess, testWarpPerspectiveGrayResize) {
}
TEST_F(MindDataImageProcess, testGetRotationMatrix2D) {
std::vector<std::vector<double>> expect_matrix = {{0.250000, 0.433013, -0.116025},
{-0.433013, 0.250000, 1.933013}};
std::vector<std::vector<double>> expect_matrix = {{0.250000, 0.433013, -0.116025}, {-0.433013, 0.250000, 1.933013}};
double angle = 60.0;
double scale = 0.5;
@ -1255,10 +1254,9 @@ TEST_F(MindDataImageProcess, testGetRotationMatrix2D) {
}
TEST_F(MindDataImageProcess, testGetPerspectiveTransform) {
std::vector<std::vector<double>> expect_matrix = {{1.272113, 3.665216, -788.484287},
{-0.394146, 3.228247, -134.009780},
{-0.001460, 0.006414, 1}};
std::vector<std::vector<double>> expect_matrix = {
{1.272113, 3.665216, -788.484287}, {-0.394146, 3.228247, -134.009780}, {-0.001460, 0.006414, 1}};
std::vector<Point> src = {Point(165, 270), Point(835, 270), Point(360, 125), Point(615, 125)};
std::vector<Point> dst = {Point(165, 270), Point(835, 270), Point(100, 100), Point(500, 30)};
@ -1268,3 +1266,16 @@ TEST_F(MindDataImageProcess, testGetPerspectiveTransform) {
EXPECT_TRUE(ret);
AccuracyComparison(expect_matrix, M);
}
TEST_F(MindDataImageProcess, testGetAffineTransform) {
std::vector<std::vector<double>> expect_matrix = {{0.400000, 0.066667, 16.666667}, {0.000000, 0.333333, 23.333333}};
std::vector<Point> src = {Point(50, 50), Point(200, 50), Point(50, 200)};
std::vector<Point> dst = {Point(40, 40), Point(100, 40), Point(50, 90)};
LiteMat M;
bool ret = false;
ret = GetAffineTransform(src, dst, M);
EXPECT_TRUE(ret);
AccuracyComparison(expect_matrix, M);
}