/winxsamp/tutorials/opencv/step001-histgram/hello.cpp

https://github.com/xushiwei/winx · C++ · 94 lines · 48 code · 17 blank · 29 comment · 6 complexity · 2c0e388ca278a1a7fc009f74d6e7bb75 MD5 · raw file

  1. /* -------------------------------------------------------------------------
  2. // WINX: a C++ template GUI library - MOST SIMPLE BUT EFFECTIVE
  3. //
  4. // This file is a part of the WINX Library.
  5. // The use and distribution terms for this software are covered by the
  6. // Common Public License 1.0 (http://opensource.org/licenses/cpl.php)
  7. // which can be found in the file CPL.txt at this distribution. By using
  8. // this software in any fashion, you are agreeing to be bound by the terms
  9. // of this license. You must not remove this notice, or any other, from
  10. // this software.
  11. //
  12. // Module: hello.cpp
  13. // Creator: xushiwei
  14. // Email: xushiweizh@gmail.com
  15. // Date: 2006-8-19 18:26:15
  16. //
  17. // $Id: hello.cpp,v 1.1 2006/10/06 05:00:21 xushiwei Exp $
  18. // -----------------------------------------------------------------------*/
  19. #include <winx/CommonDialogs.h>
  20. #include <winx/OpenCV.h>
  21. // -------------------------------------------------------------------------
  22. #define HDIM 256 // bin of HIST, default = 256
  23. int main()
  24. {
  25. IplImage *src = 0, *dst = 0;
  26. CvHistogram *hist = 0;
  27. int n = HDIM;
  28. double nn[HDIM];
  29. uchar T[HDIM];
  30. CvMat *T_mat;
  31. int x;
  32. int sum = 0; // sum of pixels of the source image ͼÏñÖÐÏóËØµãµÄ×ܺÍ
  33. double val = 0;
  34. winx::OpenFileDialog dlg(_T("Image Files(*.jpg;*.png)\0*.jpg;*.png\0All Files(*.*)\0*.*\0"));
  35. if (IDOK != dlg.DoModal())
  36. return -1;
  37. if ((src=cvLoadImage(dlg.lpstrFile, 0)) == NULL) // force to gray image
  38. return -1;
  39. cvNamedWindow( "source", 1 );
  40. cvNamedWindow( "result", 1 );
  41. // calculate histgram ¼ÆËãÖ±·½Í¼
  42. hist = cvCreateHist( 1, &n, CV_HIST_ARRAY, 0, 1 );
  43. cvCalcHist( &src, hist, 0, 0 );
  44. // Create Accumulative Distribute Function of histgram
  45. val = 0;
  46. for ( x = 0; x < n; x++)
  47. {
  48. val = val + cvGetReal1D (hist->bins, x);
  49. nn[x] = val;
  50. }
  51. // Compute intensity transformation ¼ÆËã±ä»»º¯ÊýµÄÀëÉ¢ÐÎʽ
  52. sum = src->height * src->width;
  53. for( x = 0; x < n; x++ )
  54. {
  55. T[x] = (uchar) (255 * nn[x] / sum); // range is [0,255]
  56. }
  57. // Do intensity transform for source image
  58. dst = cvCloneImage( src );
  59. T_mat = cvCreateMatHeader( 1, 256, CV_8UC1 );
  60. cvSetData( T_mat, T, 0 );
  61. // directly use look-up-table function Ö±½Óµ÷ÓÃÄÚ²¿º¯ÊýÍê³É look-up-table µÄ¹ý³Ì
  62. cvLUT( src, dst, T_mat );
  63. cvShowImage( "source", src );
  64. cvShowImage( "result", dst );
  65. cvWaitKey(0);
  66. cvDestroyWindow("source");
  67. cvDestroyWindow("result");
  68. cvReleaseImage( &src );
  69. cvReleaseImage( &dst );
  70. cvReleaseHist ( &hist );
  71. return 0;
  72. }
  73. // -------------------------------------------------------------------------
  74. // $Log: hello.cpp,v $
  75. // Revision 1.1 2006/10/06 05:00:21 xushiwei
  76. // histgram
  77. //