一、本质上,H的取值范围:0~360 S的取值范围:0~1 V的取值范围:0~255 但是,当图像为32F型的时候,各通道的取值范围是: h:0~360 s:0~1 v:0~255
当图像为8位char型时,各通道的取值范围是: h:0~255 s:0~1 v:0~255
二、代码:
#include "stdafx.h"
#include "cv.h"
#include "highgui.h"
#include "cxcore.h"
int main(int argc, char* argv[])
{
IplImage* src=cvLoadImage("E:\\大四上\\openCV\\图片\\气球.png");
/* IplImage* hsv_img=cvCreateImage(cvGetSize(src),8,3);
IplImage* h_img=cvCreateImage(cvGetSize(src),8,1);
IplImage* s_img=cvCreateImage(cvGetSize(src),8,1);
IplImage* v_img=cvCreateImage(cvGetSize(src),8,1);
cvCvtColor(src,hsv_img,CV_BGR2HSV); //将src这幅图像的BGR的值转换为HSV,并存储在hsv_img这幅图像中
cvSplit(hsv_img,h_img,s_img,v_img,NULL); //将图像分解为4个通道的值
//输出h的值,h的值在0到180之间
for(int i=0;i<h_img->height;i++)
{
for(int j=0;j<h_img->width;j++)
{
float value=cvGetReal2D(h_img,i,j);
printf("%f ",value);
}
printf("\n");
}*/
//输出h的值,h的值在0到360之间
/* IplImage* src_float=cvCreateImage(cvGetSize(src),IPL_DEPTH_32F,3);
IplImage* hsv=cvCreateImage(cvGetSize(src),IPL_DEPTH_32F,3);
cvConvertScale(src,src_float,1.0,0); //把char型转换为float型
IplImage* h=cvCreateImage(cvGetSize(src),IPL_DEPTH_32F,1);
IplImage* s=cvCreateImage(cvGetSize(src),IPL_DEPTH_32F,1);
IplImage* v=cvCreateImage(cvGetSize(src),IPL_DEPTH_32F,1);
cvCvtColor(src_float,hsv,CV_BGR2HSV);
cvSplit(hsv,h,s,v,NULL);
for(int i=0;i<h->height;i++)
{
for(int j=0;j<h->width;j++)
{
float value=cvGetReal2D(h,i,j);
printf("%f ",value);
}
printf("\n");
}*/
//输出h的值在0~255之间
IplImage* src_float=cvCreateImage(cvGetSize(src),IPL_DEPTH_32F,3);
IplImage* hsv=cvCreateImage(cvGetSize(src),IPL_DEPTH_32F,3);
cvConvertScale(src,src_float,1.0,0); //把char型转换为float型
IplImage* h=cvCreateImage(cvGetSize(src),IPL_DEPTH_32F,1);
IplImage* s=cvCreateImage(cvGetSize(src),IPL_DEPTH_32F,1);
IplImage* v=cvCreateImage(cvGetSize(src),IPL_DEPTH_32F,1);
cvCvtColor(src_float,hsv,CV_BGR2HSV);
cvSplit(hsv,h,s,v,NULL);
IplImage* hh=cvCreateImage(cvGetSize(src),8,1);
IplImage* ss=cvCreateImage(cvGetSize(src),8,1);
IplImage* vv=cvCreateImage(cvGetSize(src),8,1);
cvConvertScale(h,hh,(1.0/360.0)*255.0,0);
cvConvertScale(s,ss,255.0,0);
cvConvertScale(v,vv,1.0,0);
for(int y = 0; y < hh->height; y++)
{
for(int x = 0; x < hh->width; x++)
{
float values=cvGetReal2D(hh, y, x);
printf("%f ",values);
}
printf("\n");
}
cvNamedWindow("src");
cvShowImage("src",src);
cvWaitKey(0);
return 0;
}
三、关键函数解析:
1、cvCvtColor(src,hsv_img,CV_BGR2HSV); 颜色空间的转换,将src这幅图像的BGR的值转换为HSV,并存储在hsv_img这幅图像中
2、cvSplit(hsv_img,h_img,s_img,v_img,NULL); 将图像分解为4个通道的值,顺序必须是h、s、v
3、cvConvertScale(src,src_float,1.0,0); 把char型转换为float型,把src这幅图像乘1.0,然后再保存在src_float中
原文:http://www.cnblogs.com/-chx/p/5329500.html