PageRenderTime 322ms CodeModel.GetById 27ms RepoModel.GetById 1ms app.codeStats 0ms

/samples/c/demhist.c

https://github.com/hcl3210/opencv
C | 125 lines | 95 code | 23 blank | 7 comment | 10 complexity | a8c3aba9ad0ffcb906fe2356d92b4fe1 MD5 | raw file
Possible License(s): LGPL-2.0, BSD-3-Clause
  1. #ifdef _CH_
  2. #pragma package <opencv>
  3. #endif
  4. #ifndef _EiC
  5. #include "cv.h"
  6. #include "highgui.h"
  7. #include <stdio.h>
  8. #endif
  9. char file_name[] = "baboon.jpg";
  10. int _brightness = 100;
  11. int _contrast = 100;
  12. int hist_size = 64;
  13. float range_0[]={0,256};
  14. float* ranges[] = { range_0 };
  15. IplImage *src_image = 0, *dst_image = 0, *hist_image = 0;
  16. CvHistogram *hist;
  17. uchar lut[256];
  18. CvMat* lut_mat;
  19. /* brightness/contrast callback function */
  20. void update_brightcont( int arg )
  21. {
  22. int brightness = _brightness - 100;
  23. int contrast = _contrast - 100;
  24. int i, bin_w;
  25. float max_value = 0;
  26. /*
  27. * The algorithm is by Werner D. Streidt
  28. * (http://visca.com/ffactory/archives/5-99/msg00021.html)
  29. */
  30. if( contrast > 0 )
  31. {
  32. double delta = 127.*contrast/100;
  33. double a = 255./(255. - delta*2);
  34. double b = a*(brightness - delta);
  35. for( i = 0; i < 256; i++ )
  36. {
  37. int v = cvRound(a*i + b);
  38. if( v < 0 )
  39. v = 0;
  40. if( v > 255 )
  41. v = 255;
  42. lut[i] = (uchar)v;
  43. }
  44. }
  45. else
  46. {
  47. double delta = -128.*contrast/100;
  48. double a = (256.-delta*2)/255.;
  49. double b = a*brightness + delta;
  50. for( i = 0; i < 256; i++ )
  51. {
  52. int v = cvRound(a*i + b);
  53. if( v < 0 )
  54. v = 0;
  55. if( v > 255 )
  56. v = 255;
  57. lut[i] = (uchar)v;
  58. }
  59. }
  60. cvLUT( src_image, dst_image, lut_mat );
  61. cvShowImage( "image", dst_image );
  62. cvCalcHist( &dst_image, hist, 0, NULL );
  63. cvZero( dst_image );
  64. cvGetMinMaxHistValue( hist, 0, &max_value, 0, 0 );
  65. cvScale( hist->bins, hist->bins, ((double)hist_image->height)/max_value, 0 );
  66. /*cvNormalizeHist( hist, 1000 );*/
  67. cvSet( hist_image, cvScalarAll(255), 0 );
  68. bin_w = cvRound((double)hist_image->width/hist_size);
  69. for( i = 0; i < hist_size; i++ )
  70. cvRectangle( hist_image, cvPoint(i*bin_w, hist_image->height),
  71. cvPoint((i+1)*bin_w, hist_image->height - cvRound(cvGetReal1D(hist->bins,i))),
  72. cvScalarAll(0), -1, 8, 0 );
  73. cvShowImage( "histogram", hist_image );
  74. }
  75. int main( int argc, char** argv )
  76. {
  77. // Load the source image. HighGUI use.
  78. src_image = cvLoadImage( argc == 2 ? argv[1] : file_name, 0 );
  79. if( !src_image )
  80. {
  81. printf("Image was not loaded.\n");
  82. return -1;
  83. }
  84. dst_image = cvCloneImage(src_image);
  85. hist_image = cvCreateImage(cvSize(320,200), 8, 1);
  86. hist = cvCreateHist(1, &hist_size, CV_HIST_ARRAY, ranges, 1);
  87. lut_mat = cvCreateMatHeader( 1, 256, CV_8UC1 );
  88. cvSetData( lut_mat, lut, 0 );
  89. cvNamedWindow("image", 0);
  90. cvNamedWindow("histogram", 0);
  91. cvCreateTrackbar("brightness", "image", &_brightness, 200, update_brightcont);
  92. cvCreateTrackbar("contrast", "image", &_contrast, 200, update_brightcont);
  93. update_brightcont(0);
  94. cvWaitKey(0);
  95. cvReleaseImage(&src_image);
  96. cvReleaseImage(&dst_image);
  97. cvReleaseHist(&hist);
  98. return 0;
  99. }
  100. #ifdef _EiC
  101. main(1,"demhist.c");
  102. #endif