/winxsamp/tutorials/opencv/step002-histgram(winx)/hello.cpp

https://github.com/xushiwei/winx · C++ · 101 lines · 53 code · 19 blank · 29 comment · 6 complexity · 51adf643c6ff491db098dcd9a5e7d99f 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 08:47:54 xushiwei Exp $
  18. // -----------------------------------------------------------------------*/
  19. #include <winx.h>
  20. #include <winx/CommonDialogs.h>
  21. #include <winx/OpenCV.h>
  22. // -------------------------------------------------------------------------
  23. #define HDIM 256 // bin of HIST, default = 256
  24. int APIENTRY WinMain(HINSTANCE hInstance,
  25. HINSTANCE hPrevInstance,
  26. LPSTR lpCmdLine,
  27. int nCmdShow)
  28. {
  29. EnableMemoryLeakCheck();
  30. IplImage *src = 0, *dst = 0;
  31. CvHistogram *hist = 0;
  32. int n = HDIM;
  33. double nn[HDIM];
  34. uchar T[HDIM];
  35. CvMat *T_mat;
  36. int x;
  37. int sum = 0; // sum of pixels of the source image ͼÏñÖÐÏóËØµãµÄ×ܺÍ
  38. double val = 0;
  39. winx::OpenFileDialog dlg(_T("Image Files(*.jpg;*.png)\0*.jpg;*.png\0All Files(*.*)\0*.*\0"));
  40. if (IDOK != dlg.DoModal())
  41. return -1;
  42. if ((src=cvLoadImage(dlg.lpstrFile, 0)) == NULL) // force to gray image
  43. return -1;
  44. // calculate histgram ¼ÆËãÖ±·½Í¼
  45. hist = cvCreateHist( 1, &n, CV_HIST_ARRAY, 0, 1 );
  46. cvCalcHist( &src, hist, 0, 0 );
  47. // Create Accumulative Distribute Function of histgram
  48. val = 0;
  49. for ( x = 0; x < n; x++)
  50. {
  51. val = val + cvGetReal1D (hist->bins, x);
  52. nn[x] = val;
  53. }
  54. // Compute intensity transformation ¼ÆËã±ä»»º¯ÊýµÄÀëÉ¢ÐÎʽ
  55. sum = src->height * src->width;
  56. for( x = 0; x < n; x++ )
  57. {
  58. T[x] = (uchar) (255 * nn[x] / sum); // range is [0,255]
  59. }
  60. // Do intensity transform for source image
  61. dst = cvCloneImage( src );
  62. T_mat = cvCreateMatHeader( 1, 256, CV_8UC1 );
  63. cvSetData( T_mat, T, 0 );
  64. // directly use look-up-table function Ö±½Óµ÷ÓÃÄÚ²¿º¯ÊýÍê³É look-up-table µÄ¹ý³Ì
  65. cvLUT( src, dst, T_mat );
  66. winx::CvMainFrame mainWnd;
  67. mainWnd.Create(NULL, _T("MainFrame"));
  68. winx::CvWindow* wndSrc = WINX_NEW(winx::CvWindow);
  69. wndSrc->ShowImage(src, mainWnd, _T("source"));
  70. winx::CvWindow* wndResult = WINX_NEW(winx::CvWindow);
  71. wndResult->ShowImage(dst, mainWnd, _T("result"));
  72. RunMsgLoop();
  73. cvReleaseImage( &src );
  74. cvReleaseImage( &dst );
  75. cvReleaseHist ( &hist );
  76. return 0;
  77. }
  78. // -------------------------------------------------------------------------
  79. // $Log: hello.cpp,v $
  80. // Revision 1.1 2006/10/06 08:47:54 xushiwei
  81. // histgram(winx)
  82. //