Advertisement
Guest User

Untitled

a guest
Jan 19th, 2020
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.31 KB | None | 0 0
  1. #include <opencv2/opencv.hpp>
  2. #include <iostream>
  3. #include <vector>
  4.  
  5. using namespace cv;
  6. using namespace std;
  7.  
  8. #define M 5
  9.  
  10. int main()
  11. {
  12. Mat image = imread("Src.png", IMREAD_GRAYSCALE);
  13.  
  14. vector<uchar> histogram;
  15. for (int i = 0; i < 256; ++i)
  16. {
  17. histogram.push_back(0);
  18. }
  19. for (int y = 0; y < image.rows; y++)
  20. {
  21. for (int x = 0; x < image.cols; x++)
  22. {
  23. histogram[image.at<uchar>(y, x)]++;
  24. }
  25. }
  26.  
  27. int N = image.cols * image.rows;
  28. vector<uchar> thresholds;
  29. vector<uchar> tempThresholds;
  30. for (int i = 0; i < M - 1; i++)
  31. {
  32. thresholds.push_back(0);
  33. tempThresholds.push_back(i + 1);
  34. }
  35.  
  36. float minSumSigmaW = 999999999999999.f;
  37. int L = 255;
  38.  
  39. while (tempThresholds[0] < (L - (M - 1)))
  40. {
  41. float sumSigmaW = 0;
  42. int leftBound = 0;
  43. int rightBound = 255;
  44. for (int numberOfCluster = 0; numberOfCluster < M; numberOfCluster++)
  45. {
  46. leftBound = 0;
  47. rightBound = 255;
  48. if (numberOfCluster > 0)
  49. {
  50. leftBound = tempThresholds[numberOfCluster - 1] + 1;
  51. }
  52. if (numberOfCluster < M - 1)
  53. {
  54. rightBound = tempThresholds[numberOfCluster];
  55. }
  56.  
  57. float mu = 0;
  58. float sumNf = 0;
  59. float nf = 0;
  60. for (int f = leftBound; f <= rightBound; f++)
  61. {
  62. nf = histogram[f];
  63. sumNf = sumNf + nf;
  64. mu = mu + f * nf;
  65. }
  66. mu = mu / sumNf;
  67. float q = sumNf / N;
  68. float sigmaW = 0;
  69. for (int f = leftBound; f <= rightBound; f++)
  70. {
  71. sigmaW = sigmaW + (f - mu) * (f - mu) * histogram[f];
  72. }
  73. sumSigmaW = sumSigmaW + sigmaW;
  74. }
  75.  
  76. if (sumSigmaW < minSumSigmaW)
  77. {
  78. minSumSigmaW = sumSigmaW;
  79. for (int i = 0; i < M - 1; ++i)
  80. {
  81. thresholds[i] = tempThresholds[i];
  82. }
  83. }
  84.  
  85. tempThresholds[M - 2]++;
  86. for (int i = 1; i < M - 1; i++)
  87. {
  88. if (tempThresholds[i] == 255)
  89. {
  90. tempThresholds[i - 1]++;
  91. tempThresholds[i] = tempThresholds[i - 1] + 1;
  92. }
  93. }
  94. }
  95.  
  96. for (int i = 0; i < M - 1; i++)
  97. {
  98. if (i == 0)
  99. {
  100. cout << i + 1 << "st treshold = ";
  101. }
  102. else if (i == 1)
  103. {
  104. cout << i + 1 << "nd treshold = ";
  105. }
  106. else if (i == 2)
  107. {
  108. cout << i + 1 << "rd treshold = ";
  109. }
  110. else
  111. {
  112. cout << i + 1 << "th treshold = ";
  113. }
  114. cout << (int)thresholds[i] << endl;
  115. }
  116. imshow("Source image", image);
  117. int amountOfTreshholds = M - 1;
  118. uchar fPerThreshold = 255 / amountOfTreshholds;
  119. for (int y = 0; y < image.rows; y++)
  120. {
  121. for (int x = 0; x < image.cols; x++)
  122. {
  123. uchar f = image.at<uchar>(y, x);
  124. for (int i = 0; i < amountOfTreshholds; i++)
  125. {
  126. if (f < thresholds[i])
  127. {
  128. image.at<uchar>(y, x) = fPerThreshold * i;
  129. break;
  130. }
  131. else if (i == amountOfTreshholds - 1)
  132. {
  133. image.at<uchar>(y, x) = 255;
  134. }
  135. }
  136. }
  137. }
  138. imwrite("result.bmp", image);
  139. imshow("Result image", image);
  140.  
  141. unsigned long maxF = 0;
  142. for (int f = 0; f < 256; f++)
  143. {
  144. if (histogram[f] > maxF)
  145. {
  146. maxF = histogram[f];
  147. }
  148. }
  149. Mat hist = Mat(300, 256, CV_8UC3, Scalar(0, 0, 0));
  150. for (int f = 0; f < 255; f++)
  151. {
  152. line(hist, Point(f, 299), Point(f, (maxF - histogram[f]) * 300 / maxF), Scalar(0, 255, 0));
  153. }
  154. for (int i = 0; i < thresholds.size(); i++)
  155. {
  156. line(hist, Point(thresholds[i], 299), Point(thresholds[i], 0), Scalar(0, 0, 255));
  157. }
  158. imwrite("histogram.bmp", hist);
  159. imshow("Histogram", hist);
  160. waitKey(0);
  161. return 0;
  162. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement