otsu_implementation.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. #include <iostream>
  2. #include <opencv2/opencv.hpp>
  3. #include <opencv2/imgproc.hpp>
  4. using namespace std;
  5. using namespace cv;
  6. int main(){
  7. // read the image in BGR format
  8. Mat testImage = imread("boat.jpg", 0);
  9. int bins_num = 256;
  10. // Get the histogram
  11. long double histogram[256];
  12. // initialize all intensity values to 0
  13. for(int i = 0; i < 256; i++)
  14. {
  15. histogram[i] = 0;
  16. }
  17. // calculate the no of pixels for each intensity values
  18. for(int y = 0; y < testImage.rows; y++)
  19. for(int x = 0; x < testImage.cols; x++)
  20. histogram[(int)testImage.at<uchar>(y,x)]++;
  21. // Calculate the bin_edges
  22. long double bin_edges[256];
  23. bin_edges[0] = 0.0;
  24. long double increment = 0.99609375;
  25. for(int i = 1; i < 256; i++){
  26. bin_edges[i] = bin_edges[i-1] + increment;
  27. }
  28. // Calculate bin_mids
  29. long double bin_mids[256];
  30. for(int i = 0; i < 256; i++){
  31. bin_mids[i] = (bin_edges[i] + bin_edges[(uchar)(i+1)])/2;
  32. }
  33. // Calculate weight 1 and weight 2
  34. long double weight1[256];
  35. weight1[0] = histogram[0];
  36. for(int i = 1; i < 256; i++){
  37. weight1[i] = histogram[i] + weight1[i-1];
  38. }
  39. int total_sum=0;
  40. for(int i = 0; i < 256; i++){
  41. total_sum = total_sum + histogram[i];
  42. }
  43. long double weight2[256];
  44. weight2[0] = total_sum;
  45. for(int i = 1; i < 256; i++){
  46. weight2[i] = weight2[i-1] - histogram[i - 1];
  47. }
  48. // Calculate mean 1 and mean 2
  49. long double histogram_bin_mids[256];
  50. for(int i = 0; i < 256; i++){
  51. histogram_bin_mids[i] = histogram[i] * bin_mids[i];
  52. }
  53. long double cumsum_mean1[256];
  54. cumsum_mean1[0] = histogram_bin_mids[0];
  55. for(int i = 1; i < 256; i++){
  56. cumsum_mean1[i] = cumsum_mean1[i-1] + histogram_bin_mids[i];
  57. }
  58. long double cumsum_mean2[256];
  59. cumsum_mean2[0] = histogram_bin_mids[255];
  60. for(int i = 1, j=254; i < 256 && j>=0; i++, j--){
  61. cumsum_mean2[i] = cumsum_mean2[i-1] + histogram_bin_mids[j];
  62. }
  63. long double mean1[256];
  64. for(int i = 0; i < 256; i++){
  65. mean1[i] = cumsum_mean1[i] / weight1[i];
  66. }
  67. long double mean2[256];
  68. for(int i = 0, j = 255; i < 256 && j >= 0; i++, j--){
  69. mean2[j] = cumsum_mean2[i] / weight2[j];
  70. }
  71. // Calculate Inter_class_variance
  72. long double Inter_class_variance[255];
  73. long double dnum = 10000000000;
  74. for(int i = 0; i < 255; i++){
  75. Inter_class_variance[i] = ((weight1[i] * weight2[i] * (mean1[i] - mean2[i+1])) / dnum) * (mean1[i] - mean2[i+1]);
  76. }
  77. // Get the maximum value
  78. long double maxi = 0;
  79. int getmax = 0;
  80. for(int i = 0;i < 255; i++){
  81. if(maxi < Inter_class_variance[i]){
  82. maxi = Inter_class_variance[i];
  83. getmax = i;
  84. }
  85. }
  86. cout << "Otsu's algorithm implementation thresholding result: " << bin_mids[getmax];
  87. return 0;
  88. }