Commits

iorodeo committed e4f247d

Worked on translation of pixel feature vector.

Comments (0)

Files changed (4)

src/demo/fly_sorter/hog_position_fitter.cpp

                 CV_WINDOW_AUTOSIZE | CV_WINDOW_KEEPRATIO | CV_GUI_EXPANDED
                 );
     }
+    cv::namedWindow(
+            "maskWindow",
+            CV_WINDOW_AUTOSIZE | CV_WINDOW_KEEPRATIO | CV_GUI_EXPANDED
+            );
 };
 
 void HogPositionFitter::setParam(HogPositionFitterParam param)
                     imageSize,
                     cv::INTER_LINEAR,
                     cv::BORDER_CONSTANT,
-                    param_.fillValuesLUV*255.0
+                    param_.fillValuesLUV*255.0  // Scale
                     );
 
             // Get pixel feature vector
 
 cv::Mat HogPositionFitter::getPixelFeatureVector(cv::Mat image)
 {
-    //std::cout << __PRETTY_FUNCTION__ << std::endl;
+    std::cout << __PRETTY_FUNCTION__ << std::endl;
+    std::cout <<  "(x) image.cols = " << image.cols << ", (y) image.rows = " << image.rows << std::endl;
 
-    cv::Mat pixelFeatureVector;
 
-    unsigned int normRadius = 3;
-    double normConst = 0.001;
-
+    // Get Gradient data
     GradientData gradData = getGradientData(
             image,
-            normRadius,
-            normConst,
+            param_.pixelFeatureVector.gradNormRadius,
+            param_.pixelFeatureVector.gradNormConst*255, // Scale
             GRAD_METHOD_SCHARR
             );
 
+    // Create mask which is 0 on filled in values (due to rotation)
+    // Don't need to create whole matrix - figure out how fill values are being cast.
+    cv::Mat fillValuesMat = cv::Mat(
+            image.size(),
+            image.type(), 
+            255.0*param_.fillValuesLUV // Scale
+            ); 
+    //cv::Mat fillMask = image != fillValuesMat;
+   
+    cv::Mat fillMask = cv::Mat(image.size(), CV_8UC1, cv::Scalar(255));
+
+    for (int i=0; i<image.cols; i++)
+    {
+        for (int j=0; j<image.rows; j++)
+        {
+            cv::Vec3b pixVec = image.at<cv::Vec3b>(j,i);
+            cv::Vec3b fillVec = fillValuesMat.at<cv::Vec3b>(j,i);
+            bool isEqual = true; 
+            for (int k=0; k<3; k++)
+            {
+                int val = int(pixVec.val[k]);
+                int fillVal = int(fillVec.val[k]);
+                //std::cout << fillVal << " ";
+                if (val != fillVal)
+                { 
+                    isEqual = false;
+                }
+            }
+            if (isEqual)
+            {
+                fillMask.at<uchar>(j,i) = 0;
+            }
+            //std::cout << std::endl;
+        }
+    }
+
+
+    // Erode mask slightly to get rid of any gradient at fill bndry 
+    unsigned int elemDiam = 2*param_.pixelFeatureVector.fillBndryErodeRadius+1;
+    cv::Size elemSize = cv::Size(elemDiam,elemDiam);
+    cv::Mat structElem = cv::getStructuringElement(cv::MORPH_ELLIPSE, elemSize);
+    cv::erode(fillMask,fillMask,structElem);
+    cv::imshow("maskWindow", fillMask);
+
+
+    std::vector<double> meanColor;
+    std::vector<double> meanGradMag;
+    std::vector<double> histColor;
+    std::vector<double> histGradMag;
+    std::vector<double> histGradOri;
+
+    for (int i=0; i<param_.pixelFeatureVector.binParam.size(); i++)
+    {
+        unsigned int numX = param_.pixelFeatureVector.binParam[i].numX;
+        unsigned int numY = param_.pixelFeatureVector.binParam[i].numY;
+        double binWidth = double(image.cols-1)/double(numX);
+        double binHeight = double(image.rows-1)/double(numY);
+
+        std::cout << "  binParam(" << i << ") = " << numX << ", " << numY << std::endl;
+
+        // Loop over spacial bins
+        for (int indX=0; indX < numX; indX++)
+        {
+            for (int indY=0; indY < numY; indY++)
+            {
+                int x = int(std::round(indX*binWidth));
+                int y = int(std::round(indY*binHeight));
+                cv::Rect roiRect = cv::Rect(x,y,binWidth,binHeight);
+                cv::Mat subImage = image(roiRect);
+                cv::Mat normMagMax = gradData.normMagMax(roiRect);
+                cv::Mat oriOfNormMagMax = gradData.oriOfNormMagMax(roiRect);
+
+                std::cout << "    roiRect: " << roiRect.x << ", " << roiRect.y << ", " << roiRect.width << ", " << roiRect.height << std::endl;
+
+            }
+        } 
+    }
+
+
+    cv::Mat pixelFeatureVector;
     return pixelFeatureVector;
 }
 
     }
 
     // Find magnitude and orientation of gradient
