歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> OpenCV中 RGB 轉換到 HSI空間

OpenCV中 RGB 轉換到 HSI空間

日期:2017/3/1 10:26:38   编辑:Linux編程

本文實現了RGB空間轉換到HSI空間,並分別求出S分量(飽和度)、I分量(亮度、光強)

  1. #include "stdafx.h"
  2. #include "highgui.h"
  3. #include"cv.h"
  4. int main(int argc, char *argv[])
  5. {
  6. IplImage *img = cvLoadImage("G:\\huo.jpg");
  7. IplImage *r = cvCreateImage( cvGetSize(img), img->depth, 1);
  8. IplImage *g = cvCreateImage( cvGetSize(img), img->depth, 1);
  9. IplImage *b = cvCreateImage( cvGetSize(img), img->depth, 1);
  10. CvMat *rmat = cvCreateMat( img->height, img->width, CV_32FC1);
  11. CvMat *gmat = cvCreateMat( img->height, img->width, CV_32FC1);
  12. CvMat *bmat = cvCreateMat( img->height, img->width, CV_32FC1);
  13. CvMat *temp1 = cvCreateMat( img->height, img->width, CV_32FC1);
  14. CvMat *temp2 = cvCreateMat( img->height, img->width, CV_32FC1);
  15. CvMat *temp3 = cvCreateMat( img->height, img->width, CV_32FC1);
  16. cvNamedWindow("saturation", 1);
  17. cvNamedWindow("intensity", 1);
  18. cvSplit( img, b, g, r, NULL);
  19. cvConvert(b, bmat);
  20. cvConvert(g, gmat);
  21. cvConvert(r, rmat);
  22. //計算飽和度S
  23. cvMin( bmat, gmat, temp1);
  24. cvMin( rmat, temp1, temp1);
  25. cvAdd( bmat, gmat, temp2 );
  26. cvAdd( temp2, rmat, temp2);
  27. cvDiv( temp1, temp2, temp3, 765.0);
  28. cvAbsDiffS( temp3, temp3, cvScalarAll( 255.0 ));
  29. cvConvert( temp3, r );
  30. //計算亮度I
  31. cvAddWeighted( bmat, 1.0/3.0, rmat, 1.0/3.0, 0.0, bmat );
  32. cvAddWeighted( bmat, 1.0, gmat, 1.0/3.0, 0.0, bmat);
  33. cvConvert( bmat, g );
  34. cvShowImage("saturation", r);
  35. cvShowImage( "intensity", g);
  36. cvWaitKey(0);
  37. cvReleaseImage(&img);
  38. cvDestroyWindow("saturation");
  39. cvDestroyWindow("intensity");
  40. return 0;
  41. }
Copyright © Linux教程網 All Rights Reserved