mv3d: update mv_depth_test_suite 27/282527/4
authorTae-Young Chung <ty83.chung@samsung.com>
Wed, 5 Oct 2022 05:31:05 +0000 (14:31 +0900)
committerTae-Young Chung <ty83.chung@samsung.com>
Wed, 5 Oct 2022 09:33:23 +0000 (09:33 +0000)
[Version]: 0.23.35-0
[Issue type]: update test

- apply coding convention
- update global functions to static ones
- remove unused variables

Change-Id: Ibf8cb916cda98a9656e99632add7897f5c8f2947
Signed-off-by: Tae-Young Chung <ty83.chung@samsung.com>
test/testsuites/mv3d/depth_test_suite.cpp

index bee2201..100e976 100644 (file)
 #define MAX_STRING_LENGTH 1024
 #define ARRAY_SIZE(x) (sizeof((x)) / sizeof((x)[0]))
 
-#define __max(a,b)  (((a) > (b)) ? (a) : (b))
-#define __min(a,b)  (((a) < (b)) ? (a) : (b))
+#define __max(a, b) (((a) > (b)) ? (a) : (b))
+#define __min(a, b) (((a) < (b)) ? (a) : (b))
 
 /* There are calib.txt, im0.png, and im1.png in each dataset directories.*/
-static const char* dataset[] = {
-       "Adirondack",
-       "Jadeplant",
-       "Motorcycle",
-       "Piano",
-       "Pipes",
-       "Playroom",
-       "Playtable",
-       "Recycle",
-       "Shelves",
-       "Vintage",
-       "Backpack",
-       "Bicycle1",
-       "Cable",
-       "Classroom1",
-       "Couch",
-       "Flowers",
-       "Mask",
-       "Shopvac",
-       "Sticks",
-       "Storage",
-       "Sword1",
-       "Sword2",
-       "Umbrella"
-};
+static const char *dataset[] = { "Adirondack", "Jadeplant",     "Motorcycle", "Piano",   "Pipes",        "Playroom",
+                                                                "Playtable",  "Recycle",        "Shelves",        "Vintage", "Backpack", "Bicycle1",
+                                                                "Cable",          "Classroom1", "Couch",          "Flowers", "Mask",     "Shopvac",
+                                                                "Sticks",         "Storage",    "Sword1",         "Sword2",  "Umbrella" };
 
 class StopWatch
 {
 public:
-       StopWatch() {
+       StopWatch()
+       {
                start = std::chrono::steady_clock::now();
        }
        ~StopWatch() = default;
 
-       std::chrono::milliseconds elapsedTime() {
-               return std::chrono::duration_cast<std::chrono::milliseconds>(
-                               std::chrono::steady_clock::now() - start);
+       std::chrono::milliseconds elapsedTime()
+       {
+               return std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::steady_clock::now() - start);
        }
+
 private:
        std::chrono::steady_clock::time_point start;
 };
 
-typedef struct _appdata {
+typedef struct _appdata
+{
        mv_3d_h mv3d;
        std::chrono::milliseconds diffMs;
        std::string dataPath;
-       std::string intrinsicName;
-       std::string rgbName;
        std::string datasetName;
        float minDisp;
        float maxDisp;
@@ -101,20 +81,21 @@ typedef struct _appdata {
        std::string dataset;
 } appdata;
 
-enum {
+enum
+{
        FMT_PFM = 1,
        FMT_PNG
 };
 
-int littleendian()
+static int littleendian()
 {
        int intval = 1;
-       uchar *uval = (uchar *)&intval;
+       uchar *uval = (uchar *) &intval;
        return uval[0] == 1;
 }
 
-void WriteFilePFM(float *data, int width, int height, const char* filename,
-                               float minDisp, float maxDisp, float scalefactor = 1 / 255.0)
+static void WriteFilePFM(float *data, int width, int height, const char *filename, float minDisp, float maxDisp,
+                                                float scalefactor = 1 / 255.0)
 {
        FILE *stream = fopen(filename, "wb");
        if (stream == 0) {
@@ -129,13 +110,13 @@ void WriteFilePFM(float *data, int width, int height, const char* filename,
 
        int n = width;
        for (int y = height - 1; y >= 0; y--) {
-               floatptr = data + y * width;
+               float *ptr = data + y * width;
                for (int x = 0; x < width; x++) {
                        if (ptr[x] <= 0 || ptr[x] > maxDisp) { //xsh modified
                                ptr[x] = INFINITY;
                        }
                }
-               if ((int)fwrite(ptr, sizeof(float), n, stream) != n) {
+               if ((int) fwrite(ptr, sizeof(float), n, stream) != n) {
                        fprintf(stderr, "WriteFilePFM: problem writing data\n");
                        exit(1);
                }
@@ -146,18 +127,20 @@ void WriteFilePFM(float *data, int width, int height, const char* filename,
 // translate value x in [0..1] into color triplet using "jet" color map
 // if out of range, use darker colors
 // variation of an idea by http://www.metastine.com/?p=7
-void jet(float x, int& r, int& g, int& b)
+static void jet(float x, int &r, int &g, int &b)
 {
-    if (x < 0) x = -0.05;
-    if (x > 1) x =  1.05;
-    x = x / 1.15 + 0.1; // use slightly asymmetric range to avoid darkest shades of blue.
-    r = __max(0, __min(255, (int)(round(255 * (1.5 - 4*fabs(x - .75))))));
-    g = __max(0, __min(255, (int)(round(255 * (1.5 - 4*fabs(x - .5))))));
-    b = __max(0, __min(255, (int)(round(255 * (1.5 - 4*fabs(x - .25))))));
+       if (x < 0)
+               x = -0.05;
+       if (x > 1)
+               x = 1.05;
+       x = x / 1.15 + 0.1; // use slightly asymmetric range to avoid darkest shades of blue.
+       r = __max(0, __min(255, (int) (round(255 * (1.5 - 4 * fabs(x - .75))))));
+       g = __max(0, __min(255, (int) (round(255 * (1.5 - 4 * fabs(x - .5))))));
+       b = __max(0, __min(255, (int) (round(255 * (1.5 - 4 * fabs(x - .25))))));
 }
 
-void WriteFilePNG(unsigned short *data, int width, int height, const char* filename,
-                               float minDisp, float maxDisp)
+static void WriteFilePNG(unsigned short *data, int width, int height, const char *filename, float minDisp,
+                                                float maxDisp)
 {
        // Open the file
        FILE *stream = fopen(filename, "wb");
@@ -169,13 +152,13 @@ void WriteFilePNG(unsigned short *data, int width, int height, const char* filen
        cv::Mat dump(cv::Size(width, height), CV_16U);
 
        for (int y = 0; y < height; y++) {
-               unsigned shortptr = data + y * width;
+               unsigned short *ptr = data + y * width;
                for (int x = 0; x < width; x++) {
                        unsigned short f = ptr[x];
 
-                       dump.at<unsigned short>(y,x) = f;
+                       dump.at<unsigned short>(y, x) = f;
                }
-    }
+       }
 
        printf("%d x %d", width, height);
        cv::imwrite(filename, dump);
@@ -183,7 +166,7 @@ void WriteFilePNG(unsigned short *data, int width, int height, const char* filen
        fclose(stream);
 }
 
-void WritePLY(double *data, int size, const char* filename)
+static void WritePLY(double *data, int size, const char *filename)
 {
        std::ofstream stream(filename, std::ofstream::out);
        stream << "ply" << std::endl;
@@ -195,61 +178,56 @@ void WritePLY(double *data, int size, const char* filename)
        stream << "end_header" << std::endl;
 
        for (int pc = 0; pc < size; ++pc) {
-               stream << std::fixed << std::setprecision(2) <<
-                               data[pc*3] << " " << data[pc*3 + 1] << " " << data[pc*3 + 2] << std::endl;
+               stream << std::fixed << std::setprecision(2) << data[pc * 3] << " " << data[pc * 3 + 1] << " "
+                          << data[pc * 3 + 2] << std::endl;
        }
 
        return;
 }
 
-void _depth_middlebury_cb(mv_source_h source,
-       unsigned short* depth,
-       unsigned int width,
-       unsigned int height,
-       void* user_data)
+static void _depth_middlebury_cb(mv_source_h source, unsigned short *depth, unsigned int width, unsigned int height,
+                                                                void *user_data)
 {
        if (!user_data) {
                printf("user_data is null. Skip..\n");
                return;
        }
 
-       auto udata = static_cast<appdata*>(user_data);
+       auto udata = static_cast<appdata *>(user_data);
 
        StopWatch stopWatch;
 
        if (udata->fmt == FMT_PFM) {
-               WriteFilePFM(reinterpret_cast<float*>(depth), width, height, udata->datasetName.c_str(),
-                       udata->minDisp, udata->maxDisp);
+               WriteFilePFM(reinterpret_cast<float *>(depth), width, height, udata->datasetName.c_str(), udata->minDisp,
+                                        udata->maxDisp);
        } else {
-               WriteFilePNG(depth, width, height, udata->datasetName.c_str(),
-                       udata->minDisp, udata->maxDisp);
+               WriteFilePNG(depth, width, height, udata->datasetName.c_str(), udata->minDisp, udata->maxDisp);
        }
 
        udata->diffMs = stopWatch.elapsedTime();
 }
 
-void _pointcloud_middlebury_cb(mv_source_h source,
-       mv_3d_pointcloud_h pointcloud,
-       void *user_data)
+static void _pointcloud_middlebury_cb(mv_source_h source, mv_3d_pointcloud_h pointcloud, void *user_data)
 {
        if (!user_data) {
                printf("user_data is null. Skip..\n");
                return;
        }
 
-       auto udata = static_cast<appdata*>(user_data);
-       std::string filename = udata->dataset + std::string(".pcd");
-       mv_3d_pointcloud_write_file(udata->mv3d, pointcloud, MV_3D_POINTCLOUD_TYPE_PCD_BIN, (char*)filename.c_str());
+       auto udata = static_cast<appdata *>(user_data);
+       std::string filename = udata->dataset + std::string(".ply");
+       mv_3d_pointcloud_write_file(udata->mv3d, pointcloud, MV_3D_POINTCLOUD_TYPE_PLY_TXT, (char *) filename.c_str());
 
-       mv_3d_pointcloud_plane_model_h model;
-       mv_3d_pointcloud_plane_inlier_h inlier;
+       mv_3d_pointcloud_plane_model_h model = nullptr;
+       mv_3d_pointcloud_plane_inlier_h inlier = nullptr;
 
        mv_3d_pointcloud_plane_model_create(&model);
        mv_3d_pointcloud_plane_inlier_create(&inlier);
        mv_3d_pointcloud_segment_plane(udata->mv3d, pointcloud, &model, &inlier);
 
-       filename = udata->dataset + std::string("-plane.pcd");
-       mv_3d_pointcloud_plane_write_file(udata->mv3d, model, inlier, pointcloud, MV_3D_POINTCLOUD_TYPE_PCD_BIN, (char*)filename.c_str());
+       filename = udata->dataset + std::string("-plane.ply");
+       mv_3d_pointcloud_plane_write_file(udata->mv3d, model, inlier, pointcloud, MV_3D_POINTCLOUD_TYPE_PLY_TXT,
+                                                                         (char *) filename.c_str());
 
        mv_3d_pointcloud_plane_model_destroy(model);
        mv_3d_pointcloud_plane_inlier_destroy(inlier);
@@ -257,29 +235,27 @@ void _pointcloud_middlebury_cb(mv_source_h source,
 
 int perform_middlebury_test()
 {
-       charpath_to_dataset = NULL;
-       charsuffix_for_algo = NULL;
+       char *path_to_dataset = NULL;
+       char *suffix_for_algo = NULL;
        int err = MEDIA_VISION_ERROR_NONE;
        mv_engine_config_h engine_config = NULL;
-       mv_source_h left_source  = NULL;
+       mv_source_h left_source = NULL;
        mv_source_h right_source = NULL;
-       mv_3d_h mv3d_handle  = NULL;
+       mv_3d_h mv3d_handle = NULL;
        size_t datasize = ARRAY_SIZE(dataset);
        cv::Mat left_frame, right_frame;
 
-       while (input_string("root path including dataset:",
-                                               1024, &path_to_dataset) == -1) {
+       while (input_string("root path including dataset:", 1024, &path_to_dataset) == -1) {
                printf("Incorrect! Try again.\n");
        }
 
-       while (input_string("suffix for algorithm:",
-                                               1024, &suffix_for_algo) == -1) {
+       while (input_string("suffix for algorithm:", 1024, &suffix_for_algo) == -1) {
                printf("Incorrect! Try again.\n");
        }
 
-       const char* formats[] = {"pfm", "png"};
+       const char *formats[] = { "pfm", "png" };
        int sel_fmt = -1;
-       while(sel_fmt <= 0 || sel_fmt > static_cast<int>(ARRAY_SIZE(formats))) {
+       while (sel_fmt <= 0 || sel_fmt > static_cast<int>(ARRAY_SIZE(formats))) {
                sel_fmt = show_menu_linear("Select Action", formats, ARRAY_SIZE(formats));
        }
 
@@ -313,19 +289,17 @@ int perform_middlebury_test()
 
                snprintf(dataPath, MAX_STRING_LENGTH, "%s/%s", path_to_dataset, dataset[data]);
                snprintf(calibFilename, MAX_STRING_LENGTH, "%s/calib.txt", dataPath);
-               snprintf(stereoConfigFileName, MAX_STRING_LENGTH, "%s/calibOcv.xml", dataPath);
+               snprintf(stereoConfigFileName, MAX_STRING_LENGTH, "%s/calibOcv.yaml", dataPath);
                snprintf(leftFilename, MAX_STRING_LENGTH, "%s/im0.png", dataPath);
                snprintf(rightFilename, MAX_STRING_LENGTH, "%s/im1.png", dataPath);
 
-               if ((access(calibFilename, F_OK) < 0) ||
-                       (access(leftFilename, F_OK) < 0) ||
-                       (access(rightFilename, F_OK) < 0) ) {
+               if ((access(calibFilename, F_OK) < 0) || (access(leftFilename, F_OK) < 0) ||
+                       (access(rightFilename, F_OK) < 0)) {
                        printf("Invalid dataset path:\n");
                        goto _err;
                }
 
-               printf("Run dataset %s\n", dataset[data]);
-               FILE* fp = fopen(calibFilename, "r");
+               FILE *fp = fopen(calibFilename, "r");
                char line[MAX_STRING_LENGTH];
                float fVal;
                float dmin = 0, dmax = 0;
@@ -333,10 +307,14 @@ int perform_middlebury_test()
                int width = 0, height = 0;
                if (fp != NULL) {
                        while (fgets(line, MAX_STRING_LENGTH, fp) != NULL) {
-                               if (sscanf(line, "vmin= %f", &fVal) == 1) dmin = fVal;
-                               if (sscanf(line, "vmax= %f", &fVal) == 1) dmax = fVal;
-                               if (sscanf(line, "width= %d", &iVal) == 1) width = iVal;
-                               if (sscanf(line, "height= %d", &iVal) == 1) height = iVal;
+                               if (sscanf(line, "vmin= %f", &fVal) == 1)
+                                       dmin = fVal;
+                               if (sscanf(line, "vmax= %f", &fVal) == 1)
+                                       dmax = fVal;
+                               if (sscanf(line, "width= %d", &iVal) == 1)
+                                       width = iVal;
+                               if (sscanf(line, "height= %d", &iVal) == 1)
+                                       height = iVal;
                        }
                        fclose(fp);
                } else {
@@ -348,71 +326,64 @@ int perform_middlebury_test()
                int maxDisp = static_cast<int>(dmax);
 
                // engine_config
-               err = mv_engine_config_set_int_attribute(engine_config,
-                                       MV_3D_DEPTH_MODE, MV_3D_DEPTH_MODE_STEREO);
+               err = mv_engine_config_set_int_attribute(engine_config, MV_3D_DEPTH_MODE, MV_3D_DEPTH_MODE_STEREO);
                if (err != MEDIA_VISION_ERROR_NONE) {
                        printf("Failed to set depth mode\n");
                        goto _err;
                }
 
-               err = mv_engine_config_set_int_attribute(engine_config,
-                                       MV_3D_DEPTH_WIDTH, width);
+               err = mv_engine_config_set_int_attribute(engine_config, MV_3D_DEPTH_WIDTH, width);
                if (err != MEDIA_VISION_ERROR_NONE) {
                        printf("Failed to set width\n");
                        goto _err;
                }
 
-               err = mv_engine_config_set_int_attribute(engine_config,
-                                       MV_3D_DEPTH_HEIGHT, height);
+               err = mv_engine_config_set_int_attribute(engine_config, MV_3D_DEPTH_HEIGHT, height);
                if (err != MEDIA_VISION_ERROR_NONE) {
                        printf("Failed to set height\n");
                        goto _err;
                }
 
-               err = mv_engine_config_set_int_attribute(engine_config,
-                                       MV_3D_DEPTH_MIN_DISPARITY, minDisp);
+               err = mv_engine_config_set_int_attribute(engine_config, MV_3D_DEPTH_MIN_DISPARITY, minDisp);
                if (err != MEDIA_VISION_ERROR_NONE) {
                        printf("Failed to set min disparity\n");
                        goto _err;
                }
 
-               err = mv_engine_config_set_int_attribute(engine_config,
-                                       MV_3D_DEPTH_MAX_DISPARITY, maxDisp);
+               err = mv_engine_config_set_int_attribute(engine_config, MV_3D_DEPTH_MAX_DISPARITY, maxDisp);
                if (err != MEDIA_VISION_ERROR_NONE) {
                        printf("Failed to set max disparity\n");
                        goto _err;
                }
 
-               err = mv_engine_config_set_string_attribute(engine_config,
-                                       MV_3D_DEPTH_STEREO_CONFIG_FILE_PATH, stereoConfigFileName);
+               err = mv_engine_config_set_string_attribute(engine_config, MV_3D_DEPTH_STEREO_CONFIG_FILE_PATH,
+                                                                                                       stereoConfigFileName);
                if (err != MEDIA_VISION_ERROR_NONE) {
                        printf("Failed to set stereo config file path\n");
                        goto _err;
                }
 
-               err = mv_engine_config_set_double_attribute(engine_config,
-                                       MV_3D_POINTCLOUD_SAMPLING_RATIO, samplingRatio);
+               err = mv_engine_config_set_double_attribute(engine_config, MV_3D_POINTCLOUD_SAMPLING_RATIO, samplingRatio);
                if (err != MEDIA_VISION_ERROR_NONE) {
                        printf("Failed to set stereo config file path\n");
                        goto _err;
                }
 
-               err = mv_engine_config_set_int_attribute(engine_config,
-                                       MV_3D_POINTCLOUD_OUTLIER_REMOVAL_POINTS, outlierRemovalPoints);
+               err = mv_engine_config_set_int_attribute(engine_config, MV_3D_POINTCLOUD_OUTLIER_REMOVAL_POINTS,
+                                                                                                outlierRemovalPoints);
                if (err != MEDIA_VISION_ERROR_NONE) {
                        printf("Failed to set stereo config file path\n");
                        goto _err;
                }
 
-               err = mv_engine_config_set_double_attribute(engine_config,
-                                       MV_3D_POINTCLOUD_OUTLIER_REMOVAL_RADIUS, outlierRemovalRadius);
+               err = mv_engine_config_set_double_attribute(engine_config, MV_3D_POINTCLOUD_OUTLIER_REMOVAL_RADIUS,
+                                                                                                       outlierRemovalRadius);
                if (err != MEDIA_VISION_ERROR_NONE) {
                        printf("Failed to set stereo config file path\n");
                        goto _err;
                }
 
-               err = mv_engine_config_set_string_attribute(engine_config,
-                                       MV_3D_POINTCLOUD_OUTPUT_FILE_PATH, MEDIA_FILE_PATH);
+               err = mv_engine_config_set_string_attribute(engine_config, MV_3D_POINTCLOUD_OUTPUT_FILE_PATH, MEDIA_FILE_PATH);
                if (err != MEDIA_VISION_ERROR_NONE) {
                        printf("Failed to set stereo config file path\n");
                        goto _err;
@@ -422,25 +393,19 @@ int perform_middlebury_test()
                left_frame = cv::imread(leftFilename, cv::IMREAD_COLOR);
                right_frame = cv::imread(rightFilename, cv::IMREAD_COLOR);
 
-               err = mv_source_fill_by_buffer(left_source,
-                               left_frame.ptr<unsigned char>(),
-                               left_frame.elemSize() * left_frame.size().width
-                                                                         * left_frame.size().height,
-                               left_frame.size().width,
-                               left_frame.size().height,
-                               MEDIA_VISION_COLORSPACE_RGB888);
+               err = mv_source_fill_by_buffer(left_source, left_frame.ptr<unsigned char>(),
+                                                                          left_frame.elemSize() * left_frame.size().width * left_frame.size().height,
+                                                                          left_frame.size().width, left_frame.size().height,
+                                                                          MEDIA_VISION_COLORSPACE_RGB888);
                if (err != MEDIA_VISION_ERROR_NONE) {
                        printf("Failed to fill left_source\n");
                        goto _err;
                }
 
-               err = mv_source_fill_by_buffer(right_source,
-                               right_frame.ptr<unsigned char>(),
-                               right_frame.elemSize() * right_frame.size().width
-                                                                         * right_frame.size().height,
-                               right_frame.size().width,
-                               right_frame.size().height,
-                               MEDIA_VISION_COLORSPACE_RGB888);
+               err = mv_source_fill_by_buffer(right_source, right_frame.ptr<unsigned char>(),
+                                                                          right_frame.elemSize() * right_frame.size().width * right_frame.size().height,
+                                                                          right_frame.size().width, right_frame.size().height,
+                                                                          MEDIA_VISION_COLORSPACE_RGB888);
                if (err != MEDIA_VISION_ERROR_NONE) {
                        printf("Failed to fill right_source\n");
                        goto _err;
@@ -460,27 +425,23 @@ int perform_middlebury_test()
                }
 
                // get depth
-               appdata dump {
-                       mv3d_handle,
-                       std::chrono::milliseconds::zero(),
-                       std::string(dataPath),
-                       std::string(dataPath) + std::string("/intrinsics.json"),
-                       std::string(leftFilename),
-                       std::string(dataPath) + std::string("/disp0") + std::string(suffix_for_algo),
-                       static_cast<float>(minDisp),
-                       static_cast<float>(maxDisp),
-                       sel_fmt
-               };
+               appdata dump { mv3d_handle,
+                                          std::chrono::milliseconds::zero(),
+                                          std::string(dataPath),
+                                          std::string(dataPath) + std::string("/disp0") + std::string(suffix_for_algo),
+                                          static_cast<float>(minDisp),
+                                          static_cast<float>(maxDisp),
+                                          sel_fmt,
+                                          std::string(dataset[data]) };
                dump.datasetName += sel_fmt == FMT_PFM ? std::string(".pfm") : std::string(".png");
-               dump.dataset = std::string(dataset[data]);
 
-               err = mv_3d_set_depth_cb(mv3d_handle, _depth_middlebury_cb, static_cast<void*>(&dump));
+               err = mv_3d_set_depth_cb(mv3d_handle, _depth_middlebury_cb, static_cast<void *>(&dump));
                if (err != MEDIA_VISION_ERROR_NONE) {
                        printf("Failed to set depth callback to handle\n");
                        goto _err;
                }
 
-               err = mv_3d_set_pointcloud_cb(mv3d_handle, _pointcloud_middlebury_cb, static_cast<void*>(&dump));
+               err = mv_3d_set_pointcloud_cb(mv3d_handle, _pointcloud_middlebury_cb, static_cast<void *>(&dump));
                if (err != MEDIA_VISION_ERROR_NONE) {
                        printf("Failed to set pointcloud callback to handle\n");
                        goto _err;
@@ -491,11 +452,9 @@ int perform_middlebury_test()
                        printf("Failed to prepare depth handle\n");
                        goto _err;
                }
+
                StopWatch stopWatch;
-               err = mv_3d_run(mv3d_handle,
-                                               left_source,
-                                               right_source,
-                                               NULL);
+               err = mv_3d_run(mv3d_handle, left_source, right_source, NULL);
 
                /* time to save a file is excluded */
                std::chrono::milliseconds diffMs = stopWatch.elapsedTime() - dump.diffMs;
@@ -531,7 +490,7 @@ _err:
 int main()
 {
        int err = MEDIA_VISION_ERROR_NONE;
-       const char* names[] = {"Middlebury - TrainingQ(Imperf)"};
+       const char *names[] = { "Middlebury - TrainingQ(Imperf)" };
 
        int sel_opt = show_menu_linear("Select Action", names, ARRAY_SIZE(names));
        if (sel_opt <= 0 || sel_opt > static_cast<int>(ARRAY_SIZE(names))) {