颜色直方图特征代码

颜色直方图比较简单,统计图像中的颜色分布。是个常用的全局特征,一般需要的话,可以加上SPM,能有效对空间位置进行编码。

代码如下:

[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. #include <sys/types.h>  
  2. #include <boost/filesystem.hpp>  
  3. #include "opencv2/core/core.hpp"  
  4. #include "opencv2/highgui/highgui.hpp"  
  5. #include "opencv2/nonfree/features2d.hpp"  
  6. #include "opencv2/imgproc/imgproc.hpp"  
  7. #include <boost/algorithm/string.hpp>  
  8. using namespace cv;  
  9. using namespace std;  
  10.   
  11. void compute_colorfeatures(const string& imgPath, vector<float>& features) {  
  12.         Mat mat = imread(imgPath);  
  13.         Mat hsv_img;  
  14.         cvtColor(mat, hsv_img, COLOR_BGR2HSV);  
  15.         int h_bins = 30;  
  16.         int s_bins = 50;  
  17.         int histSize[] = { h_bins, s_bins };  
  18.         // hue varies from 0 to 256, saturation from 0 to 180  
  19.         float s_ranges[] = { 0, 256 };  
  20.         float h_ranges[] = { 0, 180 };  
  21.         const float* ranges[] = { h_ranges, s_ranges };  
  22.         // Use the o-th and 1-st channels  
  23.         int channels[] = { 0, 1 };  
  24.         /// Histograms  
  25.         MatND color_features;  
  26.         /// Calculate the histograms for the HSV images  
  27.         calcHist(&hsv_img, 1, channels, Mat(), color_features, 2, histSize, ranges,  
  28.                         truefalse);  
  29.         normalize(color_features, color_features, 0, 1, NORM_MINMAX, -1, Mat());  
  30.         for (int i = 0; i < color_features.rows; i++) {  
  31.                 for (int j = 0; j < color_features.cols; j++) {  
  32.                         features.push_back(color_features.at<float>(i, j));  
  33.                 }  
  34.         }  
  35. }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值