/perception/descriptors_2d/src/hist_test.cpp

https://github.com/emmjaykay/stanford_self_driving_car_code · C++ · 123 lines · 60 code · 18 blank · 45 comment · 7 complexity · cc9ff023965e50a4c970e8ee5a26d6d2 MD5 · raw file

  1. /********************************************************
  2. Stanford Driving Software
  3. Copyright (c) 2011 Stanford University
  4. All rights reserved.
  5. Redistribution and use in source and binary forms, with
  6. or without modification, are permitted provided that the
  7. following conditions are met:
  8. * Redistributions of source code must retain the above
  9. copyright notice, this list of conditions and the
  10. following disclaimer.
  11. * Redistributions in binary form must reproduce the above
  12. copyright notice, this list of conditions and the
  13. following disclaimer in the documentation and/or other
  14. materials provided with the distribution.
  15. * The names of the contributors may not be used to endorse
  16. or promote products derived from this software
  17. without specific prior written permission.
  18. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  19. CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED
  20. WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  21. WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
  22. PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  23. COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
  24. INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  25. CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  26. PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  27. DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  28. CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  29. CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
  30. OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  31. SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  32. DAMAGE.
  33. ********************************************************/
  34. #include <iostream>
  35. #include "opencv/cxcore.h"
  36. #include "opencv/cv.h"
  37. #include "opencv/highgui.h"
  38. #include "opencv/cvaux.hpp"
  39. using namespace std;
  40. void rgbTest(IplImage* img) {
  41. // -- Get RGB
  42. IplImage* r = cvCreateImage( cvGetSize(img), 8, 1 );
  43. IplImage* g = cvCreateImage( cvGetSize(img), 8, 1 );
  44. IplImage* b = cvCreateImage( cvGetSize(img), 8, 1 );
  45. cvSplit(img, b, g, r, 0);
  46. // -- Setup hist.
  47. int num_bins = 8;
  48. float range[] = {0, 256};
  49. float* ranges[] = {range, range};
  50. int sizes[] = {num_bins, num_bins};
  51. CvHistogram* hist = cvCreateHist(2, sizes, CV_HIST_ARRAY, ranges, 1);
  52. IplImage* imgs[] = {r, g};
  53. // -- Compute.
  54. cvCalcHist(imgs, hist, 0, NULL);
  55. // -- Output.
  56. cout << "RGB" << endl;
  57. for(int i=0; i<num_bins; i++) {
  58. for(int j=0; j<num_bins; j++) {
  59. cout << cvQueryHistValue_2D(hist, i, j) << " ";
  60. }
  61. }
  62. cout << endl;
  63. }
  64. int main(int argc, char** argv) {
  65. // -- Get img.
  66. if(argc == 1) {
  67. cout << "Usage: " << argv[0] << " imgname" << endl;
  68. return 1;
  69. }
  70. IplImage* img = cvLoadImage(argv[1]);
  71. if(!img) {
  72. cout << "Could not load image." << endl;
  73. return 1;
  74. }
  75. // -- Get HSV.
  76. IplImage* hsv = cvCreateImage( cvGetSize(img), 8, 3 );
  77. IplImage* hue = cvCreateImage( cvGetSize(img), 8, 1 );
  78. IplImage* sat = cvCreateImage( cvGetSize(img), 8, 1 );
  79. IplImage* val = cvCreateImage( cvGetSize(img), 8, 1 );
  80. cvCvtColor(img, hsv, CV_BGR2HSV);
  81. cvSplit(hsv, hue, sat, val, 0);
  82. // -- Setup hist.
  83. int num_bins = 8;
  84. float huerange[] = {0, 181};
  85. float satrange[] = {0, 256};
  86. float* ranges[] = {huerange, satrange};
  87. int sizes[] = {num_bins, num_bins};
  88. CvHistogram* hist = cvCreateHist(2, sizes, CV_HIST_ARRAY, ranges, 1);
  89. IplImage* imgs[] = {hue, sat};
  90. // -- Compute.
  91. cvCalcHist(imgs, hist, 0, NULL);
  92. // -- Output.
  93. cout << "HSV" << endl;
  94. for(int i=0; i<num_bins; i++) {
  95. for(int j=0; j<num_bins; j++) {
  96. cout << cvQueryHistValue_2D(hist, i, j) << " ";
  97. }
  98. }
  99. cout << endl;
  100. rgbTest(img);
  101. return 0;
  102. }