Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
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
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
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
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