一、图像空间的转换
RGB -> HSV 转换公式
在openCV中,H取值范围是 0-360,S是 0-1,V是0-255。
本例显示的H的值是0-180,原因是cvCreateImage()中创建的是8位图像深度的图像。存放值范围是0-255,不足360。
#include "stdafx.h" #include "cv.h" #include "highgui.h" #include "cxcore.h" int _tmain(int argc, _TCHAR* argv[]) { IplImage* src = cvLoadImage("D:\\PERSONAL\\VC++\\OpenCV\\image\\hyhead4.jpg"); IplImage* img_hsv = cvCreateImage(cvGetSize(src),8,3); IplImage* img_h = cvCreateImage(cvGetSize(src),8,1); IplImage* img_s = cvCreateImage(cvGetSize(src),8,1); IplImage* img_v = cvCreateImage(cvGetSize(src),8,1); cvCvtColor(src,img_hsv,CV_BGR2HSV); cvSplit(img_hsv,img_h,img_s,img_v,NULL); for(int y=0;y<img_h->height;y++) { for(int x=0;x<img_h->width;x++) { float value = (float)cvGetReal2D(img_h,y,x);//获取单通道图像上该店的值(这里是H的值) if(value > 178) { printf("%f ",value); } } } cvNamedWindow("BGR"); cvShowImage("BGR",src); cvWaitKey(0); cvReleaseImage(&src); cvDestroyWindow("BGR"); return 0; }
IplImage* src = cvLoadImage("D:\\PERSONAL\\VC++\\OpenCV\\image\\hyhead4.jpg"); /* IplImage* img_hsv = cvCreateImage(cvGetSize(src),8,3); IplImage* img_h = cvCreateImage(cvGetSize(src),8,1); IplImage* img_s = cvCreateImage(cvGetSize(src),8,1); IplImage* img_v = cvCreateImage(cvGetSize(src),8,1); */ IplImage* src_float = cvCreateImage(cvGetSize(src),IPL_DEPTH_32F,3); cvConvertScale(src,src_float,1.0,0); //用于转换数据类型。因子是1.0,偏移是0 详细用法看下面 //把src中RGB的值(int)转换为(float)存放到src_float中 IplImage* img_hsv = cvCreateImage(cvGetSize(src),IPL_DEPTH_32F,3); IplImage* img_h = cvCreateImage(cvGetSize(src),IPL_DEPTH_32F,1); IplImage* img_s = cvCreateImage(cvGetSize(src),IPL_DEPTH_32F,1); IplImage* img_v = cvCreateImage(cvGetSize(src),IPL_DEPTH_32F,1); <span style="white-space:pre"> </span>//注意此时是对<span style="font-family: Arial, Helvetica, sans-serif;">src_float进行由BGR -> HSV的变化</span> cvCvtColor(src_float,img_hsv,CV_BGR2HSV);//转换src(BGR)为(HSV)存入img_hsv中 元素的值可以是int/float cvSplit(img_hsv,img_h,img_s,img_v,NULL); for(int y=0;y<img_h->height;y++) { for(int x=0;x<img_h->width;x++) { float value = (float)cvGetReal2D(img_h,y,x);//获取单通道图像上该店的值(这里是H的值) if(value > 358){ printf("%f ",value); } } }