Advertisement
Guest User

Untitled

a guest
Nov 13th, 2018
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.05 KB | None | 0 0
  1. Mat uneq = imread("images/uneq.jpg", CV_LOAD_IMAGE_COLOR);
  2. if (uneq.empty()) printf("Unable to read input file (%s, %d).", __FILE__, __LINE__);
  3.  
  4. cvtColor(uneq, uneq, CV_BGR2GRAY);
  5. const int L = 256;
  6. int p[L];
  7. for (int l = 0; l < L; l++)
  8. p[l] = 0;
  9. for (int j = 0; j < uneq.rows; j++)
  10. for (int i = 0; i < uneq.cols; i++)
  11. for (int l = 0; l < L; l++)
  12. if (uneq.at<uchar>(j, i) == l)
  13. p[l]++;
  14.  
  15. int cdf[L];
  16. for (int l = 0; l < L; l++)
  17. cdf[l] = 0;
  18.  
  19. int ipom,index;
  20. int max=0;
  21. ipom = p[0];
  22. cdf[0] = p[0];
  23. for (int l = 1; l < L; l++)
  24. {
  25. if (ipom == 0)
  26. {
  27. ipom = p[l];
  28. index = l;
  29. }
  30. if (p[l] > max)
  31. max = p[l];
  32. cdf[l] = cdf[l - 1] + p[l];
  33. }
  34.  
  35. /*
  36. int soucet = 0;
  37. for (int l = 0; l < L; l++)
  38. {
  39. soucet += p[l];
  40. printf("%d: %d\n", l, p[l]);
  41. }
  42. printf("soucet: %d\n", soucet);
  43. for (int l = 0; l < L; l++)
  44. {
  45. printf("%d: %d\n", l, cdf[l]);
  46. }*/
  47.  
  48. Mat eq(uneq.rows, uneq.cols, CV_8UC1);
  49. double pom;
  50. for (int j = 0; j < eq.rows; j++)
  51. for (int i = 0; i < eq.cols; i++)
  52. eq.at<uchar>(j, i) = round(((float)(cdf[uneq.at<uchar>(j, i)] - cdf[index]) / (float)(eq.rows*eq.cols - cdf[index])) * (L - 1));
  53.  
  54. int pp[L];
  55. for (int l = 0; l < L; l++)
  56. pp[l] = 0;
  57. for (int j = 0; j < eq.rows; j++)
  58. for (int i = 0; i < eq.cols; i++)
  59. for (int l = 0; l < L; l++)
  60. if (eq.at<uchar>(j, i) == l)
  61. pp[l]++;
  62.  
  63. int cdff[L];
  64. for (int l = 0; l < L; l++)
  65. cdff[l] = 0;
  66. for (int l = 1; l < L; l++)
  67. cdff[l] = cdff[l - 1] + pp[l];
  68.  
  69. Mat hist(max, L, CV_8UC1);
  70. Mat hist2(max, L, CV_8UC1);
  71. for (int j = 0; j < hist.rows; j++)
  72. for (int i = 0; i < hist.cols; i++)
  73. hist.at<uchar>(j, i) = 0;
  74. for (int i = 0; i < hist.cols; i++)
  75. for (int j = hist.rows - p[i]; j < hist.rows; j++)
  76. hist.at<uchar>(j, i) = 255;
  77. for (int j = 0; j < hist2.rows; j++)
  78. for (int i = 0; i < hist2.cols; i++)
  79. hist2.at<uchar>(j, i) = 0;
  80. for (int i = 0; i < hist2.cols; i++)
  81. for (int j = hist2.rows - pp[i]; j < hist2.rows; j++)
  82. hist2.at<uchar>(j, i) = 255;
  83.  
  84. Mat chist(uneq.rows*uneq.cols, L, CV_8UC1);
  85. Mat chist2(uneq.rows*uneq.cols, L, CV_8UC1);
  86. for (int j = 0; j < chist.rows; j++)
  87. for (int i = 0; i < chist.cols; i++)
  88. chist.at<uchar>(j, i) = 0;
  89. for (int i = 0; i < chist.cols; i++)
  90. for (int j = chist.rows - cdf[i]; j < chist.rows; j++)
  91. chist.at<uchar>(j, i) = 255;
  92. for (int j = 0; j < chist2.rows; j++)
  93. for (int i = 0; i < chist2.cols; i++)
  94. chist2.at<uchar>(j, i) = 0;
  95. for (int i = 0; i < chist2.cols; i++)
  96. for (int j = chist2.rows - cdff[i]; j < chist2.rows; j++)
  97. chist2.at<uchar>(j, i) = 255;
  98.  
  99.  
  100. namedWindow("Uneq", 0);
  101. namedWindow("Eq", 0);
  102. imshow("Uneq", uneq);
  103. imshow("Eq", eq);
  104. namedWindow("Input Histogram", 0);
  105. imshow("Input Histogram", hist);
  106. namedWindow("Output Histogram 2", 0);
  107. imshow("Output Histogram 2", hist2);
  108. namedWindow("Input Cumulative Histogram", 0);
  109. imshow("Input Cumulative Histogram", chist);
  110. namedWindow("Output Cumulative Histogram 2", 0);
  111. imshow("Output Cumulative Histogram 2", chist2);
  112. waitKey(0);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement