mv3d: update depth_test_suite.cpp to select disparity or depth 84/282884/2
authorTae-Young Chung <ty83.chung@samsung.com>
Thu, 13 Oct 2022 02:40:56 +0000 (11:40 +0900)
committerTae-Young Chung <ty83.chung@samsung.com>
Thu, 13 Oct 2022 04:12:39 +0000 (13:12 +0900)
[Version]: 0.23.37-0
[Issue type]: update

Change-Id: If782905b32fb2057d1b1c42c3c4438639438dac4
Signed-off-by: Tae-Young Chung <ty83.chung@samsung.com>
packaging/capi-media-vision.spec
test/testsuites/mv3d/depth_test_suite.cpp

index 60cb16c..0f015d9 100644 (file)
@@ -1,6 +1,6 @@
 Name:        capi-media-vision
 Summary:     Media Vision library for Tizen Native API
-Version:     0.23.36
+Version:     0.23.37
 Release:     0
 Group:       Multimedia/Framework
 License:     Apache-2.0 and BSD-3-Clause
index 100e976..b5c964e 100644 (file)
@@ -51,6 +51,8 @@ static const char *dataset[] = { "Adirondack", "Jadeplant",    "Motorcycle", "Pian
                                                                 "Cable",          "Classroom1", "Couch",          "Flowers", "Mask",     "Shopvac",
                                                                 "Sticks",         "Storage",    "Sword1",         "Sword2",  "Umbrella" };
 
+enum DistanceMode { DISTANCE_DEPTH, DISTANCE_DISPARITY };
+
 class StopWatch
 {
 public:
@@ -77,8 +79,10 @@ typedef struct _appdata
        std::string datasetName;
        float minDisp;
        float maxDisp;
+       float depth2Disp;
        int fmt;
        std::string dataset;
+       DistanceMode distanceMode;
 } appdata;
 
 enum
@@ -94,8 +98,8 @@ static int littleendian()
        return uval[0] == 1;
 }
 
-static void WriteFilePFM(float *data, int width, int height, const char *filename, float minDisp, float maxDisp,
-                                                float scalefactor = 1 / 255.0)
+static void WriteFilePFM(unsigned short *data, int width, int height, const char *filename, float minDisp, float maxDisp,
+                                                float depth2Disp, DistanceMode distanceMode, float scalefactor = 1 / 255.0)
 {
        FILE *stream = fopen(filename, "wb");
        if (stream == 0) {
@@ -108,19 +112,21 @@ static void WriteFilePFM(float *data, int width, int height, const char *filenam
 
        fprintf(stream, "Pf\n%d %d\n%f\n", width, height, scalefactor);
 
-       int n = width;
+       std::vector<float> dump(width);
        for (int y = height - 1; y >= 0; y--) {
-               float *ptr = data + y * width;
+               unsigned short* ptr = data + y * width;
                for (int x = 0; x < width; x++) {
-                       if (ptr[x] <= 0 || ptr[x] > maxDisp) { //xsh modified
-                               ptr[x] = INFINITY;
-                       }
+                       if (distanceMode == DISTANCE_DISPARITY)
+                               dump[x] = depth2Disp / static_cast<float>(ptr[x]);
+                       else
+                               dump[x] = static_cast<float>(ptr[x]);
                }
-               if ((int) fwrite(ptr, sizeof(float), n, stream) != n) {
+               if ((int)fwrite(dump.data(), sizeof(float), width, stream) != width) {
                        fprintf(stderr, "WriteFilePFM: problem writing data\n");
                        exit(1);
                }
        }
+
        fclose(stream);
 }
 
@@ -140,7 +146,7 @@ static void jet(float x, int &r, int &g, int &b)
 }
 
 static void WriteFilePNG(unsigned short *data, int width, int height, const char *filename, float minDisp,
-                                                float maxDisp)
+                                                float maxDisp, float depth2Disp, int distanceMode)
 {
        // Open the file
        FILE *stream = fopen(filename, "wb");
@@ -152,15 +158,16 @@ static void WriteFilePNG(unsigned short *data, int width, int height, const char
        cv::Mat dump(cv::Size(width, height), CV_16U);
 
        for (int y = 0; y < height; y++) {
-               unsigned short *ptr = data + y * width;
+               unsigned shortptr = data + y * width;
                for (int x = 0; x < width; x++) {
-                       unsigned short f = ptr[x];
 
-                       dump.at<unsigned short>(y, x) = f;
+                       if (distanceMode == DISTANCE_DISPARITY)
+                               dump.at<unsigned short>(y,x) = static_cast<unsigned short>(depth2Disp / static_cast<float>(ptr[x]));
+                       else
+                               dump.at<unsigned short>(y,x) = ptr[x];
                }
-       }
+    }
 
-       printf("%d x %d", width, height);
        cv::imwrite(filename, dump);
 
        fclose(stream);
@@ -198,10 +205,11 @@ static void _depth_middlebury_cb(mv_source_h source, unsigned short *depth, unsi
        StopWatch stopWatch;
 
        if (udata->fmt == FMT_PFM) {
-               WriteFilePFM(reinterpret_cast<float *>(depth), width, height, udata->datasetName.c_str(), udata->minDisp,
-                                        udata->maxDisp);
+               WriteFilePFM(depth, width, height, udata->datasetName.c_str(),
+                       udata->minDisp, udata->maxDisp, udata->depth2Disp, udata->distanceMode);
        } 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->depth2Disp, udata->distanceMode);
        }
 
        udata->diffMs = stopWatch.elapsedTime();
@@ -259,6 +267,12 @@ int perform_middlebury_test()
                sel_fmt = show_menu_linear("Select Action", formats, ARRAY_SIZE(formats));
        }
 
+       const char* distances[] = {"disparty", "depth"};
+       int sel_distance = -1;
+       while (sel_distance <= 0 || sel_distance > ARRAY_SIZE(distances)) {
+               sel_distance = show_menu_linear("Select Action", distances, ARRAY_SIZE(distances));
+       }
+
        err = mv_create_engine_config(&engine_config);
        if (err != MEDIA_VISION_ERROR_NONE) {
                printf("Failed to create engine config\n");
@@ -304,7 +318,10 @@ int perform_middlebury_test()
                float fVal;
                float dmin = 0, dmax = 0;
                int iVal;
+               float fx = 0.f;
+               float baseline = 0.0f;
                int width = 0, height = 0;
+
                if (fp != NULL) {
                        while (fgets(line, MAX_STRING_LENGTH, fp) != NULL) {
                                if (sscanf(line, "vmin= %f", &fVal) == 1)
@@ -315,6 +332,10 @@ int perform_middlebury_test()
                                        width = iVal;
                                if (sscanf(line, "height= %d", &iVal) == 1)
                                        height = iVal;
+                               if (sscanf(line, "cam0=[ %f", &fVal) == 1)
+                                       fx = fVal;
+                               if (sscanf(line, "baseline= %f", &fVal) == 1)
+                                       baseline = fVal;
                        }
                        fclose(fp);
                } else {
@@ -390,13 +411,13 @@ int perform_middlebury_test()
                }
 
                // left_source, right_source
-               left_frame = cv::imread(leftFilename, cv::IMREAD_COLOR);
-               right_frame = cv::imread(rightFilename, cv::IMREAD_COLOR);
+               left_frame = cv::imread(leftFilename, cv::IMREAD_GRAYSCALE);
+               right_frame = cv::imread(rightFilename, cv::IMREAD_GRAYSCALE);
 
                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);
+                                                                          MEDIA_VISION_COLORSPACE_Y800);
                if (err != MEDIA_VISION_ERROR_NONE) {
                        printf("Failed to fill left_source\n");
                        goto _err;
@@ -405,7 +426,7 @@ int perform_middlebury_test()
                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);
+                                                                          MEDIA_VISION_COLORSPACE_Y800);
                if (err != MEDIA_VISION_ERROR_NONE) {
                        printf("Failed to fill right_source\n");
                        goto _err;
@@ -431,8 +452,11 @@ int perform_middlebury_test()
                                           std::string(dataPath) + std::string("/disp0") + std::string(suffix_for_algo),
                                           static_cast<float>(minDisp),
                                           static_cast<float>(maxDisp),
+                                          fx * baseline,
                                           sel_fmt,
-                                          std::string(dataset[data]) };
+                                          std::string(dataset[data]),
+                                          static_cast<DistanceMode>(sel_distance) };
+               printf("fx:%f, baseline:%f, d2d: %f\n", fx, baseline, dump.depth2Disp);
                dump.datasetName += sel_fmt == FMT_PFM ? std::string(".pfm") : std::string(".png");
 
                err = mv_3d_set_depth_cb(mv3d_handle, _depth_middlebury_cb, static_cast<void *>(&dump));