OpenCV图像特征之颜色直方图

Linux大全评论841 views阅读模式

OpenCV之颜色空间:

颜色空间RGB(Red 红色,Green 绿色,Blue 蓝色)

R的取值范围:0-255

G的取值范围:0-255

B的取值范围:0-255

颜色空间HSV (Hue 色相,Saturation 饱和度,intensity 亮度)

H的取值范围:0-179

S的取值范围:0-255

V的取值范围:0-255

颜色空间HLS (Hue 色相,lightness 亮度,Saturation 饱和度)

H的取值范围:0-179

L的取值范围:0-255

S的取值范围:0-255

--------------------------------------分割线 --------------------------------------

编辑推荐

Ubuntu Linux下安装OpenCV2.4.1所需包 http://www.linuxidc.com/Linux/2012-08/68184.htm

Ubuntu 12.04 安装 OpenCV2.4.2 http://www.linuxidc.com/Linux/2012-09/70158.htm

CentOS下OpenCV无法读取视频文件 http://www.linuxidc.com/Linux/2011-07/39295.htm

Ubuntu 12.04下安装OpenCV 2.4.5总结 http://www.linuxidc.com/Linux/2013-06/86704.htm

Ubuntu 10.04中安装OpenCv2.1九步曲 http://www.linuxidc.com/Linux/2010-09/28678.htm

基于QT和OpenCV的人脸识别系统 http://www.linuxidc.com/Linux/2011-11/47806.htm

--------------------------------------分割线 --------------------------------------

知道了一些常用的颜色空间各个通道的像素的取值范围,我们下面讨论颜色直方图

一维直方图:
 
比如,我们只计算上图S通道的直方图,并有30个bin。

#include<opencv2\core\core.hpp>
#include<opencv2\highgui\highgui.hpp>
#include<opencv2\imgproc\imgproc.hpp>

using namespace cv;

#include<iostream>

using namespace std;
int main(int argc ,char** argv)
{
 Mat src,hsv;
 if(argc != 2 || !(src = imread(argv[1],1)).data || src.channels() != 3)
  return -1;

 //颜色空间的转换BGR转HSV
 cvtColor(src,hsv,CV_BGR2HSV);

 //把H通道分为30个bin,把S通道等分为32bin
 int hbins  = 30;
 //int sbins = 32;
 //int histSize[] = {hbins,sbins};
 int histSize[] = {hbins};
 //H的取值范围 0-179
 float hranges [] = {0,180};
 //S的取值范围 0-255
 //float sranges [] ={0,255};
 //const float* ranges [] = {hranges,sranges};
 const float* ranges [] = {hranges};
 MatND hist;
 //我们根据图像第一个通道一维直方图
 int channels[] = {0};
 calcHist(&hsv,1,channels,Mat(),hist,1,histSize,ranges,true,false);

 //输出直方图
 cout<<hist<<endl;
 return 0;
}

输出结果,肯定一个30维的向量:

OpenCV图像特征之颜色直方图

企鹅博客
  • 本文由 发表于 2019年9月8日 10:33:28
  • 转载请务必保留本文链接:https://www.qieseo.com/179980.html

发表评论