!13575 Add check to RgbToGray

From: @shenwei41
Reviewed-by: @tiancixiao,@liucunwei
Signed-off-by: @liucunwei
pull/13575/MERGE
mindspore-ci-bot 4 years ago committed by Gitee
commit 06892b83ff

@ -1680,9 +1680,18 @@ bool GetAffineTransform(std::vector<Point> src_point, std::vector<Point> dst_poi
bool ConvertRgbToGray(const LiteMat &src, LDataType data_type, int w, int h, LiteMat &mat) {
if (data_type == LDataType::UINT8) {
if (src.IsEmpty()) {
return false;
}
if (mat.IsEmpty()) {
mat.Init(w, h, 1, LDataType::UINT8);
}
if (mat.channel_ != 1) {
return false;
}
if ((src.width_ != w) || (src.height_ != h)) {
return false;
}
unsigned char *ptr = mat;
const unsigned char *data_ptr = src;
for (int y = 0; y < h; y++) {

@ -1798,6 +1798,39 @@ TEST_F(MindDataImageProcess, testConvertRgbToGray) {
CompareMat(rgb_mat, lite_mat_gray);
}
TEST_F(MindDataImageProcess, testConvertRgbToGrayFail) {
std::string filename = "data/dataset/apple.jpg";
cv::Mat image = cv::imread(filename, cv::ImreadModes::IMREAD_COLOR);
cv::Mat rgb_mat;
cv::Mat rgb_mat1;
cv::cvtColor(image, rgb_mat, CV_BGR2GRAY);
cv::imwrite("./opencv_image.jpg", rgb_mat);
cv::cvtColor(image, rgb_mat1, CV_BGR2RGB);
// The width and height of the output image is different from the original image.
LiteMat lite_mat_rgb;
lite_mat_rgb.Init(rgb_mat1.cols, rgb_mat1.rows, rgb_mat1.channels(), rgb_mat1.data, LDataType::UINT8);
LiteMat lite_mat_gray;
bool ret = ConvertRgbToGray(lite_mat_rgb, LDataType::UINT8, 1000, 1000, lite_mat_gray);
ASSERT_TRUE(ret == false);
// The input lite_mat_rgb object is null.
LiteMat lite_mat_rgb1;
LiteMat lite_mat_gray1;
bool ret1 = ConvertRgbToGray(lite_mat_rgb1, LDataType::UINT8, image.cols, image.rows, lite_mat_gray1);
ASSERT_TRUE(ret1 == false);
// The channel of output image object is not 1.
LiteMat lite_mat_rgb2;
lite_mat_rgb2.Init(rgb_mat1.cols, rgb_mat1.rows, rgb_mat1.channels(), rgb_mat1.data, LDataType::UINT8);
LiteMat lite_mat_gray2;
lite_mat_gray2.Init(rgb_mat1.cols, rgb_mat1.rows, rgb_mat1.channels(), rgb_mat1.data, LDataType::UINT8);
bool ret2 = ConvertRgbToGray(lite_mat_rgb2, LDataType::UINT8, image.cols, image.rows, lite_mat_gray2);
ASSERT_TRUE(ret2 == false);
}
TEST_F(MindDataImageProcess, testResizePreserveARWithFillerv) {
std::string filename = "data/dataset/apple.jpg";
cv::Mat image = cv::imread(filename, cv::ImreadModes::IMREAD_COLOR);

Loading…
Cancel
Save