颜色直方图特征代码
2015-07-27 21:48
330 查看
颜色直方图比较简单,统计图像中的颜色分布。是个常用的全局特征,一般需要的话,可以加上SPM,能有效对空间位置进行编码。
代码如下:
[cpp] view
plaincopy
#include <sys/types.h>
#include <boost/filesystem.hpp>
#include "opencv2/core/core.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/nonfree/features2d.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include <boost/algorithm/string.hpp>
using namespace cv;
using namespace std;
void compute_colorfeatures(const string& imgPath, vector<float>& features) {
Mat mat = imread(imgPath);
Mat hsv_img;
cvtColor(mat, hsv_img, COLOR_BGR2HSV);
int h_bins = 30;
int s_bins = 50;
int histSize[] = { h_bins, s_bins };
// hue varies from 0 to 256, saturation from 0 to 180
float s_ranges[] = { 0, 256 };
float h_ranges[] = { 0, 180 };
const float* ranges[] = { h_ranges, s_ranges };
// Use the o-th and 1-st channels
int channels[] = { 0, 1 };
/// Histograms
MatND color_features;
/// Calculate the histograms for the HSV images
calcHist(&hsv_img, 1, channels, Mat(), color_features, 2, histSize, ranges,
true, false);
normalize(color_features, color_features, 0, 1, NORM_MINMAX, -1, Mat());
for (int i = 0; i < color_features.rows; i++) {
for (int j = 0; j < color_features.cols; j++) {
features.push_back(color_features.at<float>(i, j));
}
}
}
代码如下:
[cpp] view
plaincopy
#include <sys/types.h>
#include <boost/filesystem.hpp>
#include "opencv2/core/core.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/nonfree/features2d.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include <boost/algorithm/string.hpp>
using namespace cv;
using namespace std;
void compute_colorfeatures(const string& imgPath, vector<float>& features) {
Mat mat = imread(imgPath);
Mat hsv_img;
cvtColor(mat, hsv_img, COLOR_BGR2HSV);
int h_bins = 30;
int s_bins = 50;
int histSize[] = { h_bins, s_bins };
// hue varies from 0 to 256, saturation from 0 to 180
float s_ranges[] = { 0, 256 };
float h_ranges[] = { 0, 180 };
const float* ranges[] = { h_ranges, s_ranges };
// Use the o-th and 1-st channels
int channels[] = { 0, 1 };
/// Histograms
MatND color_features;
/// Calculate the histograms for the HSV images
calcHist(&hsv_img, 1, channels, Mat(), color_features, 2, histSize, ranges,
true, false);
normalize(color_features, color_features, 0, 1, NORM_MINMAX, -1, Mat());
for (int i = 0; i < color_features.rows; i++) {
for (int j = 0; j < color_features.cols; j++) {
features.push_back(color_features.at<float>(i, j));
}
}
}
相关文章推荐
- 正确lua简单的扩展,可以加速相关C++数据。
- django 1.8 官方文档翻译: 3-6-1 中间件概览
- 代码注释总结
- Asp.net-MyFirstMVCProject详细解释
- 小飞的电梯调度算法
- 数组和指针的区别
- 使用Maven的插件测试
- caffe python批量抽取图像特征
- 助手系列之python的FTP服务器
- iOS开发分分钟搞定C语言——函数
- 随手小代码——二分法排序
- python变量、运算符、表达式、数据类型
- C++之“友元类”学习笔记
- 谷歌注册多个与Google Play和 China相关域名
- [LeetCode] Sqrt(x)
- Java学习--(一)环境配置与Eclipse安装
- Python脚本调试方法
- 有关C++中为什么用模板类的原因
- caffe c++批量抽取特征
- Java 用线程池实现web服务器