本文實現了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;
- }