-    cv::cartToPolar(gradData.dx, gradData.dy, gradData.mag, gradData.orient);
+    cv::cartToPolar(gradData.dx, gradData.dy, gradData.mag, gradData.ori);
 
     // Find normalized maginitude and gradient. Note, I don't completely 
     // understand this normalization - discuss with Kristin.
                 );
     }
     gradData.normMag = gradData.mag/(smoothMag+normConst);
+
+    // Loop over image  get maxium of gradient magnitudes (w.r.t. color) for each pixel.  
+    // Also get the orientation of the gradient with the largest magniude. .  
+    gradData.normMagMax = cv::Mat(gradData.normMag.size(), CV_32FC1, cv::Scalar(0.0));
+    gradData.oriOfNormMagMax = cv::Mat(gradData.ori.size(), CV_32FC1, cv::Scalar(0.0));
+    for (int i=0; i<image.cols; i++)
+    {
+        for (int j=0; j<image.rows; j++)
+        {
+            cv::Vec3f normMagVec = gradData.normMag.at<cv::Vec3f>(j,i);
+            cv::Vec3f oriVec = gradData.ori.at<cv::Vec3f>(j,i);
+            double normMagMax = 0.0;
+            double oriOfNormMagMax = 0.0;
+            for (int k=0; k<3; k++)
+            {
+                if (normMagVec.val[k] >= normMagMax)
+                {
+                    normMagMax = normMagVec.val[k];
+                    oriOfNormMagMax = oriVec.val[k];
+                }
+            }
+            gradData.normMagMax.at<float>(j,i) = normMagMax;
+            gradData.oriOfNormMagMax.at<float>(j,i) = oriOfNormMagMax; 
+        }
+    }
     return gradData;
 }
 
 {
     cv::Mat f1D = getTriangleFilter1D(normRadius);
     cv::Mat f2D = f1D.t()*f1D;
-    std::cout << "f2D: size = " << f2D.rows << " x " << f2D.cols << std::endl;
     return f2D;
 }

src/demo/fly_sorter/hog_position_fitter.hpp

 class GradientData
 {
     public:
-        cv::Mat dx;
-        cv::Mat dy;
-        cv::Mat mag;
-        cv::Mat normMag;
-        cv::Mat orient;
+        cv::Mat dx;              // derivative w.r.t. x
+        cv::Mat dy;              // derivative w.r.t. y
+        cv::Mat mag;             // gradient magnitude
+        cv::Mat ori;             // gradient orientation
+        cv::Mat normMag;         // normalized gradient magnitude
+        cv::Mat normMagMax;      // maximum (over all channels) of normalized gradient magnitude
+        cv::Mat oriOfNormMagMax; // orientation of maximum gradient
 };
 
 

src/demo/fly_sorter/parameters.cpp

 // FlySegmenterParam
 // ----------------------------------------------------------------------------
 
-// TEMPORARY - kludgey, but I just need some data for developing 
-// the fly segmenter. We will read this from a file later.
-// ----------------------------------------------------------------------------
+// TEMPORARY 
 ClassifierParam FlySegmenterParam::createDefaultClassifierParam()
 {
     ClassifierParam classifierParam;
 };
 const ClassifierParam FlySegmenterParam::DEFAULT_CLASSIFIER_PARAM = 
 createDefaultClassifierParam();
-// ----------------------------------------------------------------------------
 
 
 FlySegmenterParam::FlySegmenterParam()
     classifier = DEFAULT_CLASSIFIER_PARAM;
 };
 
+// BinParam
+// ----------------------------------------------------------------------------
+BinParam::BinParam()
+{
+    numX = 0;
+    numY = 0;
+}
+
+BinParam::BinParam(unsigned int numX_, unsigned int numY_)
+{
+    numX = numX_;
+    numY = numY_;
+}
+
 
 // PixelFeatureVectorParam
 // ----------------------------------------------------------------------------
+const unsigned int PixelFeatureVectorParam::DEFAULT_GRAD_NORM_RADIUS = 5;
+const double PixelFeatureVectorParam::DEFAULT_GRAD_NORM_CONST = 0.005;  // Needs scaling by 255 (??)
+const unsigned int PixelFeatureVectorParam::DEFAULT_FILL_BNDRY_ERODE_RADIUS = 2;
+
+// TEMPORARY  - needs scaling by 255 (??)
 std::vector<cv::Scalar> PixelFeatureVectorParam::createDefaultColorEdgeVector()
 {
     std::vector<cv::Scalar> colorEdgeVector;
     colorEdgeVector.push_back(cv::Scalar(2.0000000, 2.0000000, 2.0000000));
     return colorEdgeVector;
 }
