|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914 |
- /**
- * Copyright 2020-2021 Huawei Technologies Co., Ltd
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
- #include <opencv2/opencv.hpp>
- #include <opencv2/imgproc/types_c.h>
- #include <fstream>
-
- #include "common/common.h"
- #include "lite_cv/lite_mat.h"
- #include "lite_cv/image_process.h"
- #include "minddata/dataset/kernels/image/resize_cubic_op.h"
-
- using namespace mindspore::dataset;
- class MindDataImageProcess : public UT::Common {
- public:
- MindDataImageProcess() {}
-
- void SetUp() {}
- };
-
- void CompareMat(cv::Mat cv_mat, LiteMat lite_mat) {
- int cv_h = cv_mat.rows;
- int cv_w = cv_mat.cols;
- int cv_c = cv_mat.channels();
- int lite_h = lite_mat.height_;
- int lite_w = lite_mat.width_;
- int lite_c = lite_mat.channel_;
- ASSERT_TRUE(cv_h == lite_h);
- ASSERT_TRUE(cv_w == lite_w);
- ASSERT_TRUE(cv_c == lite_c);
- }
-
- void Lite3CImageProcess(LiteMat &lite_mat_bgr, LiteMat &lite_norm_mat_cut) {
- bool ret;
- LiteMat lite_mat_resize;
- ret = ResizeBilinear(lite_mat_bgr, lite_mat_resize, 256, 256);
- ASSERT_TRUE(ret == true);
- LiteMat lite_mat_convert_float;
- ret = ConvertTo(lite_mat_resize, lite_mat_convert_float, 1.0);
- ASSERT_TRUE(ret == true);
-
- LiteMat lite_mat_crop;
- ret = Crop(lite_mat_convert_float, lite_mat_crop, 16, 16, 224, 224);
- ASSERT_TRUE(ret == true);
- std::vector<float> means = {0.485, 0.456, 0.406};
- std::vector<float> stds = {0.229, 0.224, 0.225};
- SubStractMeanNormalize(lite_mat_crop, lite_norm_mat_cut, means, stds);
- return;
- }
-
- cv::Mat cv3CImageProcess(cv::Mat &image) {
- cv::Mat resize_256_image;
- cv::resize(image, resize_256_image, cv::Size(256, 256), CV_INTER_LINEAR);
- cv::Mat float_256_image;
- resize_256_image.convertTo(float_256_image, CV_32FC3);
-
- cv::Mat roi_224_image;
- cv::Rect roi;
- roi.x = 16;
- roi.y = 16;
- roi.width = 224;
- roi.height = 224;
-
- float_256_image(roi).copyTo(roi_224_image);
-
- float meanR = 0.485;
- float meanG = 0.456;
- float meanB = 0.406;
- float varR = 0.229;
- float varG = 0.224;
- float varB = 0.225;
- cv::Scalar mean = cv::Scalar(meanR, meanG, meanB);
- cv::Scalar var = cv::Scalar(varR, varG, varB);
-
- cv::Mat imgMean(roi_224_image.size(), CV_32FC3, mean);
- cv::Mat imgVar(roi_224_image.size(), CV_32FC3, var);
-
- cv::Mat imgR1 = roi_224_image - imgMean;
- cv::Mat imgR2 = imgR1 / imgVar;
- return imgR2;
- }
-
- void AccuracyComparison(const std::vector<std::vector<double>> &expect, LiteMat &value) {
- for (int i = 0; i < expect.size(); i++) {
- for (int j = 0; j < expect[0].size(); j++) {
- double middle = std::fabs(expect[i][j] - value.ptr<double>(i)[j]);
- ASSERT_TRUE(middle <= 0.005);
- }
- }
- }
-
- TEST_F(MindDataImageProcess, testRGB) {
- std::string filename = "data/dataset/apple.jpg";
- cv::Mat image = cv::imread(filename, cv::ImreadModes::IMREAD_COLOR);
-
- cv::Mat rgba_mat;
- cv::cvtColor(image, rgba_mat, CV_BGR2RGB);
-
- bool ret = false;
- LiteMat lite_mat_rgb;
- ret = InitFromPixel(rgba_mat.data, LPixelType::RGB, LDataType::UINT8, rgba_mat.cols, rgba_mat.rows, lite_mat_rgb);
- ASSERT_TRUE(ret == true);
-
- cv::Mat dst_image(lite_mat_rgb.height_, lite_mat_rgb.width_, CV_8UC3, lite_mat_rgb.data_ptr_);
- }
-
- TEST_F(MindDataImageProcess, testLoadByMemPtr) {
- std::string filename = "data/dataset/apple.jpg";
- cv::Mat image = cv::imread(filename, cv::ImreadModes::IMREAD_COLOR);
-
- cv::Mat rgba_mat;
- cv::cvtColor(image, rgba_mat, CV_BGR2RGB);
-
- bool ret = false;
- int width = rgba_mat.cols;
- int height = rgba_mat.rows;
- uchar *p_rgb = (uchar *)malloc(width * height * 3 * sizeof(uchar));
- for (int i = 0; i < height; i++) {
- const uchar *current = rgba_mat.ptr<uchar>(i);
- for (int j = 0; j < width; j++) {
- p_rgb[i * width * 3 + 3 * j + 0] = current[3 * j + 0];
- p_rgb[i * width * 3 + 3 * j + 1] = current[3 * j + 1];
- p_rgb[i * width * 3 + 3 * j + 2] = current[3 * j + 2];
- }
- }
-
- LiteMat lite_mat_rgb(width, height, 3, (void *)p_rgb, LDataType::UINT8);
- LiteMat lite_mat_resize;
- ret = ResizeBilinear(lite_mat_rgb, lite_mat_resize, 256, 256);
- ASSERT_TRUE(ret == true);
- LiteMat lite_mat_convert_float;
- ret = ConvertTo(lite_mat_resize, lite_mat_convert_float, 1.0);
- ASSERT_TRUE(ret == true);
-
- LiteMat lite_mat_crop;
- ret = Crop(lite_mat_convert_float, lite_mat_crop, 16, 16, 224, 224);
- ASSERT_TRUE(ret == true);
- std::vector<float> means = {0.485, 0.456, 0.406};
- std::vector<float> stds = {0.229, 0.224, 0.225};
- LiteMat lite_norm_mat_cut;
- ret = SubStractMeanNormalize(lite_mat_crop, lite_norm_mat_cut, means, stds);
-
- int pad_width = lite_norm_mat_cut.width_ + 20;
- int pad_height = lite_norm_mat_cut.height_ + 20;
- float *p_rgb_pad = (float *)malloc(pad_width * pad_height * 3 * sizeof(float));
-
- LiteMat makeborder(pad_width, pad_height, 3, (void *)p_rgb_pad, LDataType::FLOAT32);
- ret = Pad(lite_norm_mat_cut, makeborder, 10, 30, 40, 10, PaddBorderType::PADD_BORDER_CONSTANT, 255, 255, 255);
- cv::Mat dst_image(pad_height, pad_width, CV_8UC3, p_rgb_pad);
- free(p_rgb);
- free(p_rgb_pad);
- }
-
- TEST_F(MindDataImageProcess, test3C) {
- std::string filename = "data/dataset/apple.jpg";
- cv::Mat image = cv::imread(filename, cv::ImreadModes::IMREAD_COLOR);
- cv::Mat cv_image = cv3CImageProcess(image);
-
- // convert to RGBA for Android bitmap(rgba)
- cv::Mat rgba_mat;
- cv::cvtColor(image, rgba_mat, CV_BGR2RGBA);
-
- bool ret = false;
- LiteMat lite_mat_bgr;
- ret =
- InitFromPixel(rgba_mat.data, LPixelType::RGBA2BGR, LDataType::UINT8, rgba_mat.cols, rgba_mat.rows, lite_mat_bgr);
- ASSERT_TRUE(ret == true);
- LiteMat lite_norm_mat_cut;
- Lite3CImageProcess(lite_mat_bgr, lite_norm_mat_cut);
-
- cv::Mat dst_image(lite_norm_mat_cut.height_, lite_norm_mat_cut.width_, CV_32FC3, lite_norm_mat_cut.data_ptr_);
- CompareMat(cv_image, lite_norm_mat_cut);
- }
-
- TEST_F(MindDataImageProcess, testCubic3C) {
- std::string filename = "data/dataset/apple.jpg";
- cv::Mat image = cv::imread(filename, cv::ImreadModes::IMREAD_COLOR);
- cv::Mat rgb_mat;
- cv::cvtColor(image, rgb_mat, CV_BGR2RGB);
-
- LiteMat imIn, imOut;
- int32_t output_width = 24;
- int32_t output_height = 24;
- imIn.Init(rgb_mat.cols, rgb_mat.rows, rgb_mat.channels(), rgb_mat.data, LDataType::UINT8);
- imOut.Init(output_width, output_height, 3, LDataType::UINT8);
-
- bool ret = ResizeCubic(imIn, imOut, output_width, output_height);
-
- ASSERT_TRUE(ret == true);
- return;
- }
-
- bool ReadYUV(const char *filename, int w, int h, uint8_t **data) {
- FILE *f = fopen(filename, "rb");
- if (f == nullptr) {
- return false;
- }
- fseek(f, 0, SEEK_END);
- int size = ftell(f);
- int expect_size = w * h + 2 * ((w + 1) / 2) * ((h + 1) / 2);
- if (size != expect_size) {
- fclose(f);
- return false;
- }
- fseek(f, 0, SEEK_SET);
- *data = (uint8_t *)malloc(size);
- size_t re = fread(*data, 1, size, f);
- if (re != size) {
- fclose(f);
- return false;
- }
- fclose(f);
- return true;
- }
-
- TEST_F(MindDataImageProcess, TestRGBA2GRAY) {
- std::string filename = "data/dataset/apple.jpg";
- cv::Mat src_image = cv::imread(filename, cv::ImreadModes::IMREAD_COLOR);
- cv::Mat gray_image;
- cv::cvtColor(src_image, gray_image, CV_BGR2GRAY);
-
- cv::Mat rgba_mat;
- cv::cvtColor(src_image, rgba_mat, CV_BGR2RGBA);
- bool ret = false;
- LiteMat lite_mat_gray;
- ret =
- InitFromPixel(rgba_mat.data, LPixelType::RGBA2GRAY, LDataType::UINT8, rgba_mat.cols, rgba_mat.rows, lite_mat_gray);
- ASSERT_TRUE(ret == true);
-
- double distance = 0.f;
- int total_size = gray_image.cols * gray_image.rows * gray_image.channels();
- for (int i = 0; i < total_size; i++) {
- distance += pow((uint8_t)gray_image.data[i] - ((uint8_t *)lite_mat_gray)[i], 2);
- }
- distance = sqrt(distance / total_size);
- EXPECT_EQ(distance, 0.0f);
- }
-
- TEST_F(MindDataImageProcess, testNV21ToBGR) {
- // ffmpeg -i ./data/dataset/apple.jpg -s 1024*800 -pix_fmt nv21 ./data/dataset/yuv/test_nv21.yuv
- const char *filename = "data/dataset/yuv/test_nv21.yuv";
- int w = 1024;
- int h = 800;
- uint8_t *yuv_data = nullptr;
- bool ret = ReadYUV(filename, w, h, &yuv_data);
- ASSERT_TRUE(ret == true);
-
- cv::Mat yuvimg(h * 3 / 2, w, CV_8UC1, yuv_data);
- cv::Mat rgbimage;
-
- cv::cvtColor(yuvimg, rgbimage, cv::COLOR_YUV2BGR_NV21);
-
- LiteMat lite_mat_bgr;
-
- ret = InitFromPixel(yuv_data, LPixelType::NV212BGR, LDataType::UINT8, w, h, lite_mat_bgr);
- ASSERT_TRUE(ret == true);
- cv::Mat dst_image(lite_mat_bgr.height_, lite_mat_bgr.width_, CV_8UC3, lite_mat_bgr.data_ptr_);
- free(yuv_data);
- }
-
- TEST_F(MindDataImageProcess, testNV12ToBGR) {
- // ffmpeg -i ./data/dataset/apple.jpg -s 1024*800 -pix_fmt nv12 ./data/dataset/yuv/test_nv12.yuv
- const char *filename = "data/dataset/yuv/test_nv12.yuv";
- int w = 1024;
- int h = 800;
- uint8_t *yuv_data = nullptr;
- bool ret = ReadYUV(filename, w, h, &yuv_data);
- ASSERT_TRUE(ret == true);
-
- cv::Mat yuvimg(h * 3 / 2, w, CV_8UC1);
- memcpy(yuvimg.data, yuv_data, w * h * 3 / 2);
- cv::Mat rgbimage;
-
- cv::cvtColor(yuvimg, rgbimage, cv::COLOR_YUV2BGR_NV12);
- LiteMat lite_mat_bgr;
- ret = InitFromPixel(yuv_data, LPixelType::NV122BGR, LDataType::UINT8, w, h, lite_mat_bgr);
- ASSERT_TRUE(ret == true);
- cv::Mat dst_image(lite_mat_bgr.height_, lite_mat_bgr.width_, CV_8UC3, lite_mat_bgr.data_ptr_);
- free(yuv_data);
- }
-
- TEST_F(MindDataImageProcess, testExtractChannel) {
- std::string filename = "data/dataset/apple.jpg";
- cv::Mat src_image = cv::imread(filename, cv::ImreadModes::IMREAD_COLOR);
- cv::Mat dst_image;
- cv::extractChannel(src_image, dst_image, 2);
- // convert to RGBA for Android bitmap(rgba)
- cv::Mat rgba_mat;
- cv::cvtColor(src_image, rgba_mat, CV_BGR2RGBA);
-
- bool ret = false;
- LiteMat lite_mat_bgr;
- ret =
- InitFromPixel(rgba_mat.data, LPixelType::RGBA2BGR, LDataType::UINT8, rgba_mat.cols, rgba_mat.rows, lite_mat_bgr);
- ASSERT_TRUE(ret == true);
-
- LiteMat lite_B;
- ret = ExtractChannel(lite_mat_bgr, lite_B, 0);
- ASSERT_TRUE(ret == true);
-
- LiteMat lite_R;
- ret = ExtractChannel(lite_mat_bgr, lite_R, 2);
- ASSERT_TRUE(ret == true);
- cv::Mat dst_imageR(lite_R.height_, lite_R.width_, CV_8UC1, lite_R.data_ptr_);
- // cv::imwrite("./test_lite_r.jpg", dst_imageR);
- }
-
- TEST_F(MindDataImageProcess, testSplit) {
- std::string filename = "data/dataset/apple.jpg";
- cv::Mat src_image = cv::imread(filename, cv::ImreadModes::IMREAD_COLOR);
- std::vector<cv::Mat> dst_images;
- cv::split(src_image, dst_images);
- // convert to RGBA for Android bitmap(rgba)
- cv::Mat rgba_mat;
- cv::cvtColor(src_image, rgba_mat, CV_BGR2RGBA);
-
- bool ret = false;
- LiteMat lite_mat_bgr;
- ret =
- InitFromPixel(rgba_mat.data, LPixelType::RGBA2BGR, LDataType::UINT8, rgba_mat.cols, rgba_mat.rows, lite_mat_bgr);
- ASSERT_TRUE(ret == true);
- std::vector<LiteMat> lite_all;
- ret = Split(lite_mat_bgr, lite_all);
- ASSERT_TRUE(ret == true);
- ASSERT_TRUE(lite_all.size() == 3);
- LiteMat lite_r = lite_all[2];
- cv::Mat dst_imageR(lite_r.height_, lite_r.width_, CV_8UC1, lite_r.data_ptr_);
- }
-
- TEST_F(MindDataImageProcess, testMerge) {
- std::string filename = "data/dataset/apple.jpg";
- cv::Mat src_image = cv::imread(filename, cv::ImreadModes::IMREAD_COLOR);
- std::vector<cv::Mat> dst_images;
- cv::split(src_image, dst_images);
- // convert to RGBA for Android bitmap(rgba)
- cv::Mat rgba_mat;
- cv::cvtColor(src_image, rgba_mat, CV_BGR2RGBA);
-
- bool ret = false;
- LiteMat lite_mat_bgr;
- ret =
- InitFromPixel(rgba_mat.data, LPixelType::RGBA2BGR, LDataType::UINT8, rgba_mat.cols, rgba_mat.rows, lite_mat_bgr);
- ASSERT_TRUE(ret == true);
- std::vector<LiteMat> lite_all;
- ret = Split(lite_mat_bgr, lite_all);
- ASSERT_TRUE(ret == true);
- ASSERT_TRUE(lite_all.size() == 3);
- LiteMat lite_r = lite_all[2];
- cv::Mat dst_imageR(lite_r.height_, lite_r.width_, CV_8UC1, lite_r.data_ptr_);
-
- LiteMat merge_mat;
- EXPECT_TRUE(Merge(lite_all, merge_mat));
- EXPECT_EQ(merge_mat.height_, lite_mat_bgr.height_);
- EXPECT_EQ(merge_mat.width_, lite_mat_bgr.width_);
- EXPECT_EQ(merge_mat.channel_, lite_mat_bgr.channel_);
- }
-
- void Lite1CImageProcess(LiteMat &lite_mat_bgr, LiteMat &lite_norm_mat_cut) {
- LiteMat lite_mat_resize;
- int ret = ResizeBilinear(lite_mat_bgr, lite_mat_resize, 256, 256);
- ASSERT_TRUE(ret == true);
- LiteMat lite_mat_convert_float;
- ret = ConvertTo(lite_mat_resize, lite_mat_convert_float);
- ASSERT_TRUE(ret == true);
- LiteMat lite_mat_cut;
- ret = Crop(lite_mat_convert_float, lite_mat_cut, 16, 16, 224, 224);
- ASSERT_TRUE(ret == true);
- std::vector<float> means = {0.485};
- std::vector<float> stds = {0.229};
- ret = SubStractMeanNormalize(lite_mat_cut, lite_norm_mat_cut, means, stds);
- ASSERT_TRUE(ret == true);
- return;
- }
-
- cv::Mat cv1CImageProcess(cv::Mat &image) {
- cv::Mat gray_image;
- cv::cvtColor(image, gray_image, CV_BGR2GRAY);
-
- cv::Mat resize_256_image;
- cv::resize(gray_image, resize_256_image, cv::Size(256, 256), CV_INTER_LINEAR);
- cv::Mat float_256_image;
- resize_256_image.convertTo(float_256_image, CV_32FC3);
-
- cv::Mat roi_224_image;
- cv::Rect roi;
- roi.x = 16;
- roi.y = 16;
- roi.width = 224;
- roi.height = 224;
-
- float_256_image(roi).copyTo(roi_224_image);
-
- float meanR = 0.485;
- float varR = 0.229;
- cv::Scalar mean = cv::Scalar(meanR);
- cv::Scalar var = cv::Scalar(varR);
-
- cv::Mat imgMean(roi_224_image.size(), CV_32FC1, mean);
- cv::Mat imgVar(roi_224_image.size(), CV_32FC1, var);
-
- cv::Mat imgR1 = roi_224_image - imgMean;
- cv::Mat imgR2 = imgR1 / imgVar;
- return imgR2;
- }
-
- TEST_F(MindDataImageProcess, test1C) {
- std::string filename = "data/dataset/apple.jpg";
- cv::Mat image = cv::imread(filename, cv::ImreadModes::IMREAD_COLOR);
- cv::Mat cv_image = cv1CImageProcess(image);
-
- // convert to RGBA for Android bitmap(rgba)
- cv::Mat rgba_mat;
- cv::cvtColor(image, rgba_mat, CV_BGR2RGBA);
-
- LiteMat lite_mat_bgr;
- bool ret =
- InitFromPixel(rgba_mat.data, LPixelType::RGBA2GRAY, LDataType::UINT8, rgba_mat.cols, rgba_mat.rows, lite_mat_bgr);
- ASSERT_TRUE(ret == true);
- LiteMat lite_norm_mat_cut;
- Lite1CImageProcess(lite_mat_bgr, lite_norm_mat_cut);
- cv::Mat dst_image(lite_norm_mat_cut.height_, lite_norm_mat_cut.width_, CV_32FC1, lite_norm_mat_cut.data_ptr_);
- CompareMat(cv_image, lite_norm_mat_cut);
- }
-
- TEST_F(MindDataImageProcess, TestPadd) {
- std::string filename = "data/dataset/apple.jpg";
- cv::Mat image = cv::imread(filename, cv::ImreadModes::IMREAD_COLOR);
-
- int left = 10;
- int right = 20;
- int top = 30;
- int bottom = 40;
- cv::Mat b_image;
- cv::Scalar color = cv::Scalar(255, 255, 255);
- cv::copyMakeBorder(image, b_image, top, bottom, left, right, cv::BORDER_CONSTANT, color);
- cv::Mat rgba_mat;
- cv::cvtColor(image, rgba_mat, CV_BGR2RGBA);
-
- LiteMat lite_mat_bgr;
- bool ret =
- InitFromPixel(rgba_mat.data, LPixelType::RGBA2BGR, LDataType::UINT8, rgba_mat.cols, rgba_mat.rows, lite_mat_bgr);
- ASSERT_TRUE(ret == true);
- LiteMat makeborder;
- ret = Pad(lite_mat_bgr, makeborder, top, bottom, left, right, PaddBorderType::PADD_BORDER_CONSTANT, 255, 255, 255);
- ASSERT_TRUE(ret == true);
- size_t total_size = makeborder.height_ * makeborder.width_ * makeborder.channel_;
- double distance = 0.0f;
- for (size_t i = 0; i < total_size; i++) {
- distance += pow((uint8_t)b_image.data[i] - ((uint8_t *)makeborder)[i], 2);
- }
- distance = sqrt(distance / total_size);
- EXPECT_EQ(distance, 0.0f);
- }
-
- TEST_F(MindDataImageProcess, TestPadZero) {
- std::string filename = "data/dataset/apple.jpg";
- cv::Mat image = cv::imread(filename, cv::ImreadModes::IMREAD_COLOR);
-
- int left = 0;
- int right = 0;
- int top = 0;
- int bottom = 0;
- cv::Mat b_image;
- cv::Scalar color = cv::Scalar(255, 255, 255);
- cv::copyMakeBorder(image, b_image, top, bottom, left, right, cv::BORDER_CONSTANT, color);
- cv::Mat rgba_mat;
- cv::cvtColor(image, rgba_mat, CV_BGR2RGBA);
-
- LiteMat lite_mat_bgr;
- bool ret =
- InitFromPixel(rgba_mat.data, LPixelType::RGBA2BGR, LDataType::UINT8, rgba_mat.cols, rgba_mat.rows, lite_mat_bgr);
- ASSERT_TRUE(ret == true);
- LiteMat makeborder;
- ret = Pad(lite_mat_bgr, makeborder, top, bottom, left, right, PaddBorderType::PADD_BORDER_CONSTANT, 255, 255, 255);
- ASSERT_TRUE(ret == true);
- size_t total_size = makeborder.height_ * makeborder.width_ * makeborder.channel_;
- double distance = 0.0f;
- for (size_t i = 0; i < total_size; i++) {
- distance += pow((uint8_t)b_image.data[i] - ((uint8_t *)makeborder)[i], 2);
- }
- distance = sqrt(distance / total_size);
- EXPECT_EQ(distance, 0.0f);
- }
-
- TEST_F(MindDataImageProcess, TestPadReplicate) {
- std::string filename = "data/dataset/apple.jpg";
- cv::Mat image = cv::imread(filename, cv::ImreadModes::IMREAD_COLOR);
-
- int left = 20;
- int right = 20;
- int top = 20;
- int bottom = 20;
- cv::Mat b_image;
- cv::copyMakeBorder(image, b_image, top, bottom, left, right, cv::BORDER_REPLICATE);
-
- cv::Mat rgba_mat;
- cv::cvtColor(image, rgba_mat, CV_BGR2RGBA);
- LiteMat lite_mat_bgr;
- bool ret =
- InitFromPixel(rgba_mat.data, LPixelType::RGBA2BGR, LDataType::UINT8, rgba_mat.cols, rgba_mat.rows, lite_mat_bgr);
- ASSERT_TRUE(ret == true);
-
- LiteMat makeborder;
- ret = Pad(lite_mat_bgr, makeborder, top, bottom, left, right, PaddBorderType::PADD_BORDER_REPLICATE);
- ASSERT_TRUE(ret == true);
-
- size_t total_size = makeborder.height_ * makeborder.width_ * makeborder.channel_;
- double distance = 0.0f;
- for (size_t i = 0; i < total_size; i++) {
- distance += pow((uint8_t)b_image.data[i] - ((uint8_t *)makeborder)[i], 2);
- }
- distance = sqrt(distance / total_size);
- EXPECT_EQ(distance, 0.0f);
- }
-
- TEST_F(MindDataImageProcess, TestPadReflect101) {
- std::string filename = "data/dataset/apple.jpg";
- cv::Mat image = cv::imread(filename, cv::ImreadModes::IMREAD_COLOR);
-
- int left = 20;
- int right = 20;
- int top = 20;
- int bottom = 20;
- cv::Mat b_image;
- cv::copyMakeBorder(image, b_image, top, bottom, left, right, cv::BORDER_REFLECT_101);
-
- cv::Mat rgba_mat;
- cv::cvtColor(image, rgba_mat, CV_BGR2RGBA);
- LiteMat lite_mat_bgr;
- bool ret =
- InitFromPixel(rgba_mat.data, LPixelType::RGBA2BGR, LDataType::UINT8, rgba_mat.cols, rgba_mat.rows, lite_mat_bgr);
- ASSERT_TRUE(ret == true);
-
- LiteMat makeborder;
- ret = Pad(lite_mat_bgr, makeborder, top, bottom, left, right, PaddBorderType::PADD_BORDER_REFLECT_101);
- ASSERT_TRUE(ret == true);
-
- size_t total_size = makeborder.height_ * makeborder.width_ * makeborder.channel_;
- double distance = 0.0f;
- for (size_t i = 0; i < total_size; i++) {
- distance += pow((uint8_t)b_image.data[i] - ((uint8_t *)makeborder)[i], 2);
- }
- distance = sqrt(distance / total_size);
- EXPECT_EQ(distance, 0.0f);
- }
-
- TEST_F(MindDataImageProcess, TestGetDefaultBoxes) {
- std::string benchmark = "data/dataset/testLite/default_boxes.bin";
- BoxesConfig config;
- config.img_shape = {300, 300};
- config.num_default = {3, 6, 6, 6, 6, 6};
- config.feature_size = {19, 10, 5, 3, 2, 1};
- config.min_scale = 0.2;
- config.max_scale = 0.95;
- config.aspect_rations = {{2}, {2, 3}, {2, 3}, {2, 3}, {2, 3}, {2, 3}};
- config.steps = {16, 32, 64, 100, 150, 300};
- config.prior_scaling = {0.1, 0.2};
-
- int rows = 1917;
- int cols = 4;
- std::vector<double> benchmark_boxes(rows * cols);
- std::ifstream in(benchmark, std::ios::in | std::ios::binary);
- in.read(reinterpret_cast<char *>(benchmark_boxes.data()), benchmark_boxes.size() * sizeof(double));
- in.close();
-
- std::vector<std::vector<float>> default_boxes = GetDefaultBoxes(config);
- EXPECT_EQ(default_boxes.size(), rows);
- EXPECT_EQ(default_boxes[0].size(), cols);
-
- double distance = 0.0f;
- for (int i = 0; i < rows; i++) {
- for (int j = 0; j < cols; j++) {
- distance += pow(default_boxes[i][j] - benchmark_boxes[i * cols + j], 2);
- }
- }
- distance = sqrt(distance);
- EXPECT_LT(distance, 1e-5);
- }
-
- TEST_F(MindDataImageProcess, TestApplyNms) {
- std::vector<std::vector<float>> all_boxes = {{1, 1, 2, 2}, {3, 3, 4, 4}, {5, 5, 6, 6}, {5, 5, 6, 6}};
- std::vector<float> all_scores = {0.6, 0.5, 0.4, 0.9};
- std::vector<int> keep = ApplyNms(all_boxes, all_scores, 0.5, 10);
- ASSERT_TRUE(keep[0] == 3);
- ASSERT_TRUE(keep[1] == 0);
- ASSERT_TRUE(keep[2] == 1);
- }
-
- TEST_F(MindDataImageProcess, TestAffineInput) {
- LiteMat src(3, 3);
- LiteMat dst;
- double M[6] = {1};
- EXPECT_FALSE(Affine(src, dst, M, {}, UINT8_C1(0)));
- EXPECT_FALSE(Affine(src, dst, M, {3}, UINT8_C1(0)));
- EXPECT_FALSE(Affine(src, dst, M, {0, 0}, UINT8_C1(0)));
- }
-
- TEST_F(MindDataImageProcess, TestAffine) {
- // The input matrix
- // 0 0 1 0 0
- // 0 0 1 0 0
- // 2 2 3 2 2
- // 0 0 1 0 0
- // 0 0 1 0 0
- size_t rows = 5;
- size_t cols = 5;
- LiteMat src(rows, cols);
- for (size_t i = 0; i < rows; i++) {
- for (size_t j = 0; j < cols; j++) {
- if (i == 2 && j == 2) {
- static_cast<UINT8_C1 *>(src.data_ptr_)[i * cols + j] = 3;
- } else if (i == 2) {
- static_cast<UINT8_C1 *>(src.data_ptr_)[i * cols + j] = 2;
- } else if (j == 2) {
- static_cast<UINT8_C1 *>(src.data_ptr_)[i * cols + j] = 1;
- } else {
- static_cast<UINT8_C1 *>(src.data_ptr_)[i * cols + j] = 0;
- }
- }
- }
-
- // Expect output matrix
- // 0 0 2 0 0
- // 0 0 2 0 0
- // 1 1 3 1 1
- // 0 0 2 0 0
- // 0 0 2 0 0
- LiteMat expect(rows, cols);
- for (size_t i = 0; i < rows; i++) {
- for (size_t j = 0; j < cols; j++) {
- if (i == 2 && j == 2) {
- static_cast<UINT8_C1 *>(expect.data_ptr_)[i * cols + j] = 3;
- } else if (i == 2) {
- static_cast<UINT8_C1 *>(expect.data_ptr_)[i * cols + j] = 1;
- } else if (j == 2) {
- static_cast<UINT8_C1 *>(expect.data_ptr_)[i * cols + j] = 2;
- } else {
- static_cast<UINT8_C1 *>(expect.data_ptr_)[i * cols + j] = 0;
- }
- }
- }
-
- double angle = 90.0f;
- cv::Point2f center(rows / 2, cols / 2);
- cv::Mat rotate_matrix = cv::getRotationMatrix2D(center, angle, 1.0);
- double M[6];
- for (size_t i = 0; i < 6; i++) {
- M[i] = rotate_matrix.at<double>(i);
- }
- LiteMat dst;
- EXPECT_TRUE(Affine(src, dst, M, {rows, cols}, UINT8_C1(0)));
-
- for (size_t i = 0; i < rows; i++) {
- for (size_t j = 0; j < cols; j++) {
- EXPECT_EQ(static_cast<UINT8_C1 *>(expect.data_ptr_)[i * cols + j].c1,
- static_cast<UINT8_C1 *>(dst.data_ptr_)[i * cols + j].c1);
- }
- }
- }
-
- TEST_F(MindDataImageProcess, TestSubtractUint8) {
- const size_t cols = 4;
- // Test uint8
- LiteMat src1_uint8(1, cols);
- LiteMat src2_uint8(1, cols);
- LiteMat expect_uint8(1, cols);
- for (size_t i = 0; i < cols; i++) {
- static_cast<UINT8_C1 *>(src1_uint8.data_ptr_)[i] = 3;
- static_cast<UINT8_C1 *>(src2_uint8.data_ptr_)[i] = 2;
- static_cast<UINT8_C1 *>(expect_uint8.data_ptr_)[i] = 1;
- }
- LiteMat dst_uint8;
- EXPECT_TRUE(Subtract(src1_uint8, src2_uint8, &dst_uint8));
- for (size_t i = 0; i < cols; i++) {
- EXPECT_EQ(static_cast<UINT8_C1 *>(expect_uint8.data_ptr_)[i].c1,
- static_cast<UINT8_C1 *>(dst_uint8.data_ptr_)[i].c1);
- }
- }
-
- TEST_F(MindDataImageProcess, TestSubtractInt8) {
- const size_t cols = 4;
- // Test int8
- LiteMat src1_int8(1, cols, LDataType(LDataType::INT8));
- LiteMat src2_int8(1, cols, LDataType(LDataType::INT8));
- LiteMat expect_int8(1, cols, LDataType(LDataType::INT8));
- for (size_t i = 0; i < cols; i++) {
- static_cast<INT8_C1 *>(src1_int8.data_ptr_)[i] = 2;
- static_cast<INT8_C1 *>(src2_int8.data_ptr_)[i] = 3;
- static_cast<INT8_C1 *>(expect_int8.data_ptr_)[i] = -1;
- }
- LiteMat dst_int8;
- EXPECT_TRUE(Subtract(src1_int8, src2_int8, &dst_int8));
- for (size_t i = 0; i < cols; i++) {
- EXPECT_EQ(static_cast<INT8_C1 *>(expect_int8.data_ptr_)[i].c1, static_cast<INT8_C1 *>(dst_int8.data_ptr_)[i].c1);
- }
- }
-
- TEST_F(MindDataImageProcess, TestSubtractUInt16) {
- const size_t cols = 4;
- // Test uint16
- LiteMat src1_uint16(1, cols, LDataType(LDataType::UINT16));
- LiteMat src2_uint16(1, cols, LDataType(LDataType::UINT16));
- LiteMat expect_uint16(1, cols, LDataType(LDataType::UINT16));
- for (size_t i = 0; i < cols; i++) {
- static_cast<UINT16_C1 *>(src1_uint16.data_ptr_)[i] = 2;
- static_cast<UINT16_C1 *>(src2_uint16.data_ptr_)[i] = 3;
- static_cast<UINT16_C1 *>(expect_uint16.data_ptr_)[i] = 0;
- }
- LiteMat dst_uint16;
- EXPECT_TRUE(Subtract(src1_uint16, src2_uint16, &dst_uint16));
- for (size_t i = 0; i < cols; i++) {
- EXPECT_EQ(static_cast<UINT16_C1 *>(expect_uint16.data_ptr_)[i].c1,
- static_cast<UINT16_C1 *>(dst_uint16.data_ptr_)[i].c1);
- }
- }
-
- TEST_F(MindDataImageProcess, TestSubtractInt16) {
- const size_t cols = 4;
- // Test int16
- LiteMat src1_int16(1, cols, LDataType(LDataType::INT16));
- LiteMat src2_int16(1, cols, LDataType(LDataType::INT16));
- LiteMat expect_int16(1, cols, LDataType(LDataType::INT16));
- for (size_t i = 0; i < cols; i++) {
- static_cast<INT16_C1 *>(src1_int16.data_ptr_)[i] = 2;
- static_cast<INT16_C1 *>(src2_int16.data_ptr_)[i] = 3;
- static_cast<INT16_C1 *>(expect_int16.data_ptr_)[i] = -1;
- }
- LiteMat dst_int16;
- EXPECT_TRUE(Subtract(src1_int16, src2_int16, &dst_int16));
- for (size_t i = 0; i < cols; i++) {
- EXPECT_EQ(static_cast<INT16_C1 *>(expect_int16.data_ptr_)[i].c1,
- static_cast<INT16_C1 *>(dst_int16.data_ptr_)[i].c1);
- }
- }
-
- TEST_F(MindDataImageProcess, TestSubtractUInt32) {
- const size_t cols = 4;
- // Test uint16
- LiteMat src1_uint32(1, cols, LDataType(LDataType::UINT32));
- LiteMat src2_uint32(1, cols, LDataType(LDataType::UINT32));
- LiteMat expect_uint32(1, cols, LDataType(LDataType::UINT32));
- for (size_t i = 0; i < cols; i++) {
- static_cast<UINT32_C1 *>(src1_uint32.data_ptr_)[i] = 2;
- static_cast<UINT32_C1 *>(src2_uint32.data_ptr_)[i] = 3;
- static_cast<UINT32_C1 *>(expect_uint32.data_ptr_)[i] = 0;
- }
- LiteMat dst_uint32;
- EXPECT_TRUE(Subtract(src1_uint32, src2_uint32, &dst_uint32));
- for (size_t i = 0; i < cols; i++) {
- EXPECT_EQ(static_cast<UINT32_C1 *>(expect_uint32.data_ptr_)[i].c1,
- static_cast<UINT32_C1 *>(dst_uint32.data_ptr_)[i].c1);
- }
- }
-
- TEST_F(MindDataImageProcess, TestSubtractInt32) {
- const size_t cols = 4;
- // Test int32
- LiteMat src1_int32(1, cols, LDataType(LDataType::INT32));
- LiteMat src2_int32(1, cols, LDataType(LDataType::INT32));
- LiteMat expect_int32(1, cols, LDataType(LDataType::INT32));
- for (size_t i = 0; i < cols; i++) {
- static_cast<INT32_C1 *>(src1_int32.data_ptr_)[i] = 2;
- static_cast<INT32_C1 *>(src2_int32.data_ptr_)[i] = 4;
- static_cast<INT32_C1 *>(expect_int32.data_ptr_)[i] = -2;
- }
- LiteMat dst_int32;
- EXPECT_TRUE(Subtract(src1_int32, src2_int32, &dst_int32));
- for (size_t i = 0; i < cols; i++) {
- EXPECT_EQ(static_cast<INT32_C1 *>(expect_int32.data_ptr_)[i].c1,
- static_cast<INT32_C1 *>(dst_int32.data_ptr_)[i].c1);
- }
- }
-
- TEST_F(MindDataImageProcess, TestSubtractFloat) {
- const size_t cols = 4;
- // Test float
- LiteMat src1_float(1, cols, LDataType(LDataType::FLOAT32));
- LiteMat src2_float(1, cols, LDataType(LDataType::FLOAT32));
- LiteMat expect_float(1, cols, LDataType(LDataType::FLOAT32));
- for (size_t i = 0; i < cols; i++) {
- static_cast<FLOAT32_C1 *>(src1_float.data_ptr_)[i] = 3.4;
- static_cast<FLOAT32_C1 *>(src2_float.data_ptr_)[i] = 5.7;
- static_cast<FLOAT32_C1 *>(expect_float.data_ptr_)[i] = -2.3;
- }
- LiteMat dst_float;
- EXPECT_TRUE(Subtract(src1_float, src2_float, &dst_float));
- for (size_t i = 0; i < cols; i++) {
- EXPECT_FLOAT_EQ(static_cast<FLOAT32_C1 *>(expect_float.data_ptr_)[i].c1,
- static_cast<FLOAT32_C1 *>(dst_float.data_ptr_)[i].c1);
- }
- }
-
- TEST_F(MindDataImageProcess, TestDivideUint8) {
- const size_t cols = 4;
- // Test uint8
- LiteMat src1_uint8(1, cols);
- LiteMat src2_uint8(1, cols);
- LiteMat expect_uint8(1, cols);
- for (size_t i = 0; i < cols; i++) {
- static_cast<UINT8_C1 *>(src1_uint8.data_ptr_)[i] = 8;
- static_cast<UINT8_C1 *>(src2_uint8.data_ptr_)[i] = 4;
- static_cast<UINT8_C1 *>(expect_uint8.data_ptr_)[i] = 2;
- }
- LiteMat dst_uint8;
- EXPECT_TRUE(Divide(src1_uint8, src2_uint8, &dst_uint8));
- for (size_t i = 0; i < cols; i++) {
- EXPECT_EQ(static_cast<UINT8_C1 *>(expect_uint8.data_ptr_)[i].c1,
- static_cast<UINT8_C1 *>(dst_uint8.data_ptr_)[i].c1);
- }
- }
-
- TEST_F(MindDataImageProcess, TestDivideInt8) {
- const size_t cols = 4;
- // Test int8
- LiteMat src1_int8(1, cols, LDataType(LDataType::INT8));
- LiteMat src2_int8(1, cols, LDataType(LDataType::INT8));
- LiteMat expect_int8(1, cols, LDataType(LDataType::INT8));
- for (size_t i = 0; i < cols; i++) {
- static_cast<INT8_C1 *>(src1_int8.data_ptr_)[i] = 8;
- static_cast<INT8_C1 *>(src2_int8.data_ptr_)[i] = -4;
- static_cast<INT8_C1 *>(expect_int8.data_ptr_)[i] = -2;
- }
- LiteMat dst_int8;
- EXPECT_TRUE(Divide(src1_int8, src2_int8, &dst_int8));
- for (size_t i = 0; i < cols; i++) {
- EXPECT_EQ(static_cast<INT8_C1 *>(expect_int8.data_ptr_)[i].c1, static_cast<INT8_C1 *>(dst_int8.data_ptr_)[i].c1);
- }
- }
-
- TEST_F(MindDataImageProcess, TestDivideUInt16) {
- const size_t cols = 4;
- // Test uint16
- LiteMat src1_uint16(1, cols, LDataType(LDataType::UINT16));
- LiteMat src2_uint16(1, cols, LDataType(LDataType::UINT16));
- LiteMat expect_uint16(1, cols, LDataType(LDataType::UINT16));
- for (size_t i = 0; i < cols; i++) {
- static_cast<UINT16_C1 *>(src1_uint16.data_ptr_)[i] = 40000;
- static_cast<UINT16_C1 *>(src2_uint16.data_ptr_)[i] = 20000;
- static_cast<UINT16_C1 *>(expect_uint16.data_ptr_)[i] = 2;
- }
- LiteMat dst_uint16;
- EXPECT_TRUE(Divide(src1_uint16, src2_uint16, &dst_uint16));
- for (size_t i = 0; i < cols; i++) {
- EXPECT_EQ(static_cast<UINT16_C1 *>(expect_uint16.data_ptr_)[i].c1,
- static_cast<UINT16_C1 *>(dst_uint16.data_ptr_)[i].c1);
- }
- }
-
- TEST_F(MindDataImageProcess, TestDivideInt16) {
- const size_t cols = 4;
- // Test int16
- LiteMat src1_int16(1, cols, LDataType(LDataType::INT16));
- LiteMat src2_int16(1, cols, LDataType(LDataType::INT16));
- LiteMat expect_int16(1, cols, LDataType(LDataType::INT16));
- for (size_t i = 0; i < cols; i++) {
- static_cast<INT16_C1 *>(src1_int16.data_ptr_)[i] = 30000;
- static_cast<INT16_C1 *>(src2_int16.data_ptr_)[i] = -3;
- static_cast<INT16_C1 *>(expect_int16.data_ptr_)[i] = -10000;
- }
- LiteMat dst_int16;
- EXPECT_TRUE(Divide(src1_int16, src2_int16, &dst_int16));
- for (size_t i = 0; i < cols; i++) {
- EXPECT_EQ(static_cast<INT16_C1 *>(expect_int16.data_ptr_)[i].c1,
- static_cast<INT16_C1 *>(dst_int16.data_ptr_)[i].c1);
- }
- }
-
- TEST_F(MindDataImageProcess, TestDivideUInt32) {
- const size_t cols = 4;
- // Test uint16
- LiteMat src1_uint32(1, cols, LDataType(LDataType::UINT32));
- LiteMat src2_uint32(1, cols, LDataType(LDataType::UINT32));
- LiteMat expect_uint32(1, cols, LDataType(LDataType::UINT32));
- for (size_t i = 0; i < cols; i++) {
- static_cast<UINT32_C1 *>(src1_uint32.data_ptr_)[i] = 4000000000;
- static_cast<UINT32_C1 *>(src2_uint32.data_ptr_)[i] = 4;
- static_cast<UINT32_C1 *>(expect_uint32.data_ptr_)[i] = 1000000000;
- }
- LiteMat dst_uint32;
- EXPECT_TRUE(Divide(src1_uint32, src2_uint32, &dst_uint32));
- for (size_t i = 0; i < cols; i++) {
- EXPECT_EQ(static_cast<UINT32_C1 *>(expect_uint32.data_ptr_)[i].c1,
- static_cast<UINT32_C1 *>(dst_uint32.data_ptr_)[i].c1);
- }
- }
-
- TEST_F(MindDataImageProcess, TestDivideInt32) {
- const size_t cols = 4;
- // Test int32
- LiteMat src1_int32(1, cols, LDataType(LDataType::INT32));
- LiteMat src2_int32(1, cols, LDataType(LDataType::INT32));
- LiteMat expect_int32(1, cols, LDataType(LDataType::INT32));
- for (size_t i = 0; i < cols; i++) {
- static_cast<INT32_C1 *>(src1_int32.data_ptr_)[i] = 2000000000;
- static_cast<INT32_C1 *>(src2_int32.data_ptr_)[i] = -2;
- static_cast<INT32_C1 *>(expect_int32.data_ptr_)[i] = -1000000000;
- }
- LiteMat dst_int32;
- EXPECT_TRUE(Divide(src1_int32, src2_int32, &dst_int32));
- for (size_t i = 0; i < cols; i++) {
- EXPECT_EQ(static_cast<INT32_C1 *>(expect_int32.data_ptr_)[i].c1,
- static_cast<INT32_C1 *>(dst_int32.data_ptr_)[i].c1);
- }
- }
-
- TEST_F(MindDataImageProcess, TestDivideFloat) {
- const size_t cols = 4;
- // Test float
- LiteMat src1_float(1, cols, LDataType(LDataType::FLOAT32));
- LiteMat src2_float(1, cols, LDataType(LDataType::FLOAT32));
- LiteMat expect_float(1, cols, LDataType(LDataType::FLOAT32));
- for (size_t i = 0; i < cols; i++) {
- static_cast<FLOAT32_C1 *>(src1_float.data_ptr_)[i] = 12.34f;
- static_cast<FLOAT32_C1 *>(src2_float.data_ptr_)[i] = -2.0f;
- static_cast<FLOAT32_C1 *>(expect_float.data_ptr_)[i] = -6.17f;
- }
- LiteMat dst_float;
- EXPECT_TRUE(Divide(src1_float, src2_float, &dst_float));
- for (size_t i = 0; i < cols; i++) {
- EXPECT_FLOAT_EQ(static_cast<FLOAT32_C1 *>(expect_float.data_ptr_)[i].c1,
- static_cast<FLOAT32_C1 *>(dst_float.data_ptr_)[i].c1);
- }
- }
-
- TEST_F(MindDataImageProcess, TestMultiplyUint8) {
- const size_t cols = 4;
- // Test uint8
- LiteMat src1_uint8(1, cols);
- LiteMat src2_uint8(1, cols);
- LiteMat expect_uint8(1, cols);
- for (size_t i = 0; i < cols; i++) {
- static_cast<UINT8_C1 *>(src1_uint8.data_ptr_)[i] = 8;
- static_cast<UINT8_C1 *>(src2_uint8.data_ptr_)[i] = 4;
- static_cast<UINT8_C1 *>(expect_uint8.data_ptr_)[i] = 32;
- }
- LiteMat dst_uint8;
- EXPECT_TRUE(Multiply(src1_uint8, src2_uint8, &dst_uint8));
- for (size_t i = 0; i < cols; i++) {
- EXPECT_EQ(static_cast<UINT8_C1 *>(expect_uint8.data_ptr_)[i].c1,
- static_cast<UINT8_C1 *>(dst_uint8.data_ptr_)[i].c1);
- }
- }
-
- TEST_F(MindDataImageProcess, TestMultiplyUInt16) {
- const size_t cols = 4;
- // Test int16
- LiteMat src1_int16(1, cols, LDataType(LDataType::UINT16));
- LiteMat src2_int16(1, cols, LDataType(LDataType::UINT16));
- LiteMat expect_int16(1, cols, LDataType(LDataType::UINT16));
- for (size_t i = 0; i < cols; i++) {
- static_cast<UINT16_C1 *>(src1_int16.data_ptr_)[i] = 60000;
- static_cast<UINT16_C1 *>(src2_int16.data_ptr_)[i] = 2;
- static_cast<UINT16_C1 *>(expect_int16.data_ptr_)[i] = 65535;
- }
- LiteMat dst_int16;
- EXPECT_TRUE(Multiply(src1_int16, src2_int16, &dst_int16));
- for (size_t i = 0; i < cols; i++) {
- EXPECT_EQ(static_cast<UINT16_C1 *>(expect_int16.data_ptr_)[i].c1,
- static_cast<UINT16_C1 *>(dst_int16.data_ptr_)[i].c1);
- }
- }
-
- TEST_F(MindDataImageProcess, TestMultiplyFloat) {
- const size_t cols = 4;
- // Test float
- LiteMat src1_float(1, cols, LDataType(LDataType::FLOAT32));
- LiteMat src2_float(1, cols, LDataType(LDataType::FLOAT32));
- LiteMat expect_float(1, cols, LDataType(LDataType::FLOAT32));
- for (size_t i = 0; i < cols; i++) {
- static_cast<FLOAT32_C1 *>(src1_float.data_ptr_)[i] = 30.0f;
- static_cast<FLOAT32_C1 *>(src2_float.data_ptr_)[i] = -2.0f;
- static_cast<FLOAT32_C1 *>(expect_float.data_ptr_)[i] = -60.0f;
- }
- LiteMat dst_float;
- EXPECT_TRUE(Multiply(src1_float, src2_float, &dst_float));
- for (size_t i = 0; i < cols; i++) {
- EXPECT_FLOAT_EQ(static_cast<FLOAT32_C1 *>(expect_float.data_ptr_)[i].c1,
- static_cast<FLOAT32_C1 *>(dst_float.data_ptr_)[i].c1);
- }
- }
-
- TEST_F(MindDataImageProcess, TestExtractChannel) {
- LiteMat lite_single;
- LiteMat lite_mat = LiteMat(1, 4, 3, LDataType::UINT16);
-
- EXPECT_FALSE(ExtractChannel(lite_mat, lite_single, 0));
- EXPECT_TRUE(lite_single.IsEmpty());
- }
- TEST_F(MindDataImageProcess, testROI3C) {
- std::string filename = "data/dataset/apple.jpg";
- cv::Mat src_image = cv::imread(filename, cv::ImreadModes::IMREAD_COLOR);
-
- cv::Mat cv_roi = cv::Mat(src_image, cv::Rect(500, 500, 3000, 1500));
-
- cv::imwrite("./cv_roi.jpg", cv_roi);
-
- bool ret = false;
- LiteMat lite_mat_bgr;
- ret = InitFromPixel(src_image.data, LPixelType::BGR, LDataType::UINT8, src_image.cols, src_image.rows, lite_mat_bgr);
- EXPECT_TRUE(ret);
- LiteMat lite_roi;
-
- ret = lite_mat_bgr.GetROI(500, 500, 3000, 1500, lite_roi);
- EXPECT_TRUE(ret);
-
- LiteMat lite_roi_save(3000, 1500, lite_roi.channel_, LDataType::UINT8);
-
- for (size_t i = 0; i < lite_roi.height_; i++) {
- const unsigned char *ptr = lite_roi.ptr<unsigned char>(i);
- size_t image_size = lite_roi.width_ * lite_roi.channel_ * sizeof(unsigned char);
- unsigned char *dst_ptr = (unsigned char *)lite_roi_save.data_ptr_ + image_size * i;
- (void)memcpy(dst_ptr, ptr, image_size);
- }
-
- cv::Mat dst_imageR(lite_roi_save.height_, lite_roi_save.width_, CV_8UC3, lite_roi_save.data_ptr_);
- cv::imwrite("./lite_roi.jpg", dst_imageR);
- }
-
- TEST_F(MindDataImageProcess, testROI3CFalse) {
- std::string filename = "data/dataset/apple.jpg";
- cv::Mat src_image = cv::imread(filename, cv::ImreadModes::IMREAD_COLOR);
-
- cv::Mat cv_roi = cv::Mat(src_image, cv::Rect(500, 500, 3000, 1500));
-
- cv::imwrite("./cv_roi.jpg", cv_roi);
-
- bool ret = false;
- LiteMat lite_mat_bgr;
- ret = InitFromPixel(src_image.data, LPixelType::BGR, LDataType::UINT8, src_image.cols, src_image.rows, lite_mat_bgr);
- EXPECT_TRUE(ret);
- LiteMat lite_roi;
-
- ret = lite_mat_bgr.GetROI(500, 500, 1200, -100, lite_roi);
- EXPECT_FALSE(ret);
- }
-
- TEST_F(MindDataImageProcess, testROI1C) {
- std::string filename = "data/dataset/apple.jpg";
- cv::Mat src_image = cv::imread(filename, cv::ImreadModes::IMREAD_COLOR);
-
- cv::Mat gray_image;
- cv::cvtColor(src_image, gray_image, CV_BGR2GRAY);
- cv::Mat cv_roi_gray = cv::Mat(gray_image, cv::Rect(500, 500, 3000, 1500));
-
- cv::imwrite("./cv_roi_gray.jpg", cv_roi_gray);
-
- cv::Mat rgba_mat;
- cv::cvtColor(src_image, rgba_mat, CV_BGR2RGBA);
- bool ret = false;
- LiteMat lite_mat_gray;
- ret =
- InitFromPixel(rgba_mat.data, LPixelType::RGBA2GRAY, LDataType::UINT8, rgba_mat.cols, rgba_mat.rows, lite_mat_gray);
- EXPECT_TRUE(ret);
- LiteMat lite_roi_gray;
-
- ret = lite_mat_gray.GetROI(500, 500, 3000, 1500, lite_roi_gray);
- EXPECT_TRUE(ret);
-
- LiteMat lite_roi_gray_save(3000, 1500, lite_roi_gray.channel_, LDataType::UINT8);
-
- for (size_t i = 0; i < lite_roi_gray.height_; i++) {
- const unsigned char *ptr = lite_roi_gray.ptr<unsigned char>(i);
- size_t image_size = lite_roi_gray.width_ * lite_roi_gray.channel_ * sizeof(unsigned char);
- unsigned char *dst_ptr = (unsigned char *)lite_roi_gray_save.data_ptr_ + image_size * i;
- (void)memcpy(dst_ptr, ptr, image_size);
- }
-
- cv::Mat dst_imageR(lite_roi_gray_save.height_, lite_roi_gray_save.width_, CV_8UC1, lite_roi_gray_save.data_ptr_);
- cv::imwrite("./lite_roi.jpg", dst_imageR);
- }
-
- // warp
- TEST_F(MindDataImageProcess, testWarpAffineBGR) {
- std::string filename = "data/dataset/apple.jpg";
- cv::Mat src_image = cv::imread(filename, cv::ImreadModes::IMREAD_COLOR);
- cv::Point2f srcTri[3];
- cv::Point2f dstTri[3];
- srcTri[0] = cv::Point2f(0, 0);
- srcTri[1] = cv::Point2f(src_image.cols - 1, 0);
- srcTri[2] = cv::Point2f(0, src_image.rows - 1);
-
- dstTri[0] = cv::Point2f(src_image.cols * 0.0, src_image.rows * 0.33);
- dstTri[1] = cv::Point2f(src_image.cols * 0.85, src_image.rows * 0.25);
- dstTri[2] = cv::Point2f(src_image.cols * 0.15, src_image.rows * 0.7);
-
- cv::Mat warp_mat = cv::getAffineTransform(srcTri, dstTri);
- ;
- cv::Mat warp_dstImage;
- cv::warpAffine(src_image, warp_dstImage, warp_mat, warp_dstImage.size());
- cv::imwrite("./warpAffine_cv_bgr.png", warp_dstImage);
-
- bool ret = false;
- LiteMat lite_mat_bgr;
- ret = InitFromPixel(src_image.data, LPixelType::BGR, LDataType::UINT8, src_image.cols, src_image.rows, lite_mat_bgr);
- EXPECT_TRUE(ret);
- double *mat_ptr = warp_mat.ptr<double>(0);
- LiteMat lite_M(3, 2, 1, mat_ptr, LDataType::DOUBLE);
-
- LiteMat lite_warp;
- std::vector<uint8_t> borderValues;
- borderValues.push_back(0);
- borderValues.push_back(0);
- borderValues.push_back(0);
- ret = WarpAffineBilinear(lite_mat_bgr, lite_warp, lite_M, lite_mat_bgr.width_, lite_mat_bgr.height_,
- PADD_BORDER_CONSTANT, borderValues);
- EXPECT_TRUE(ret);
-
- cv::Mat dst_imageR(lite_warp.height_, lite_warp.width_, CV_8UC3, lite_warp.data_ptr_);
- cv::imwrite("./warpAffine_lite_bgr.png", dst_imageR);
- }
-
- TEST_F(MindDataImageProcess, testWarpAffineBGRScale) {
- std::string filename = "data/dataset/apple.jpg";
- cv::Mat src_image = cv::imread(filename, cv::ImreadModes::IMREAD_COLOR);
- cv::Point2f srcTri[3];
- cv::Point2f dstTri[3];
- srcTri[0] = cv::Point2f(10, 20);
- srcTri[1] = cv::Point2f(src_image.cols - 1 - 100, 0);
- srcTri[2] = cv::Point2f(0, src_image.rows - 1 - 300);
-
- dstTri[0] = cv::Point2f(src_image.cols * 0.22, src_image.rows * 0.33);
- dstTri[1] = cv::Point2f(src_image.cols * 0.87, src_image.rows * 0.75);
- dstTri[2] = cv::Point2f(src_image.cols * 0.35, src_image.rows * 0.37);
-
- cv::Mat warp_mat = cv::getAffineTransform(srcTri, dstTri);
- ;
- cv::Mat warp_dstImage;
- cv::warpAffine(src_image, warp_dstImage, warp_mat, warp_dstImage.size());
- cv::imwrite("./warpAffine_cv_bgr_scale.png", warp_dstImage);
-
- bool ret = false;
- LiteMat lite_mat_bgr;
- ret = InitFromPixel(src_image.data, LPixelType::BGR, LDataType::UINT8, src_image.cols, src_image.rows, lite_mat_bgr);
- EXPECT_TRUE(ret);
- double *mat_ptr = warp_mat.ptr<double>(0);
- LiteMat lite_M(3, 2, 1, mat_ptr, LDataType::DOUBLE);
-
- LiteMat lite_warp;
- std::vector<uint8_t> borderValues;
- borderValues.push_back(0);
- borderValues.push_back(0);
- borderValues.push_back(0);
- ret = WarpAffineBilinear(lite_mat_bgr, lite_warp, lite_M, lite_mat_bgr.width_, lite_mat_bgr.height_,
- PADD_BORDER_CONSTANT, borderValues);
- EXPECT_TRUE(ret);
-
- cv::Mat dst_imageR(lite_warp.height_, lite_warp.width_, CV_8UC3, lite_warp.data_ptr_);
- cv::imwrite("./warpAffine_lite_bgr_scale.png", dst_imageR);
- }
-
- TEST_F(MindDataImageProcess, testWarpAffineBGRResize) {
- std::string filename = "data/dataset/apple.jpg";
- cv::Mat src_image = cv::imread(filename, cv::ImreadModes::IMREAD_COLOR);
- cv::Point2f srcTri[3];
- cv::Point2f dstTri[3];
- srcTri[0] = cv::Point2f(10, 20);
- srcTri[1] = cv::Point2f(src_image.cols - 1 - 100, 0);
- srcTri[2] = cv::Point2f(0, src_image.rows - 1 - 300);
-
- dstTri[0] = cv::Point2f(src_image.cols * 0.22, src_image.rows * 0.33);
- dstTri[1] = cv::Point2f(src_image.cols * 0.87, src_image.rows * 0.75);
- dstTri[2] = cv::Point2f(src_image.cols * 0.35, src_image.rows * 0.37);
-
- cv::Mat warp_mat = cv::getAffineTransform(srcTri, dstTri);
- ;
- cv::Mat warp_dstImage;
- cv::warpAffine(src_image, warp_dstImage, warp_mat, cv::Size(src_image.cols + 200, src_image.rows - 300));
- cv::imwrite("./warpAffine_cv_bgr_resize.png", warp_dstImage);
-
- bool ret = false;
- LiteMat lite_mat_bgr;
- ret = InitFromPixel(src_image.data, LPixelType::BGR, LDataType::UINT8, src_image.cols, src_image.rows, lite_mat_bgr);
- EXPECT_TRUE(ret);
- double *mat_ptr = warp_mat.ptr<double>(0);
- LiteMat lite_M(3, 2, 1, mat_ptr, LDataType::DOUBLE);
-
- LiteMat lite_warp;
- std::vector<uint8_t> borderValues;
- borderValues.push_back(0);
- borderValues.push_back(0);
- borderValues.push_back(0);
- ret = WarpAffineBilinear(lite_mat_bgr, lite_warp, lite_M, lite_mat_bgr.width_ + 200, lite_mat_bgr.height_ - 300,
- PADD_BORDER_CONSTANT, borderValues);
- EXPECT_TRUE(ret);
-
- cv::Mat dst_imageR(lite_warp.height_, lite_warp.width_, CV_8UC3, lite_warp.data_ptr_);
- cv::imwrite("./warpAffine_lite_bgr_resize.png", dst_imageR);
- }
-
- TEST_F(MindDataImageProcess, testWarpAffineGray) {
- std::string filename = "data/dataset/apple.jpg";
- cv::Mat src_image = cv::imread(filename, cv::ImreadModes::IMREAD_COLOR);
-
- cv::Mat gray_image;
- cv::cvtColor(src_image, gray_image, CV_BGR2GRAY);
-
- cv::Point2f srcTri[3];
- cv::Point2f dstTri[3];
- srcTri[0] = cv::Point2f(0, 0);
- srcTri[1] = cv::Point2f(src_image.cols - 1, 0);
- srcTri[2] = cv::Point2f(0, src_image.rows - 1);
-
- dstTri[0] = cv::Point2f(src_image.cols * 0.0, src_image.rows * 0.33);
- dstTri[1] = cv::Point2f(src_image.cols * 0.85, src_image.rows * 0.25);
- dstTri[2] = cv::Point2f(src_image.cols * 0.15, src_image.rows * 0.7);
-
- cv::Mat warp_mat = cv::getAffineTransform(srcTri, dstTri);
- ;
- cv::Mat warp_gray_dstImage;
- cv::warpAffine(gray_image, warp_gray_dstImage, warp_mat, cv::Size(src_image.cols + 200, src_image.rows - 300));
- cv::imwrite("./warpAffine_cv_gray.png", warp_gray_dstImage);
-
- cv::Mat rgba_mat;
- cv::cvtColor(src_image, rgba_mat, CV_BGR2RGBA);
- bool ret = false;
- LiteMat lite_mat_gray;
- ret =
- InitFromPixel(rgba_mat.data, LPixelType::RGBA2GRAY, LDataType::UINT8, rgba_mat.cols, rgba_mat.rows, lite_mat_gray);
- EXPECT_TRUE(ret);
- double *mat_ptr = warp_mat.ptr<double>(0);
- LiteMat lite_M(3, 2, 1, mat_ptr, LDataType::DOUBLE);
-
- LiteMat lite_warp;
- std::vector<uint8_t> borderValues;
- borderValues.push_back(0);
- ret = WarpAffineBilinear(lite_mat_gray, lite_warp, lite_M, lite_mat_gray.width_ + 200, lite_mat_gray.height_ - 300,
- PADD_BORDER_CONSTANT, borderValues);
- EXPECT_TRUE(ret);
-
- cv::Mat dst_imageR(lite_warp.height_, lite_warp.width_, CV_8UC1, lite_warp.data_ptr_);
- cv::imwrite("./warpAffine_lite_gray.png", dst_imageR);
- }
-
- TEST_F(MindDataImageProcess, testWarpPerspectiveBGRResize) {
- std::string filename = "data/dataset/apple.jpg";
- cv::Mat src_image = cv::imread(filename, cv::ImreadModes::IMREAD_COLOR);
- cv::Point2f srcQuad[4], dstQuad[4];
- srcQuad[0].x = 0;
- srcQuad[0].y = 0;
- srcQuad[1].x = src_image.cols - 1.;
- srcQuad[1].y = 0;
- srcQuad[2].x = 0;
- srcQuad[2].y = src_image.rows - 1;
- srcQuad[3].x = src_image.cols - 1;
- srcQuad[3].y = src_image.rows - 1;
-
- dstQuad[0].x = src_image.cols * 0.05;
- dstQuad[0].y = src_image.rows * 0.33;
- dstQuad[1].x = src_image.cols * 0.9;
- dstQuad[1].y = src_image.rows * 0.25;
- dstQuad[2].x = src_image.cols * 0.2;
- dstQuad[2].y = src_image.rows * 0.7;
- dstQuad[3].x = src_image.cols * 0.8;
- dstQuad[3].y = src_image.rows * 0.9;
-
- cv::Mat ptran = cv::getPerspectiveTransform(srcQuad, dstQuad, cv::DECOMP_SVD);
- cv::Mat warp_dstImage;
- cv::warpPerspective(src_image, warp_dstImage, ptran, cv::Size(src_image.cols + 200, src_image.rows - 300));
- cv::imwrite("./warpPerspective_cv_bgr.png", warp_dstImage);
-
- bool ret = false;
- LiteMat lite_mat_bgr;
- ret = InitFromPixel(src_image.data, LPixelType::BGR, LDataType::UINT8, src_image.cols, src_image.rows, lite_mat_bgr);
- EXPECT_TRUE(ret);
- double *mat_ptr = ptran.ptr<double>(0);
- LiteMat lite_M(3, 3, 1, mat_ptr, LDataType::DOUBLE);
-
- LiteMat lite_warp;
- std::vector<uint8_t> borderValues;
- borderValues.push_back(0);
- borderValues.push_back(0);
- borderValues.push_back(0);
- ret = WarpPerspectiveBilinear(lite_mat_bgr, lite_warp, lite_M, lite_mat_bgr.width_ + 200, lite_mat_bgr.height_ - 300,
- PADD_BORDER_CONSTANT, borderValues);
- EXPECT_TRUE(ret);
-
- cv::Mat dst_imageR(lite_warp.height_, lite_warp.width_, CV_8UC3, lite_warp.data_ptr_);
- cv::imwrite("./warpPerspective_lite_bgr.png", dst_imageR);
- }
-
- TEST_F(MindDataImageProcess, testWarpPerspectiveGrayResize) {
- std::string filename = "data/dataset/apple.jpg";
- cv::Mat src_image = cv::imread(filename, cv::ImreadModes::IMREAD_COLOR);
-
- cv::Mat gray_image;
- cv::cvtColor(src_image, gray_image, CV_BGR2GRAY);
-
- cv::Point2f srcQuad[4], dstQuad[4];
- srcQuad[0].x = 0;
- srcQuad[0].y = 0;
- srcQuad[1].x = src_image.cols - 1.;
- srcQuad[1].y = 0;
- srcQuad[2].x = 0;
- srcQuad[2].y = src_image.rows - 1;
- srcQuad[3].x = src_image.cols - 1;
- srcQuad[3].y = src_image.rows - 1;
-
- dstQuad[0].x = src_image.cols * 0.05;
- dstQuad[0].y = src_image.rows * 0.33;
- dstQuad[1].x = src_image.cols * 0.9;
- dstQuad[1].y = src_image.rows * 0.25;
- dstQuad[2].x = src_image.cols * 0.2;
- dstQuad[2].y = src_image.rows * 0.7;
- dstQuad[3].x = src_image.cols * 0.8;
- dstQuad[3].y = src_image.rows * 0.9;
-
- cv::Mat ptran = cv::getPerspectiveTransform(srcQuad, dstQuad, cv::DECOMP_SVD);
- cv::Mat warp_dstImage;
- cv::warpPerspective(gray_image, warp_dstImage, ptran, cv::Size(gray_image.cols + 200, gray_image.rows - 300));
- cv::imwrite("./warpPerspective_cv_gray.png", warp_dstImage);
-
- cv::Mat rgba_mat;
- cv::cvtColor(src_image, rgba_mat, CV_BGR2RGBA);
- bool ret = false;
- LiteMat lite_mat_gray;
- ret =
- InitFromPixel(rgba_mat.data, LPixelType::RGBA2GRAY, LDataType::UINT8, rgba_mat.cols, rgba_mat.rows, lite_mat_gray);
- EXPECT_TRUE(ret);
- double *mat_ptr = ptran.ptr<double>(0);
- LiteMat lite_M(3, 3, 1, mat_ptr, LDataType::DOUBLE);
-
- LiteMat lite_warp;
- std::vector<uint8_t> borderValues;
- borderValues.push_back(0);
- ret = WarpPerspectiveBilinear(lite_mat_gray, lite_warp, lite_M, lite_mat_gray.width_ + 200,
- lite_mat_gray.height_ - 300, PADD_BORDER_CONSTANT, borderValues);
- EXPECT_TRUE(ret);
-
- cv::Mat dst_imageR(lite_warp.height_, lite_warp.width_, CV_8UC1, lite_warp.data_ptr_);
- cv::imwrite("./warpPerspective_lite_gray.png", dst_imageR);
- }
-
- TEST_F(MindDataImageProcess, testGetRotationMatrix2D) {
- 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;
-
- LiteMat M;
- bool ret = false;
- ret = GetRotationMatrix2D(1.0f, 2.0f, angle, scale, M);
- EXPECT_TRUE(ret);
- AccuracyComparison(expect_matrix, M);
- }
-
- 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<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)};
-
- LiteMat M;
- bool ret = false;
- ret = GetPerspectiveTransform(src, dst, M);
- EXPECT_TRUE(ret);
- AccuracyComparison(expect_matrix, M);
- }
-
- TEST_F(MindDataImageProcess, testGetPerspectiveTransformFail) {
- std::vector<Point> src = {Point(165, 270), Point(835, 270), Point(360, 125), Point(615, 125)};
- std::vector<Point> dst = {Point(100, 100), Point(500, 30)};
-
- LiteMat M;
- bool ret = GetPerspectiveTransform(src, dst, M);
- EXPECT_FALSE(ret);
-
- std::vector<Point> src1 = {Point(360, 125), Point(615, 125)};
- std::vector<Point> dst2 = {Point(165, 270), Point(835, 270), Point(100, 100), Point(500, 30)};
-
- LiteMat M1;
- bool ret1 = GetPerspectiveTransform(src, dst, M1);
- EXPECT_FALSE(ret1);
- }
-
- 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);
- }
-
- TEST_F(MindDataImageProcess, testGetAffineTransformFail) {
- std::vector<Point> src = {Point(50, 50), Point(200, 50)};
- std::vector<Point> dst = {Point(40, 40), Point(100, 40), Point(50, 90)};
-
- LiteMat M;
- bool ret = GetAffineTransform(src, dst, M);
- EXPECT_FALSE(ret);
-
- std::vector<Point> src1 = {Point(50, 50), Point(200, 50), Point(50, 200)};
- std::vector<Point> dst1 = {Point(40, 40), Point(100, 40)};
-
- LiteMat M1;
- bool ret1 = GetAffineTransform(src1, dst1, M1);
- EXPECT_FALSE(ret1);
- }
-
- TEST_F(MindDataImageProcess, TestConv2D8U) {
- LiteMat lite_mat_src;
- lite_mat_src.Init(3, 3, 1, LDataType::UINT8);
- uint8_t *src_ptr = lite_mat_src;
- for (int i = 0; i < 9; i++) {
- src_ptr[i] = i % 3;
- }
- LiteMat kernel;
- kernel.Init(3, 3, 1, LDataType::FLOAT32);
- float *kernel_ptr = kernel;
- for (int i = 0; i < 9; i++) {
- kernel_ptr[i] = i % 2;
- }
- LiteMat lite_mat_dst;
- bool ret = Conv2D(lite_mat_src, kernel, lite_mat_dst, LDataType::UINT8);
- ASSERT_TRUE(ret == true);
-
- std::vector<uint8_t> expected_result = {2, 4, 6, 2, 4, 6, 2, 4, 6};
-
- size_t total_size = lite_mat_dst.height_ * lite_mat_dst.width_ * lite_mat_dst.channel_;
- float distance = 0.0f;
- for (size_t i = 0; i < total_size; i++) {
- distance += pow(((uint8_t *)lite_mat_dst)[i] - expected_result[i], 2);
- }
- distance = sqrt(distance / total_size);
- EXPECT_EQ(distance, 0.0f);
- }
-
- TEST_F(MindDataImageProcess, TestConv2D32F) {
- LiteMat lite_mat_src;
- lite_mat_src.Init(2, 2, 1, LDataType::FLOAT32);
- float *src_ptr = lite_mat_src;
- for (int i = 0; i < 4; i++) {
- src_ptr[i] = static_cast<float>(i) / 2;
- }
- LiteMat kernel;
- kernel.Init(2, 2, 1, LDataType::FLOAT32);
- float *kernel_ptr = kernel;
- for (int i = 0; i < 4; i++) {
- kernel_ptr[i] = static_cast<float>(i);
- }
- LiteMat lite_mat_dst;
- bool ret = Conv2D(lite_mat_src, kernel, lite_mat_dst, LDataType::FLOAT32);
- ASSERT_TRUE(ret == true);
-
- std::vector<float> expected_result = {2.f, 3.f, 6.f, 7.f};
-
- size_t total_size = lite_mat_dst.height_ * lite_mat_dst.width_ * lite_mat_dst.channel_;
- float distance = 0.0f;
- for (size_t i = 0; i < total_size; i++) {
- distance += pow(((float *)lite_mat_dst)[i] - expected_result[i], 2);
- }
- distance = sqrt(distance / total_size);
- EXPECT_EQ(distance, 0.0f);
- }
-
- TEST_F(MindDataImageProcess, TestGaussianBlurSize35) {
- std::string filename = "data/dataset/apple.jpg";
- cv::Mat src_image = cv::imread(filename, cv::ImreadModes::IMREAD_COLOR);
-
- cv::Mat dst_image;
- cv::GaussianBlur(src_image, dst_image, cv::Size(3, 5), 3, 3);
-
- cv::Mat rgba_mat;
- cv::cvtColor(src_image, rgba_mat, CV_BGR2RGBA);
-
- LiteMat lite_mat_bgr;
- bool ret =
- InitFromPixel(rgba_mat.data, LPixelType::RGBA2BGR, LDataType::UINT8, rgba_mat.cols, rgba_mat.rows, lite_mat_bgr);
- ASSERT_TRUE(ret == true);
-
- LiteMat lite_mat_dst;
- ret = GaussianBlur(lite_mat_bgr, lite_mat_dst, {3, 5}, 3, 3);
- ASSERT_TRUE(ret == true);
-
- size_t total_size = lite_mat_dst.height_ * lite_mat_dst.width_ * lite_mat_dst.channel_;
- double distance = 0.0f;
- for (size_t i = 0; i < total_size; i++) {
- distance += pow((uint8_t)dst_image.data[i] - ((uint8_t *)lite_mat_dst)[i], 2);
- }
- distance = sqrt(distance / total_size);
- EXPECT_LE(distance, 1.0f);
- }
-
- TEST_F(MindDataImageProcess, TestGaussianBlurSize13) {
- std::string filename = "data/dataset/apple.jpg";
- cv::Mat src_image = cv::imread(filename, cv::ImreadModes::IMREAD_COLOR);
-
- cv::Mat dst_image;
- cv::GaussianBlur(src_image, dst_image, cv::Size(1, 3), 3);
-
- cv::Mat rgba_mat;
- cv::cvtColor(src_image, rgba_mat, CV_BGR2RGBA);
-
- LiteMat lite_mat_bgr;
- bool ret =
- InitFromPixel(rgba_mat.data, LPixelType::RGBA2BGR, LDataType::UINT8, rgba_mat.cols, rgba_mat.rows, lite_mat_bgr);
- ASSERT_TRUE(ret == true);
-
- LiteMat lite_mat_dst;
- ret = GaussianBlur(lite_mat_bgr, lite_mat_dst, {1, 3}, 3);
- ASSERT_TRUE(ret == true);
-
- size_t total_size = lite_mat_dst.height_ * lite_mat_dst.width_ * lite_mat_dst.channel_;
- double distance = 0.0f;
- for (size_t i = 0; i < total_size; i++) {
- distance += pow((uint8_t)dst_image.data[i] - ((uint8_t *)lite_mat_dst)[i], 2);
- }
- distance = sqrt(distance / total_size);
- EXPECT_LE(distance, 1.0f);
- }
-
- TEST_F(MindDataImageProcess, TestGaussianBlurInvalidParams) {
- std::string filename = "data/dataset/apple.jpg";
- cv::Mat src_image = cv::imread(filename, cv::ImreadModes::IMREAD_COLOR);
- cv::Mat rgba_mat;
- cv::cvtColor(src_image, rgba_mat, CV_BGR2RGBA);
-
- LiteMat lite_mat_bgr;
- bool ret =
- InitFromPixel(rgba_mat.data, LPixelType::RGBA2BGR, LDataType::UINT8, rgba_mat.cols, rgba_mat.rows, lite_mat_bgr);
- ASSERT_TRUE(ret == true);
-
- LiteMat lite_mat_dst;
-
- // even size
- ret = GaussianBlur(lite_mat_bgr, lite_mat_dst, {3, 4}, 3);
- ASSERT_TRUE(ret == false);
-
- // ksize.size() != 2
- ret = GaussianBlur(lite_mat_bgr, lite_mat_dst, {3, 4, 5}, 3);
- ASSERT_TRUE(ret == false);
-
- // size less or equal to 0
- ret = GaussianBlur(lite_mat_bgr, lite_mat_dst, {0, 3}, 3);
- ASSERT_TRUE(ret == false);
-
- // sigmaX less or equal to 0
- ret = GaussianBlur(lite_mat_bgr, lite_mat_dst, {3, 3}, 0);
- ASSERT_TRUE(ret == false);
- }
-
- TEST_F(MindDataImageProcess, TestCannySize3) {
- std::string filename = "data/dataset/apple.jpg";
- cv::Mat src_image = cv::imread(filename, cv::ImreadModes::IMREAD_COLOR);
- cv::Mat gray_image;
- cv::cvtColor(src_image, gray_image, CV_BGR2GRAY);
- cv::Mat dst_image;
- cv::Canny(gray_image, dst_image, 100, 200, 3);
-
- cv::Mat rgba_mat;
- cv::cvtColor(src_image, rgba_mat, CV_BGR2RGBA);
- bool ret = false;
- LiteMat lite_mat_gray;
- ret =
- InitFromPixel(rgba_mat.data, LPixelType::RGBA2GRAY, LDataType::UINT8, rgba_mat.cols, rgba_mat.rows, lite_mat_gray);
- ASSERT_TRUE(ret == true);
-
- LiteMat lite_mat_dst;
- ret = Canny(lite_mat_gray, lite_mat_dst, 100, 200, 3);
- ASSERT_TRUE(ret == true);
-
- int total_size = lite_mat_dst.height_ * lite_mat_dst.width_ * lite_mat_dst.channel_;
- double distance = 0.0f;
- for (int i = 0; i < total_size; i++) {
- distance += pow((uint8_t)dst_image.data[i] - ((uint8_t *)lite_mat_dst)[i], 2);
- }
- distance = sqrt(distance / total_size);
- EXPECT_EQ(distance, 0.0f);
- }
-
- TEST_F(MindDataImageProcess, TestCannySize5) {
- std::string filename = "data/dataset/apple.jpg";
- cv::Mat src_image = cv::imread(filename, cv::ImreadModes::IMREAD_COLOR);
- cv::Mat gray_image;
- cv::cvtColor(src_image, gray_image, CV_BGR2GRAY);
- cv::Mat dst_image;
- cv::Canny(gray_image, dst_image, 200, 300, 5);
-
- cv::Mat rgba_mat;
- cv::cvtColor(src_image, rgba_mat, CV_BGR2RGBA);
- bool ret = false;
- LiteMat lite_mat_gray;
- ret =
- InitFromPixel(rgba_mat.data, LPixelType::RGBA2GRAY, LDataType::UINT8, rgba_mat.cols, rgba_mat.rows, lite_mat_gray);
- ASSERT_TRUE(ret == true);
-
- LiteMat lite_mat_dst;
- ret = Canny(lite_mat_gray, lite_mat_dst, 200, 300, 5);
- ASSERT_TRUE(ret == true);
-
- int total_size = lite_mat_dst.height_ * lite_mat_dst.width_ * lite_mat_dst.channel_;
- double distance = 0.0f;
- for (int i = 0; i < total_size; i++) {
- distance += pow((uint8_t)dst_image.data[i] - ((uint8_t *)lite_mat_dst)[i], 2);
- }
- distance = sqrt(distance / total_size);
- EXPECT_EQ(distance, 0.0f);
- }
-
- TEST_F(MindDataImageProcess, TestCannySize7) {
- std::string filename = "data/dataset/apple.jpg";
- cv::Mat src_image = cv::imread(filename, cv::ImreadModes::IMREAD_COLOR);
- cv::Mat gray_image;
- cv::cvtColor(src_image, gray_image, CV_BGR2GRAY);
- cv::Mat dst_image;
- cv::Canny(gray_image, dst_image, 110, 220, 7);
-
- cv::Mat rgba_mat;
- cv::cvtColor(src_image, rgba_mat, CV_BGR2RGBA);
- bool ret = false;
- LiteMat lite_mat_gray;
- ret =
- InitFromPixel(rgba_mat.data, LPixelType::RGBA2GRAY, LDataType::UINT8, rgba_mat.cols, rgba_mat.rows, lite_mat_gray);
- ASSERT_TRUE(ret == true);
-
- LiteMat lite_mat_dst;
- ret = Canny(lite_mat_gray, lite_mat_dst, 110, 220, 7);
- ASSERT_TRUE(ret == true);
-
- int total_size = lite_mat_dst.height_ * lite_mat_dst.width_ * lite_mat_dst.channel_;
- double distance = 0.0f;
- for (int i = 0; i < total_size; i++) {
- distance += pow((uint8_t)dst_image.data[i] - ((uint8_t *)lite_mat_dst)[i], 2);
- }
- distance = sqrt(distance / total_size);
- EXPECT_EQ(distance, 0.0f);
- }
-
- TEST_F(MindDataImageProcess, TestCannyL2) {
- std::string filename = "data/dataset/apple.jpg";
- cv::Mat src_image = cv::imread(filename, cv::ImreadModes::IMREAD_COLOR);
- cv::Mat gray_image;
- cv::cvtColor(src_image, gray_image, CV_BGR2GRAY);
- cv::Mat dst_image;
- cv::Canny(gray_image, dst_image, 50, 150, 3, true);
-
- cv::Mat rgba_mat;
- cv::cvtColor(src_image, rgba_mat, CV_BGR2RGBA);
- bool ret = false;
- LiteMat lite_mat_gray;
- ret =
- InitFromPixel(rgba_mat.data, LPixelType::RGBA2GRAY, LDataType::UINT8, rgba_mat.cols, rgba_mat.rows, lite_mat_gray);
- ASSERT_TRUE(ret == true);
-
- LiteMat lite_mat_dst;
- ret = Canny(lite_mat_gray, lite_mat_dst, 50, 150, 3, true);
- ASSERT_TRUE(ret == true);
-
- int total_size = lite_mat_dst.height_ * lite_mat_dst.width_ * lite_mat_dst.channel_;
- double distance = 0.0f;
- for (int i = 0; i < total_size; i++) {
- distance += pow((uint8_t)dst_image.data[i] - ((uint8_t *)lite_mat_dst)[i], 2);
- }
-
- distance = sqrt(distance / total_size);
- EXPECT_EQ(distance, 0.0f);
- }
-
- TEST_F(MindDataImageProcess, TestCannyInvalidParams) {
- std::string filename = "data/dataset/apple.jpg";
- cv::Mat src_image = cv::imread(filename, cv::ImreadModes::IMREAD_COLOR);
-
- cv::Mat rgba_mat;
- cv::cvtColor(src_image, rgba_mat, CV_BGR2RGBA);
-
- bool ret = false;
- LiteMat lite_mat_bgr;
- ret =
- InitFromPixel(rgba_mat.data, LPixelType::RGBA2BGR, LDataType::UINT8, rgba_mat.cols, rgba_mat.rows, lite_mat_bgr);
- ASSERT_TRUE(ret == true);
-
- // channel is not 1
- LiteMat lite_mat_dst;
- ret = Canny(lite_mat_bgr, lite_mat_dst, 70, 210, 3);
- ASSERT_TRUE(ret == false);
-
- LiteMat lite_mat_gray;
- ret =
- InitFromPixel(rgba_mat.data, LPixelType::RGBA2GRAY, LDataType::UINT8, rgba_mat.cols, rgba_mat.rows, lite_mat_gray);
- ASSERT_TRUE(ret == true);
-
- // low_thresh less than 0
- ret = Canny(lite_mat_gray, lite_mat_dst, -5, 230, 3);
- ASSERT_TRUE(ret == false);
-
- // high_thresh less than low_thresh
- ret = Canny(lite_mat_gray, lite_mat_dst, 250, 130, 3);
- ASSERT_TRUE(ret == false);
-
- // even size
- ret = Canny(lite_mat_gray, lite_mat_dst, 60, 180, 4);
- ASSERT_TRUE(ret == false);
-
- // size less than 3 or large than 7
- ret = Canny(lite_mat_gray, lite_mat_dst, 10, 190, 9);
- ASSERT_TRUE(ret == false);
- }
-
- TEST_F(MindDataImageProcess, TestSobel) {
- std::string filename = "data/dataset/apple.jpg";
- cv::Mat src_image = cv::imread(filename, cv::ImreadModes::IMREAD_COLOR);
- cv::Mat gray_image;
- cv::cvtColor(src_image, gray_image, CV_BGR2GRAY);
-
- cv::Mat sobel_image_x;
- cv::Mat sobel_image_y;
- cv::Sobel(gray_image, sobel_image_x, CV_32F, 1, 0, 3, 1, 0, cv::BORDER_REPLICATE);
- cv::Sobel(gray_image, sobel_image_y, CV_32F, 0, 1, 3, 1, 0, cv::BORDER_REPLICATE);
-
- cv::Mat sobel_cv_x, sobel_cv_y;
- sobel_image_x.convertTo(sobel_cv_x, CV_8UC1);
- sobel_image_y.convertTo(sobel_cv_y, CV_8UC1);
-
- cv::Mat rgba_mat;
- cv::cvtColor(src_image, rgba_mat, CV_BGR2RGBA);
- bool ret = false;
- LiteMat lite_mat_gray;
- ret =
- InitFromPixel(rgba_mat.data, LPixelType::RGBA2GRAY, LDataType::UINT8, rgba_mat.cols, rgba_mat.rows, lite_mat_gray);
- ASSERT_TRUE(ret == true);
- LiteMat lite_mat_x;
- LiteMat lite_mat_y;
- Sobel(lite_mat_gray, lite_mat_x, 1, 0, 3, 1, PaddBorderType::PADD_BORDER_REPLICATE);
- Sobel(lite_mat_gray, lite_mat_y, 0, 1, 3, 1, PaddBorderType::PADD_BORDER_REPLICATE);
- ASSERT_TRUE(ret == true);
-
- cv::Mat dst_imageX(lite_mat_x.height_, lite_mat_x.width_, CV_32FC1, lite_mat_x.data_ptr_);
- cv::Mat dst_imageY(lite_mat_y.height_, lite_mat_y.width_, CV_32FC1, lite_mat_y.data_ptr_);
- cv::Mat sobel_ms_x, sobel_ms_y;
- dst_imageX.convertTo(sobel_ms_x, CV_8UC1);
- dst_imageY.convertTo(sobel_ms_y, CV_8UC1);
-
- size_t total_size = lite_mat_x.height_ * lite_mat_x.width_ * lite_mat_x.channel_;
- float distance_x = 0.0f, distance_y = 0.0f;
- for (int i = 0; i < total_size; i++) {
- distance_x += pow((uint8_t)sobel_cv_x.data[i] - (uint8_t)sobel_ms_x.data[i], 2);
- distance_y += pow((uint8_t)sobel_cv_y.data[i] - (uint8_t)sobel_ms_y.data[i], 2);
- }
- distance_x = sqrt(distance_x / total_size);
- distance_y = sqrt(distance_y / total_size);
- EXPECT_EQ(distance_x, 0.0f);
- EXPECT_EQ(distance_y, 0.0f);
- }
-
- TEST_F(MindDataImageProcess, TestSobelFlag) {
- std::string filename = "data/dataset/apple.jpg";
- cv::Mat src_image = cv::imread(filename, cv::ImreadModes::IMREAD_COLOR);
- cv::Mat gray_image;
- cv::cvtColor(src_image, gray_image, CV_BGR2GRAY);
-
- cv::Mat sobel_image_x;
- cv::Sobel(gray_image, sobel_image_x, CV_32F, 3, 1, 5, 1, 0, cv::BORDER_REPLICATE);
-
- cv::Mat sobel_cv_x;
- sobel_image_x.convertTo(sobel_cv_x, CV_8UC1);
-
- cv::Mat rgba_mat;
- cv::cvtColor(src_image, rgba_mat, CV_BGR2RGBA);
- bool ret = false;
- LiteMat lite_mat_gray;
- ret =
- InitFromPixel(rgba_mat.data, LPixelType::RGBA2GRAY, LDataType::UINT8, rgba_mat.cols, rgba_mat.rows, lite_mat_gray);
- ASSERT_TRUE(ret == true);
- LiteMat lite_mat_x;
- Sobel(lite_mat_gray, lite_mat_x, 3, 1, 5, 1, PaddBorderType::PADD_BORDER_REPLICATE);
- ASSERT_TRUE(ret == true);
-
- cv::Mat dst_imageX(lite_mat_x.height_, lite_mat_x.width_, CV_32FC1, lite_mat_x.data_ptr_);
- cv::Mat sobel_ms_x;
- dst_imageX.convertTo(sobel_ms_x, CV_8UC1);
-
- size_t total_size = lite_mat_x.height_ * lite_mat_x.width_ * lite_mat_x.channel_;
- float distance_x = 0.0f;
- for (int i = 0; i < total_size; i++) {
- distance_x += pow((uint8_t)sobel_cv_x.data[i] - (uint8_t)sobel_ms_x.data[i], 2);
- }
- distance_x = sqrt(distance_x / total_size);
- EXPECT_EQ(distance_x, 0.0f);
- }
-
- TEST_F(MindDataImageProcess, testConvertRgbToGray) {
- 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);
-
- 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, image.cols, image.rows, lite_mat_gray);
- ASSERT_TRUE(ret == true);
-
- cv::Mat dst_image(lite_mat_gray.height_, lite_mat_gray.width_, CV_8UC1, lite_mat_gray.data_ptr_);
- cv::imwrite("./mindspore_image.jpg", dst_image);
- 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);
-
- LiteMat lite_mat_rgb;
- lite_mat_rgb.Init(image.cols, image.rows, image.channels(), image.data, LDataType::UINT8);
- 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 == true);
- 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);
- }
|