PageRenderTime 129ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/tutorials/histogram_tutorial_opencv/histogram_tut.cpp

https://bitbucket.org/iankits/opencv_development
C++ | 158 lines | 63 code | 8 blank | 87 comment | 6 complexity | ef33289f6d2566039ef0d028526b6995 MD5 | raw file
  1. /*======================================================================
  2. * Program to understand Historgram and Tracking object using it.
  3. *
  4. * Author: Ankit Singh (ankitsingh.05@gmail.com)
  5. *
  6. * @Input:
  7. * Video file
  8. *
  9. * @Output:
  10. *
  11. * @Usage:
  12. * For compiling and creating executable file and running the executable
  13. * $ ./make_prj
  14. * $ ./histogramTut sample_773-10.mp4
  15. * For cleaning files created during cmake and make command.
  16. * $ . cmake_clean
  17. *=====================================================================*/
  18. #include <cv.h>
  19. #include <highgui.h>
  20. using namespace cv;
  21. int main( int argc, char** argv ){
  22. if( argc < 2 ){
  23. printf( "\n Error! No video data!!! \n" );
  24. return -1;
  25. }
  26. /*
  27. //CvCapture* capture = 0;
  28. //capture = cvCaptureFromCAM(0); // OR
  29. CvCapture* capture = cvCreateFileCapture( argv[1] );
  30. if(!capture){
  31. printf("Capture failure\n");
  32. return -1;
  33. }*/
  34. IplImage* src;
  35. if( argc == 2 && (src=cvLoadImage(argv[1], 1))!= 0) {
  36. // Compute the HSV image and decompose it into separate planes.
  37. //
  38. IplImage* hsv = cvCreateImage( cvGetSize(src), 8, 3 );
  39. cvCvtColor( src, hsv, CV_BGR2HSV );
  40. IplImage* h_plane = cvCreateImage( cvGetSize(src), 8, 1 );
  41. IplImage* s_plane = cvCreateImage( cvGetSize(src), 8, 1 );
  42. IplImage* v_plane = cvCreateImage( cvGetSize(src), 8, 1 );
  43. IplImage* planes[] = { h_plane, s_plane };
  44. cvCvtPixToPlane( hsv, h_plane, s_plane, v_plane, 0 );
  45. // Build the histogram and compute its contents.
  46. //
  47. int h_bins = 30, s_bins = 32;
  48. CvHistogram* hist;
  49. {
  50. int hist_size[] = { h_bins, s_bins };
  51. float h_ranges[] = { 0, 180 }; // hue is [0,180]
  52. float s_ranges[] = { 0, 255 };
  53. float* ranges[] = { h_ranges, s_ranges };
  54. hist = cvCreateHist(
  55. 2,
  56. hist_size,
  57. CV_HIST_ARRAY,
  58. ranges,
  59. 1
  60. );
  61. }
  62. cvCalcHist( planes, hist, 0, 0 ); //Compute histogram
  63. cvNormalizeHist( hist, 1.0 ); //Normalize it
  64. // Create an image to use to visualize our histogram.
  65. //
  66. int scale = 10;
  67. IplImage* hist_img = cvCreateImage(
  68. cvSize( h_bins * scale, s_bins * scale ),
  69. 8,
  70. 3
  71. );
  72. cvZero( hist_img );
  73. // populate our visualization with little gray squares.
  74. //
  75. float max_value = 0;
  76. cvGetMinMaxHistValue( hist, 0, &max_value, 0, 0 );
  77. for( int h = 0; h < h_bins; h++ ) {
  78. for( int s = 0; s < s_bins; s++ ) {
  79. float bin_val = cvQueryHistValue_2D( hist, h, s );
  80. int intensity = cvRound( bin_val * 255 / max_value );
  81. cvRectangle(
  82. hist_img,
  83. cvPoint( h*scale, s*scale ),
  84. cvPoint( (h+1)*scale - 1, (s+1)*scale - 1),
  85. CV_RGB(intensity,intensity,intensity),
  86. CV_FILLED
  87. );
  88. }
  89. }
  90. cvNamedWindow( "Source", 1 );
  91. cvShowImage( "Source", src );
  92. cvNamedWindow( "H-S Histogram", 1 );
  93. cvShowImage( "H-S Histogram", hist_img );
  94. cvWaitKey(0);
  95. }
  96. }
  97. /*
  98. IplImage* frame=0;
  99. frame = cvQueryFrame(capture);
  100. if(!frame) return -1;
  101. //create a blank image and assigned to 'imgTracking' which has the same size of original video
  102. imgTracking=cvCreateImage(cvGetSize(frame),IPL_DEPTH_8U, 3);
  103. cvZero(imgTracking); //covert the image, 'imgTracking' to black
  104. cvNamedWindow("Video");
  105. cvNamedWindow("ObjectTracking");
  106. while(1) {
  107. frame = cvQueryFrame(capture);
  108. if(!frame) break;
  109. frame=cvCloneImage(frame);
  110. cvSmooth(frame, frame, CV_GAUSSIAN,3,3); //smooth the original image using Gaussian kernel
  111. IplImage* imgHSV = cvCreateImage(cvGetSize(frame), IPL_DEPTH_8U, 3);
  112. cvCvtColor(frame, imgHSV, CV_BGR2HSV); //Change the color format from BGR to HSV
  113. IplImage* imgThresh = GetThresholdedImage(imgHSV);
  114. cvSmooth(imgThresh, imgThresh, CV_GAUSSIAN,3,3); //smooth the binary image using Gaussian kernel
  115. //track the possition of the object
  116. trackObject(imgThresh);
  117. // Add the tracking image and the frame
  118. cvAdd(frame, imgTracking, frame);
  119. cvShowImage("ObjectTracking", imgThresh);
  120. cvShowImage("Video", frame);
  121. //Clean up used images
  122. cvReleaseImage(&imgHSV);
  123. cvReleaseImage(&imgThresh);
  124. cvReleaseImage(&frame);
  125. //Wait 10mS
  126. int c = cvWaitKey(10);
  127. //If 'ESC' is pressed, break the loop
  128. if((char)c==27 ) break;
  129. }
  130. cvDestroyAllWindows() ;
  131. cvReleaseImage(&imgTracking);
  132. cvReleaseCapture(&capture);
  133. }
  134. */