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 lab_color_histogram_h_DEFINED
00036 #define lab_color_histogram_h_DEFINED
00037
00038 #include <opencv2/core/core.hpp>
00039 #include <opencv2/imgproc/imgproc.hpp>
00040 #include "abstract_feature.h"
00041 #include <iostream>
00042
00043 namespace cpl_visual_features
00044 {
00045 template<int l_bins, int a_bins, int b_bins> class LabColorHistogram :
00046 public AbstractFeature<std::vector<float> >
00047 {
00048 public:
00049 LabColorHistogram()
00050 {
00051 }
00052
00053 virtual void operator()(cv::Mat& patch, cv::Rect& window)
00054 {
00055
00056 cv::Mat cvt_patch(patch.rows, patch.cols, CV_32FC3);
00057 patch.convertTo(cvt_patch, CV_32F, 1.0/255.0);
00058
00059
00060 cv::Mat lab_patch(patch.rows, patch.cols, CV_32FC3);
00061 cv::cvtColor(cvt_patch, lab_patch, CV_BGR2Lab);
00062
00063 int bins[] = {l_bins, a_bins, b_bins};
00064 int channels[] = {0,1,2};
00065 float l_ranges[] = {0,100};
00066 float a_ranges[] = {-128,128};
00067 float b_ranges[] = {-128,128};
00068 const float* ranges[] = {l_ranges, a_ranges, b_ranges};
00069
00070 cv::MatND patch_desc;
00071
00072 cv::calcHist(&lab_patch, 1, channels, cv::Mat(), patch_desc, 3,
00073 bins, ranges, true, false);
00074
00075 std::vector<float> desc;
00076 float color_sum = 0;
00077
00078 for (int i = 0; i < l_bins; ++i)
00079 {
00080 for (int j = 0; j < a_bins; ++j)
00081 {
00082 for (int k = 0; k < b_bins; ++k)
00083 {
00084 desc.push_back(patch_desc.at<float>(i,j,k));
00085 color_sum += patch_desc.at<float>(i,j,k);
00086 }
00087 }
00088 }
00089
00090 #ifdef NORMALIZE_RESULTS
00091 for (unsigned int i = 0; i < desc.size(); ++i)
00092 {
00093 desc[i] = desc[i] / color_sum;
00094 }
00095 #endif // NORMALIZE_RESULTS
00096
00097 descriptor_ = desc;
00098 }
00099
00100 virtual std::vector<float> getDescriptor() const
00101 {
00102 return descriptor_;
00103 }
00104
00105 protected:
00106 std::vector<float> descriptor_;
00107 };
00108 }
00109 #endif // lab_color_histogram_h_DEFINED