-
 const std::vector<cv::Scalar> PixelFeatureVectorParam::DEFAULT_COLOR_EDGE_VECTOR = 
 createDefaultColorEdgeVector();
 
+// TEMPORARY  - needs scaling by 255 (??)
 std::vector<double> PixelFeatureVectorParam::createDefaultGradMagEdgeVector()
 {
     std::vector<double> gradMagEdgeVector;
     gradMagEdgeVector.push_back(100.0);
     return gradMagEdgeVector;
 }
-
 const std::vector<double> PixelFeatureVectorParam::DEFAULT_GRAD_MAG_EDGE_VECTOR = 
 createDefaultGradMagEdgeVector();
 
+// TEMPORARY 
 std::vector<double> PixelFeatureVectorParam::createDefaultGradOriEdgeVector()
 {
     std::vector<double> gradOriEdgeVector;
     gradOriEdgeVector.push_back(2.61799387799149);
     return gradOriEdgeVector;
 }
-
 const std::vector<double> PixelFeatureVectorParam::DEFAULT_GRAD_ORI_EDGE_VECTOR = 
 createDefaultGradOriEdgeVector();
 
+// TEMPORARY 
+std::vector<BinParam> PixelFeatureVectorParam::createDefaultBinParam()
+{
+    std::vector<BinParam> binParam;
+    binParam.push_back(BinParam(1,5));
+    binParam.push_back(BinParam(3,5));
+    binParam.push_back(BinParam(3,1));
+    return binParam;
+}
+const std::vector<BinParam> PixelFeatureVectorParam::DEFAULT_BIN_PARAM = 
+createDefaultBinParam();
+
 PixelFeatureVectorParam::PixelFeatureVectorParam()
 {
-
+    gradNormRadius = DEFAULT_GRAD_NORM_RADIUS;
+    gradNormConst = DEFAULT_GRAD_NORM_CONST;
+    fillBndryErodeRadius = DEFAULT_FILL_BNDRY_ERODE_RADIUS;
+    colorEdgeVector = DEFAULT_COLOR_EDGE_VECTOR;
+    gradMagEdgeVector = DEFAULT_GRAD_MAG_EDGE_VECTOR;
+    gradOriEdgeVector = DEFAULT_GRAD_ORI_EDGE_VECTOR;
+    binParam = DEFAULT_BIN_PARAM;
 };
 
 // HogPositionFitterParam
 cv::Scalar(0.3703704,0.3259259,0.4962940); 
 const double HogPositionFitterParam::DEFAULT_PAD_BORDER = 1.1;
 
-// TEMPORARY - kludgey, but I just need some data for developing 
-// the fly segmenter. We will read this from a file later.
-// ----------------------------------------------------------------------------
+// TEMPORARY 
 ClassifierParam HogPositionFitterParam::createDefaultOrientClassifierParam()
 {
     ClassifierParam classifierParam;

src/demo/fly_sorter/parameters.hpp

 };
 
 
+class BinParam
+{
+    public:
+        unsigned int numX;
+        unsigned int numY;
+        BinParam();
+        BinParam(unsigned int numX_, unsigned int numY_);
+};
+
 class PixelFeatureVectorParam
 {
     public:
+
+        unsigned int gradNormRadius;
+        static const unsigned int DEFAULT_GRAD_NORM_RADIUS;
+
+        double gradNormConst;
+        static const double DEFAULT_GRAD_NORM_CONST;
+
+        unsigned int fillBndryErodeRadius;
+        static const unsigned int DEFAULT_FILL_BNDRY_ERODE_RADIUS;
+
         std::vector<cv::Scalar> colorEdgeVector;
         static const std::vector<cv::Scalar> DEFAULT_COLOR_EDGE_VECTOR;
         static std::vector<cv::Scalar> createDefaultColorEdgeVector();
         static const std::vector<double> DEFAULT_GRAD_ORI_EDGE_VECTOR;
         static std::vector<double> createDefaultGradOriEdgeVector();
 
+        std::vector<BinParam> binParam;
+        static const std::vector<BinParam> DEFAULT_BIN_PARAM;
+        static std::vector<BinParam> createDefaultBinParam();
+
         PixelFeatureVectorParam();
 };
 
         static const ClassifierParam DEFAULT_ORIENT_CLASSIFIER_PARAM;
         static ClassifierParam createDefaultOrientClassifierParam();
 
+        PixelFeatureVectorParam pixelFeatureVector;
+
         HogPositionFitterParam();
 };