intensity_transform.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. #include "opencv2/core.hpp"
  2. #include "opencv2/imgcodecs.hpp"
  3. #include "opencv2/highgui.hpp"
  4. #include "opencv2/intensity_transform.hpp"
  5. #include <iostream>
  6. using namespace std;
  7. using namespace cv;
  8. using namespace cv::intensity_transform;
  9. namespace
  10. {
  11. static std::string keys =
  12. "{ help h | | Print help message. }"
  13. "{ input i | | Path to the input image. }";
  14. // global variables
  15. Mat g_image;
  16. int g_gamma = 40;
  17. const int g_gammaMax = 500;
  18. Mat g_imgGamma;
  19. const std::string g_gammaWinName = "Gamma Correction";
  20. Mat g_contrastStretch;
  21. int g_r1 = 70;
  22. int g_s1 = 15;
  23. int g_r2 = 120;
  24. int g_s2 = 240;
  25. const std::string g_contrastWinName = "Contrast Stretching";
  26. Mat g_imgBIMEF;
  27. int g_mu = 50;
  28. const int g_muMax = 100;
  29. const std::string g_BIMEFWinName = "BIMEF";
  30. static void onTrackbarGamma(int, void*)
  31. {
  32. float gamma = g_gamma / 100.0f;
  33. gammaCorrection(g_image, g_imgGamma, gamma);
  34. imshow(g_gammaWinName, g_imgGamma);
  35. }
  36. static void onTrackbarContrastR1(int, void*)
  37. {
  38. contrastStretching(g_image, g_contrastStretch, g_r1, g_s1, g_r2, g_s2);
  39. imshow("Contrast Stretching", g_contrastStretch);
  40. }
  41. static void onTrackbarContrastS1(int, void*)
  42. {
  43. contrastStretching(g_image, g_contrastStretch, g_r1, g_s1, g_r2, g_s2);
  44. imshow("Contrast Stretching", g_contrastStretch);
  45. }
  46. static void onTrackbarContrastR2(int, void*)
  47. {
  48. contrastStretching(g_image, g_contrastStretch, g_r1, g_s1, g_r2, g_s2);
  49. imshow("Contrast Stretching", g_contrastStretch);
  50. }
  51. static void onTrackbarContrastS2(int, void*)
  52. {
  53. contrastStretching(g_image, g_contrastStretch, g_r1, g_s1, g_r2, g_s2);
  54. imshow("Contrast Stretching", g_contrastStretch);
  55. }
  56. static void onTrackbarBIMEF(int, void*)
  57. {
  58. float mu = g_mu / 100.0f;
  59. BIMEF(g_image, g_imgBIMEF, mu);
  60. imshow(g_BIMEFWinName, g_imgBIMEF);
  61. }
  62. }
  63. int main(int argc, char **argv)
  64. {
  65. CommandLineParser parser(argc, argv, keys);
  66. const std::string inputFilename = parser.get<String>("input");
  67. parser.about("Use this script to apply intensity transformation on an input image.");
  68. if (parser.has("help") || inputFilename.empty())
  69. {
  70. parser.printMessage();
  71. return 0;
  72. }
  73. // Read input image
  74. g_image = imread(inputFilename);
  75. // Create trackbars
  76. namedWindow(g_gammaWinName);
  77. createTrackbar("Gamma value", g_gammaWinName, &g_gamma, g_gammaMax, onTrackbarGamma);
  78. namedWindow(g_contrastWinName);
  79. createTrackbar("Contrast R1", g_contrastWinName, &g_r1, 256, onTrackbarContrastR1);
  80. createTrackbar("Contrast S1", g_contrastWinName, &g_s1, 256, onTrackbarContrastS1);
  81. createTrackbar("Contrast R2", g_contrastWinName, &g_r2, 256, onTrackbarContrastR2);
  82. createTrackbar("Contrast S2", g_contrastWinName, &g_s2, 256, onTrackbarContrastS2);
  83. namedWindow(g_BIMEFWinName);
  84. createTrackbar("Enhancement ratio mu", g_BIMEFWinName, &g_mu, g_muMax, onTrackbarBIMEF);
  85. // Apply intensity transformations
  86. Mat imgAutoscaled, imgLog;
  87. autoscaling(g_image, imgAutoscaled);
  88. gammaCorrection(g_image, g_imgGamma, g_gamma/100.0f);
  89. logTransform(g_image, imgLog);
  90. contrastStretching(g_image, g_contrastStretch, g_r1, g_s1, g_r2, g_s2);
  91. BIMEF(g_image, g_imgBIMEF, g_mu / 100.0f);
  92. // Display intensity transformation results
  93. imshow("Original Image", g_image);
  94. imshow("Autoscale", imgAutoscaled);
  95. imshow(g_gammaWinName, g_imgGamma);
  96. imshow("Log Transformation", imgLog);
  97. imshow(g_contrastWinName, g_contrastStretch);
  98. imshow(g_BIMEFWinName, g_imgBIMEF);
  99. waitKey(0);
  100. return 0;
  101. }