本文实现了RGB空间转换到HSI空间,并分别求出S分量(饱和度)、I分量(亮度、光强)
#include "stdafx.h" #include "highgui.h" #include"cv.h" int main(int argc, char *argv[]) { IplImage *img = cvLoadImage("G:\\huo.jpg"); IplImage *r = cvCreateImage( cvGetSize(img), img->depth, 1); IplImage *g = cvCreateImage( cvGetSize(img), img->depth, 1); IplImage *b = cvCreateImage( cvGetSize(img), img->depth, 1); CvMat *rmat = cvCreateMat( img->height, img->width, CV_32FC1); CvMat *gmat = cvCreateMat( img->height, img->width, CV_32FC1); CvMat *bmat = cvCreateMat( img->height, img->width, CV_32FC1); CvMat *temp1 = cvCreateMat( img->height, img->width, CV_32FC1); CvMat *temp2 = cvCreateMat( img->height, img->width, CV_32FC1); CvMat *temp3 = cvCreateMat( img->height, img->width, CV_32FC1); cvNamedWindow("saturation", 1); cvNamedWindow("intensity", 1); cvSplit( img, b, g, r, NULL); cvConvert(b, bmat); cvConvert(g, gmat); cvConvert(r, rmat); //计算饱和度S cvMin( bmat, gmat, temp1); cvMin( rmat, temp1, temp1); cvAdd( bmat, gmat, temp2 ); cvAdd( temp2, rmat, temp2); cvDiv( temp1, temp2, temp3, 765.0); cvAbsDiffS( temp3, temp3, cvScalarAll( 255.0 )); cvConvert( temp3, r ); //计算亮度I cvAddWeighted( bmat, 1.0/3.0, rmat, 1.0/3.0, 0.0, bmat ); cvAddWeighted( bmat, 1.0, gmat, 1.0/3.0, 0.0, bmat); cvConvert( bmat, g ); cvShowImage("saturation", r); cvShowImage( "intensity", g); cvWaitKey(0); cvReleaseImage(&img); cvDestroyWindow("saturation"); cvDestroyWindow("intensity"); return 0; }