color_cells.h
Go to the documentation of this file.
00001 /*********************************************************************
00002  * Software License Agreement (BSD License)
00003  *
00004  *  Copyright (c) 2010, Georgia Institute of Technology
00005  *  All rights reserved.
00006  *
00007  *  Redistribution and use in source and binary forms, with or without
00008  *  modification, are permitted provided that the following conditions
00009  *  are met:
00010  *
00011  *   * Redistributions of source code must retain the above copyright
00012  *     notice, this list of conditions and the following disclaimer.
00013  *   * Redistributions in binary form must reproduce the above
00014  *     copyright notice, this list of conditions and the following
00015  *     disclaimer in the documentation and/or other materials provided
00016  *     with the distribution.
00017  *   * Neither the name of the Georgia Institute of Technology nor the names of
00018  *     its contributors may be used to endorse or promote products derived
00019  *     from this software without specific prior written permission.
00020  *
00021  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00022  *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00023  *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00024  *  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
00025  *  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00026  *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00027  *  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00028  *  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00029  *  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00030  *  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
00031  *  ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00032  *  POSSIBILITY OF SUCH DAMAGE.
00033  *********************************************************************/
00034 
00035 #ifndef color_cell_h_DEFINED
00036 #define color_cell_h_DEFINED
00037 
00038 #include <opencv2/core/core.hpp>
00039 #include <opencv2/imgproc/imgproc.hpp>
00040 #include "abstract_feature.h"
00041 #include <vector>
00042 
00043 namespace cpl_visual_features
00044 {
00045 class ColorCell : public AbstractFeature<std::vector<float> >
00046 {
00047  public:
00048   ColorCell(int num_scales=4) : num_scales_(num_scales)
00049   {
00050   }
00051 
00052   virtual void operator()(cv::Mat& patch, cv::Rect& window)
00053   {
00054     std::vector<cv::Mat> channels;
00055     cv::split(patch, channels);
00056     std::vector<float> descriptor;
00057 
00058     for (int scale = 1; scale <= num_scales_; ++scale)
00059     {
00060       // Examine cells at each scale
00061       int cell_height = patch.rows / float(scale);
00062       int cell_width = patch.cols / float(scale);
00063 
00064       for (int cell_r = 0, vert_count = 0;
00065            cell_r < patch.rows && vert_count < scale;
00066            cell_r += cell_height, vert_count++)
00067       {
00068         for (int cell_c = 0, horz_count = 0;
00069              cell_c < patch.cols && horz_count < scale;
00070              cell_c += cell_width, horz_count++)
00071         {
00072           float r_avg = 0;
00073           float g_avg = 0;
00074           float b_avg = 0;
00075 
00076           // Compute the average value for each color channel in the cell
00077           for (int r = cell_r; r < cell_r + cell_height; r++)
00078           {
00079             for (int c = cell_c; c < cell_c + cell_height; c++)
00080             {
00081               b_avg += channels[0].at<uchar>(r,c);
00082               g_avg += channels[1].at<uchar>(r,c);
00083               r_avg += channels[2].at<uchar>(r,c);
00084             }
00085           }
00086           r_avg /= float(cell_width*cell_height);
00087           g_avg /= float(cell_width*cell_height);
00088           b_avg /= float(cell_width*cell_height);
00089 
00090           // Add the 6 color descriptors for this cell
00091           descriptor.push_back(r_avg);
00092           descriptor.push_back(g_avg);
00093           descriptor.push_back(b_avg);
00094           descriptor.push_back((r_avg - g_avg)/float(r_avg + g_avg + b_avg));
00095           descriptor.push_back((b_avg - (r_avg + g_avg)/2.0)/
00096                                float(r_avg + g_avg + b_avg));
00097           descriptor.push_back(float(r_avg + g_avg + b_avg)/(255*3));
00098         }
00099       }
00100     }
00101 
00102     descriptor_ = descriptor;
00103   }
00104 
00105   virtual std::vector<float> getDescriptor() const
00106   {
00107     return descriptor_;
00108   }
00109 
00110  protected:
00111   std::vector<float> descriptor_;
00112   int num_scales_;
00113 };
00114 }
00115 #endif // color_cell_h_DEFINED


cpl_visual_features
Author(s): Tucker Hermans
autogenerated on Wed Nov 27 2013 11:52:35