diff --git a/mindspore/ccsrc/minddata/dataset/kernels/image/lite_cv/image_process.cc b/mindspore/ccsrc/minddata/dataset/kernels/image/lite_cv/image_process.cc index f66db08cce..6e65db0641 100644 --- a/mindspore/ccsrc/minddata/dataset/kernels/image/lite_cv/image_process.cc +++ b/mindspore/ccsrc/minddata/dataset/kernels/image/lite_cv/image_process.cc @@ -1591,7 +1591,7 @@ bool GetPerspectiveTransform(std::vector src_point, std::vector ds return true; } -bool GetAffineTransformImpl(LiteMat src, LiteMat dst) { +bool GetAffineTransformImpl(LiteMat &src, LiteMat &dst) { int m = src.height_; int n = dst.width_; for (int i = 0; i < m; i++) { @@ -1603,6 +1603,8 @@ bool GetAffineTransformImpl(LiteMat src, LiteMat dst) { } if (std::abs(src.ptr(k)[i]) < DBL_EPSILON * 100) { + double x[6] = {0}; + dst.Init(1, 6, x, LDataType(LDataType::DOUBLE)); return false; } if (k != i) { @@ -1669,7 +1671,7 @@ bool GetAffineTransform(std::vector src_point, std::vector dst_poi } GetAffineTransformImpl(src1, src2); - M.Init(3, 2, LDataType(LDataType::DOUBLE)); + M.Init(3, 2, 1, LDataType(LDataType::DOUBLE)); for (int i = 0; i < M.height_; i++) { for (int j = 0; j < M.width_; j++) { M.ptr(i)[j] = src2.ptr(i * M.width_ + j)[0]; @@ -1942,6 +1944,12 @@ int ImageWarpAffineHWC(imageToolsImage_t image, imageToolsImage_t warped_image, bool ResizePreserveARWithFiller(LiteMat &src, LiteMat &dst, int h, int w, float (*ratioShiftWShiftH)[3], float (*invM)[2][3], int img_orientation) { + if (src.IsEmpty() || src.channel_ != 3 || h <= 0 || w <= 0 || h > 10000 || w > 10000) { + return false; + } + if (ratioShiftWShiftH == nullptr || invM == nullptr) { + return false; + } if (dst.IsEmpty()) { dst.Init(w, h, src.channel_, LDataType::FLOAT32); } diff --git a/tests/ut/cpp/dataset/image_process_test.cc b/tests/ut/cpp/dataset/image_process_test.cc index e414143402..b5e5ecc58a 100644 --- a/tests/ut/cpp/dataset/image_process_test.cc +++ b/tests/ut/cpp/dataset/image_process_test.cc @@ -1847,3 +1847,49 @@ TEST_F(MindDataImageProcess, testResizePreserveARWithFillerv) { cv::Mat dst_image(lite_mat_resize.height_, lite_mat_resize.width_, CV_32FC3, lite_mat_resize.data_ptr_); cv::imwrite("./mindspore_image.jpg", dst_image); } + +TEST_F(MindDataImageProcess, testResizePreserveARWithFillervFail) { + std::string filename = "data/dataset/apple.jpg"; + cv::Mat image = cv::imread(filename, cv::ImreadModes::IMREAD_COLOR); + + // The input lite_mat_rgb object is null. + LiteMat lite_mat_rgb; + LiteMat lite_mat_resize; + float ratioShiftWShiftH[3] = {0}; + float invM[2][3] = {{0, 0, 0}, {0, 0, 0}}; + int h = 1000; + int w = 1000; + bool ret = ResizePreserveARWithFiller(lite_mat_rgb, lite_mat_resize, h, w, &ratioShiftWShiftH, &invM, 0); + ASSERT_TRUE(ret == false); + + // The channel of input lite_mat_rgb object is not 3. + LiteMat lite_mat_rgb1; + lite_mat_rgb1.Init(image.cols, image.rows, 1, image.data, LDataType::UINT8); + LiteMat lite_mat_resize1; + float ratioShiftWShiftH1[3] = {0}; + float invM1[2][3] = {{0, 0, 0}, {0, 0, 0}}; + int h1 = 1000; + int w1 = 1000; + bool ret1 = ResizePreserveARWithFiller(lite_mat_rgb1, lite_mat_resize1, h1, w1, &ratioShiftWShiftH1, &invM1, 0); + ASSERT_TRUE(ret1 == false); + + // The ratioShiftWShiftH2 and invM2 is null. + LiteMat lite_mat_rgb2; + lite_mat_rgb2.Init(image.cols, image.rows, image.channels(), image.data, LDataType::UINT8); + LiteMat lite_mat_resize2; + int h2 = 1000; + int w2 = 1000; + bool ret2 = ResizePreserveARWithFiller(lite_mat_rgb2, lite_mat_resize2, h2, w2, nullptr, nullptr, 0); + ASSERT_TRUE(ret2 == false); + + // The width and height of the output image is less than or equal to 0. + LiteMat lite_mat_rgb3; + lite_mat_rgb3.Init(image.cols, image.rows, image.channels(), image.data, LDataType::UINT8); + LiteMat lite_mat_resize3; + float ratioShiftWShiftH3[3] = {0}; + float invM3[2][3] = {{0, 0, 0}, {0, 0, 0}}; + int h3 = -1000; + int w3 = 1000; + bool ret3 = ResizePreserveARWithFiller(lite_mat_rgb3, lite_mat_resize3, h3, w3, &ratioShiftWShiftH3, &invM3, 0); + ASSERT_TRUE(ret3 == false); +}