Guest User

Untitled

a guest
Jul 21st, 2018
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.55 KB | None | 0 0
  1. //
  2. // Created by 叶林 on 2018/5/1.
  3. //
  4.  
  5. #include "HistogramHelper.h"
  6. #include <iostream>
  7. #include <fstream>
  8. using namespace std;
  9.  
  10.  
  11. HistogramHelper::HistogramHelper(){
  12. hisSize = 128;
  13.  
  14. //Match MATLAB imhist
  15. float bin_width = (255.0 - 0.0) / (128 - 1);
  16. float bin_width_half = bin_width / 2;
  17.  
  18. ranges_ptr[0] = 0;
  19. ranges_ptr[1] = bin_width_half;
  20. ranges_ptr[128] = 255.0;
  21.  
  22. for (int i = 2; i < 128; i++)
  23. {
  24. ranges_ptr[i] = ranges_ptr[i - 1] + bin_width;
  25. }
  26.  
  27. ranges = ranges_ptr;
  28.  
  29. }
  30.  
  31. void HistogramHelper::importImage(Mat& curFrame){
  32. image = curFrame;
  33. }
  34.  
  35. Mat HistogramHelper::getHistogram(){
  36. // Mat tmp;
  37. // cv::cvtColor(image, tmp, cv::COLOR_BGR2YCrCb);
  38. // for(int i = 0; i < tmp.rows; i++){
  39. // for(int j =0; j < tmp.cols; j++){
  40. // cout<< tmp.at<float>(i, j)<<endl;
  41. // }
  42. // }
  43.  
  44. split(image, channelsRGB);
  45. Mat result;
  46. calcHist(&channelsRGB[0],1,0,Mat(),outputRGB[0], 1, &hisSize, &ranges, false);
  47. calcHist(&channelsRGB[1],1,0,Mat(),outputRGB[1], 1, &hisSize, &ranges, false);
  48. calcHist(&channelsRGB[2],1,0,Mat(),outputRGB[2], 1, &hisSize, &ranges, false);
  49.  
  50. ofstream in;
  51. in.open("data2.txt", ios::trunc);
  52. for (int j = 0; j < outputRGB[2].rows; j++){
  53. in << outputRGB[2].at<float>(j) << "\n";
  54.  
  55. }
  56. in.close();
  57.  
  58. result.create(outputRGB[0].rows, 3, outputRGB->type());
  59. for (int i = 0; i < 3; ++i) {
  60. for (int j = 0; j < outputRGB[i].rows; ++j) {
  61. result.at<float>(j, i) = outputRGB[i].at<float>(j, 0);
  62. }
  63. }
  64.  
  65. return result;
  66. }
Add Comment
Please, Sign In to add comment