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 hsv_color_histogram_h_DEFINED
00036 #define hsv_color_histogram_h_DEFINED
00037
00038 #include <opencv2/core/core.hpp>
00039 #include <opencv2/imgproc/imgproc.hpp>
00040 #include "abstract_feature.h"
00041 namespace cpl_visual_features
00042 {
00043 template<int h_bins, int s_bins, int v_bins> class HSVColorHistogram :
00044 public AbstractFeature<std::vector<float> >
00045 {
00046 public:
00047 HSVColorHistogram()
00048 {
00049 }
00050
00051 virtual void operator()(cv::Mat& patch, cv::Rect& window)
00052 {
00053 cv::Mat hsv_patch(patch.rows, patch.cols, CV_32FC3);
00054 cv::cvtColor(patch, hsv_patch, CV_BGR2HSV);
00055
00056 int bins[] = {h_bins, s_bins, v_bins};
00057 int channels[] = {0,1,2};
00058 float h_ranges[] = {0,256};
00059 float s_ranges[] = {0,256};
00060 float v_ranges[] = {0,256};
00061 const float* ranges[] = {h_ranges, s_ranges, v_ranges};
00062
00063 cv::MatND patch_desc;
00064
00065 cv::calcHist(&hsv_patch, 1, channels, cv::Mat(), patch_desc, 3,
00066 bins, ranges, true, false);
00067
00068 std::vector<float> desc;
00069 float color_sum = 0;
00070
00071 for (int i = 0; i < h_bins; ++i)
00072 {
00073 for (int j = 0; j < s_bins; ++j)
00074 {
00075 for (int k = 0; k < v_bins; ++k)
00076 {
00077 desc.push_back(patch_desc.at<float>(i,j,k));
00078 color_sum += patch_desc.at<float>(i,j,k);
00079 }
00080 }
00081 }
00082
00083 for (unsigned int i = 0; i < desc.size(); ++i)
00084 {
00085 desc[i] = desc[i] / color_sum;
00086 }
00087
00088 descriptor_ = desc;
00089 }
00090
00091 virtual std::vector<float> getDescriptor() const
00092 {
00093 return descriptor_;
00094 }
00095
00096 protected:
00097 std::vector<float> descriptor_;
00098 };
00099 }
00100 #endif // hsv_color_histogram_h_DEFINED