From 3df03d1a0cc2a727be9e6fb3d15b54eba8d4e72c Mon Sep 17 00:00:00 2001
From: "simon.fanetti" <simon.fanetti@etu.hesge.ch>
Date: Tue, 30 Jun 2020 22:44:50 +0200
Subject: [PATCH] fixe adjustFrame

---
 Makefile                      |  2 ++
 inc/projection.h              |  2 +-
 src/components/projection.cpp | 55 ++++++++++++++++++++---------------
 3 files changed, 34 insertions(+), 25 deletions(-)

diff --git a/Makefile b/Makefile
index 4081b6b..fe6a379 100644
--- a/Makefile
+++ b/Makefile
@@ -1,6 +1,8 @@
 all:
 	$(MAKE) -C src
 	$(MAKE) -C build
+
+app:
 	$(MAKE) -C app
 
 clean:
diff --git a/inc/projection.h b/inc/projection.h
index 6d906b5..7fb3d02 100644
--- a/inc/projection.h
+++ b/inc/projection.h
@@ -11,7 +11,7 @@ class Projection{
         float distanceTopSandbox;
 
         void deprojectPixelsFromDepth(cv::Mat &depth, cv::Rect mask, Camera *camera, cv::Point3f beamer_pos, cv::Mat &pixelsDeprojectMap);
-        void filterHighestDeprojectedPoints(cv::Mat &depth, cv::Mat &pixelsDeprojectMap, cv::Mat &pixelsDeprojectHighestMap);
+        void filterLowestDeprojectedPoints(cv::Mat &depth, cv::Mat &deprojectMap, cv::Mat &frameMapMask);
         void buildFrame(cv::Mat &depth, cv::Mat &pixelsDeprojectHighestMap, cv::Mat &src, cv::Mat &dst);
         cv::Point2i findMatchingPixel(int i, int j, float z, Camera *camera, cv::Point3f beamer_pos);
         void copyPixelsInto(cv::Point2i pixel_dst, cv::Mat &dst, cv::Point2i pixel_src, cv::Mat &src, cv::Mat &depth);
diff --git a/src/components/projection.cpp b/src/components/projection.cpp
index 77b8a51..0d9a294 100644
--- a/src/components/projection.cpp
+++ b/src/components/projection.cpp
@@ -39,16 +39,16 @@ void Projection::adjustFrame(cv::Mat depth, cv::Mat src, cv::Mat &dst, Camera *c
     cv::resize(dst, dst, getMatchingSize(dst, mask));
     cv::resize(src, src, dst.size());
    
-    cv::Mat pixelsDeprojectMap = cv::Mat_<cv::Point2i>(mask.height, mask.width, cv::Point2i(-1,-1));
-    cv::Mat pixelsDeprojectHighestMap = cv::Mat_<cv::Point2i>(mask.height, mask.width, cv::Point2i(-1,-1));
+    cv::Mat deprojectMap = cv::Mat_<cv::Point2i>(mask.height, mask.width, cv::Point2i(-1,-1));
+    cv::Mat frameMapMask = cv::Mat_<cv::Point2i>(mask.height, mask.width, cv::Point2i(-1,-1));
 
-    deprojectPixelsFromDepth(depth, mask, camera, beamer_pos, pixelsDeprojectMap);
+    deprojectPixelsFromDepth(depth, mask, camera, beamer_pos, deprojectMap);
 
-    filterHighestDeprojectedPoints(depth, pixelsDeprojectMap, pixelsDeprojectHighestMap);
+    filterLowestDeprojectedPoints(depth, deprojectMap, frameMapMask);
 
-    // TODO : Holefilling method
+    buildFrame(depth, frameMapMask, src, dst);
 
-    buildFrame(depth, pixelsDeprojectHighestMap, src, dst);
+    // TODO : Holefilling method
 
     cv::resize(dst, dst, dst_size);
     cv::warpAffine(dst, dst, adjustingMatrix, dst.size());
@@ -60,7 +60,7 @@ void Projection::adjustFrame(cv::Mat depth, cv::Mat src, cv::Mat &dst, Camera *c
  *   PRIVATE
  */
 
-void Projection::deprojectPixelsFromDepth(cv::Mat &depth, cv::Rect mask, Camera *camera, cv::Point3f beamer_pos, cv::Mat &pixelsDeprojectMap){
+void Projection::deprojectPixelsFromDepth(cv::Mat &depth, cv::Rect mask, Camera *camera, cv::Point3f beamer_pos, cv::Mat &deprojectMap){
 
     // Browse the depth frame matching the cropping mask
     // while adapting pixels's position to the beamer's position
@@ -79,43 +79,50 @@ void Projection::deprojectPixelsFromDepth(cv::Mat &depth, cv::Rect mask, Camera
             pixel.x -= mask.x;
             pixel.y -= mask.y;
 
-            pixelsDeprojectMap.at<cv::Point2i>(j,i) = pixel;
+            deprojectMap.at<cv::Point2i>(j,i) = pixel;
         }
     }
 }
 
 
-void Projection::filterHighestDeprojectedPoints(cv::Mat &depth, cv::Mat &pixelsDeprojectMap, cv::Mat &pixelsDeprojectHighestMap){
+void Projection::filterLowestDeprojectedPoints(cv::Mat &depth, cv::Mat &deprojectMap, cv::Mat &frameMapMask){
 
-    for (int j = 0; j < pixelsDeprojectMap.rows; j++){
-        for (int i = 0; i < pixelsDeprojectMap.cols; i++){
+    for (int j = 0; j < deprojectMap.rows; j++){
+        for (int i = 0; i < deprojectMap.cols; i++){
 
-            cv::Point2i pixel = pixelsDeprojectMap.at<cv::Point2i>(j,i);
+            // coords of the new pixel
+            cv::Point2i deprojectedPixel = deprojectMap.at<cv::Point2i>(j,i);
+            cv::Point2i highestDepthPixel = cv::Point2i(i,j);
 
-            if(pixel.x != -1 && pixel.y != -1){
+            if( (0 <= deprojectedPixel.x && deprojectedPixel.x < depth.cols) &&
+                (0 <= deprojectedPixel.y && deprojectedPixel.y < depth.rows) ){
+                
                 // check and keep the highest point at the location pointed by pixel
-                cv::Point2i defaultPoint = pixelsDeprojectHighestMap.at<cv::Point2i>(j,i);
-                if(defaultPoint.x != -1 && defaultPoint.y != -1){
-                    if(depth.at<float>(defaultPoint) <= depth.at<float>(pixel))
-                        pixel = defaultPoint;
+                cv::Point2i currentDepthPixel = frameMapMask.at<cv::Point2i>(deprojectedPixel);
+                if( (0 <= currentDepthPixel.x && currentDepthPixel.x < depth.cols) &&
+                    (0 <= currentDepthPixel.y && currentDepthPixel.y < depth.rows) ){
+                        if(depth.at<float>(currentDepthPixel) <= depth.at<float>(j,i)){
+                            highestDepthPixel = currentDepthPixel;
+                        }
                 }
-                pixelsDeprojectHighestMap.at<cv::Point2i>(j,i) = pixel;
+                frameMapMask.at<cv::Point2i>(deprojectedPixel) = highestDepthPixel;
             }
         }
     }
 }
 
 
-void Projection::buildFrame(cv::Mat &depth, cv::Mat &pixelsDeprojectHighestMap, cv::Mat &src, cv::Mat &dst){
+void Projection::buildFrame(cv::Mat &depth, cv::Mat &frameMapMask, cv::Mat &src, cv::Mat &dst){
     
-    for (int j = 0; j < pixelsDeprojectHighestMap.rows; j++){
-        for (int i = 0; i < pixelsDeprojectHighestMap.cols; i++){
+    for (int j = 0; j < frameMapMask.rows; j++){
+        for (int i = 0; i < frameMapMask.cols; i++){
 
-            cv::Point2i pixel = pixelsDeprojectHighestMap.at<cv::Point2i>(j,i);
+            cv::Point2i pixel_src = frameMapMask.at<cv::Point2i>(j,i);
+            cv::Point2i pixel_dst = cv::Point2i(i,j);
 
-            if( (0<=pixel.x && pixel.x<depth.cols) && (0<=pixel.y && pixel.y<depth.rows) ){
+            if( (0<=pixel_src.x && pixel_src.x<depth.cols) && (0<=pixel_src.y && pixel_src.y<depth.rows) ){
                 // src and dst must be of same size
-                copyPixelsInto(pixel, dst, cv::Point2i(i,j), src, depth);
+                copyPixelsInto(pixel_dst, dst, pixel_src, src, depth);
             }
         }
     }
-- 
GitLab