Guest User

Untitled

a guest
Jan 3rd, 2018
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.93 KB | None | 0 0
  1. #include "opencv2/opencv.hpp"
  2. #include "opencv2\highgui.hpp"
  3. #include <iostream>
  4.  
  5.  
  6. #ifdef _DEBUG
  7. #pragma comment(lib, "opencv_core331d.lib")
  8. #pragma comment(lib, "opencv_highgui331d.lib")
  9. #pragma comment(lib, "opencv_imgcodecs331d.lib")
  10. #pragma comment(lib, "opencv_objdetect331d.lib")
  11. #pragma comment(lib, "opencv_imgproc331d.lib")
  12. #pragma comment(lib, "opencv_videoio331d.lib")
  13. #else
  14. #pragma comment(lib, "opencv_core331.lib")
  15. #pragma comment(lib, "opencv_highgui331.lib")
  16. #pragma comment(lib, "opencv_imgcodecs331.lib")
  17. #pragma comment(lib, "opencv_objdetect331.lib")
  18. #pragma comment(lib, "opencv_imgproc331.lib")
  19. #pragma comment(lib, "opencv_videoio331.lib")
  20. #endif
  21.  
  22.  
  23. using namespace std;
  24. using namespace cv;
  25.  
  26.  
  27.  
  28. int main()
  29. {
  30.  
  31. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////
  32. Mat imgA, grayImg;
  33. imgA = imread(".\\goonies.jpg"); //.\\cow.jpg"); //
  34. imshow("img1", imgA);
  35.  
  36. //variables preparing
  37. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////
  38. int hbins = 255; //histogram x axis size, that is histSize,
  39. //ex) 2 -> 0~128, 129~256, ex)16 -> 0 ~ 15, 16 ~ 31...,
  40. int channels[] = { 0 }; //index of channel
  41. int histSize[] = { hbins };
  42. float hranges[] = { 0, 255 };
  43. const float* ranges[] = { hranges };
  44.  
  45. MatND HistB, HistG, HistR;
  46. //split rgb
  47. vector<Mat> bgr_planes;
  48. split(imgA, bgr_planes);
  49.  
  50. //cal histogram & normalization
  51. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////
  52. calcHist(&bgr_planes[0], 1, 0, Mat(), HistB, 1, histSize, ranges, true, false);
  53. calcHist(&bgr_planes[1], 1, 0, Mat(), HistG, 1, histSize, ranges, true, false);
  54. calcHist(&bgr_planes[2], 1, 0, Mat(), HistR, 1, histSize, ranges, true, false);
  55.  
  56. normalize(HistB, HistB, 0, 255, CV_MINMAX);
  57. normalize(HistG, HistG, 0, 255, CV_MINMAX);
  58. normalize(HistR, HistR, 0, 255, CV_MINMAX);
  59.  
  60. // Draw the histograms for B, G and R
  61. int hist_w = 500; int hist_h = 255;
  62. int ratio = cvRound((double)hist_w / hbins);
  63. Mat histImage(hist_h, hist_w, CV_8UC3, Scalar(0, 0, 0));
  64.  
  65. int x1, y1;
  66. int x2, y2;
  67. for (int i = 1; i < hbins; i++)
  68. {
  69. x1 = ratio*(i - 1);
  70. y1 = hist_h - cvRound(HistB.at<float>(i - 1));
  71. x2 = ratio*(i);
  72. y2 = hist_h - cvRound(HistB.at<float>(i));
  73.  
  74. //Blue
  75. line(histImage, Point(x1,y1), Point(x2,y2),
  76. CV_RGB(0, 0, 255), 2, 8, 0);
  77.  
  78. //Green
  79. y1 = hist_h - cvRound(HistG.at<float>(i - 1));
  80. y2 = hist_h - cvRound(HistG.at<float>(i));
  81. line(histImage, Point(x1, y1), Point(x2, y2),
  82. CV_RGB(0, 255, 0), 2, 8, 0);
  83.  
  84. //Red
  85. y1 = hist_h - cvRound(HistR.at<float>(i - 1));
  86. y2 = hist_h - cvRound(HistR.at<float>(i));
  87. line(histImage, Point(x1, y1), Point(x2, y2),
  88. CV_RGB(255, 0, 0), 2, 8, 0);
  89. }
  90.  
  91. /// Display
  92. namedWindow("calcHist Demo", CV_WINDOW_AUTOSIZE);
  93. imshow("calcHist Demo", histImage);
  94. waitKey(0);
  95.  
  96. return 0;
  97. }
Add Comment
Please, Sign In to add comment