1.Threshold

  1. #include <opencv2\opencv.hpp>
  2. #include <iostream>
  3. #include <vector>
  4. using namespace std;
  5. using namespace cv;
  6. int main()
  7. {
  8. Mat img = imread("lena.png");
  9. if (img.empty())
  10. {
  11. cout << "请确认图像文件名称是否正确" << endl;
  12. return -1;
  13. }
  14. Mat gray;
  15. cvtColor(img, gray, COLOR_BGR2GRAY);
  16. Mat img_B, img_B_V, gray_B, gray_B_V, gray_T, gray_T_V, gray_TRUNC;
  17. //彩色图像二值化
  18. threshold(img, img_B, 125, 255, THRESH_BINARY);
  19. threshold(img, img_B_V, 125, 255, THRESH_BINARY_INV);
  20. imshow("img_B", img_B);
  21. imshow("img_B_V", img_B_V);
  22. //灰度图BINARY二值化
  23. threshold(gray, gray_B, 125, 255, THRESH_BINARY);
  24. threshold(gray, gray_B_V, 125, 255, THRESH_BINARY_INV);
  25. imshow("gray_B", gray_B);
  26. imshow("gray_B_V", gray_B_V);
  27. //灰度图像TOZERO变换
  28. threshold(gray, gray_T, 125, 255, THRESH_TOZERO);
  29. threshold(gray, gray_T_V, 125, 255, THRESH_TOZERO_INV);
  30. imshow("gray_T", gray_T);
  31. imshow("gray_T_V", gray_T_V);
  32. //灰度图像TRUNC变换
  33. threshold(gray, gray_TRUNC, 125, 255, THRESH_TRUNC);
  34. imshow("gray_TRUNC", gray_TRUNC);
  35. //灰度图像大津法和三角形法二值化
  36. Mat img_Thr = imread("threshold.png", IMREAD_GRAYSCALE);
  37. Mat img_Thr_O, img_Thr_T;
  38. threshold(img_Thr, img_Thr_O, 100, 255, THRESH_BINARY | THRESH_OTSU);
  39. threshold(img_Thr, img_Thr_T, 125, 255, THRESH_BINARY | THRESH_TRIANGLE);
  40. imshow("img_Thr", img_Thr);
  41. imshow("img_Thr_O", img_Thr_O);
  42. imshow("img_Thr_T", img_Thr_T);
  43. //灰度图像自适应二值化
  44. Mat adaptive_mean, adaptive_gauss;
  45. adaptiveThreshold(img_Thr, adaptive_mean, 255, ADAPTIVE_THRESH_MEAN_C, THRESH_BINARY, 55, 0);
  46. adaptiveThreshold(img_Thr, adaptive_gauss, 255, ADAPTIVE_THRESH_GAUSSIAN_C, THRESH_BINARY, 55, 0);
  47. imshow("adaptive_mean", adaptive_mean);
  48. imshow("adaptive_gauss", adaptive_gauss);
  49. waitKey(0);
  50. return 0;
  51. }

图3-15.png

2.LUT

  1. #include <opencv2\opencv.hpp>
  2. #include <iostream>
  3. using namespace std;
  4. using namespace cv;
  5. int main()
  6. {
  7. //LUT查找表第一层
  8. uchar lutFirst[256];
  9. for (int i = 0; i<256; i++)
  10. {
  11. if (i <= 100)
  12. lutFirst[i] = 0;
  13. if (i > 100 && i <= 200)
  14. lutFirst[i] = 100;
  15. if (i > 200)
  16. lutFirst[i] = 255;
  17. }
  18. Mat lutOne(1, 256, CV_8UC1, lutFirst);
  19. //LUT查找表第二层
  20. uchar lutSecond[256];
  21. for (int i = 0; i<256; i++)
  22. {
  23. if (i <= 100)
  24. lutSecond[i] = 0;
  25. if (i > 100 && i <= 150)
  26. lutSecond[i] = 100;
  27. if (i > 150 && i <= 200)
  28. lutSecond[i] = 150;
  29. if (i > 200)
  30. lutSecond[i] = 255;
  31. }
  32. Mat lutTwo(1, 256, CV_8UC1, lutSecond);
  33. //LUT查找表第三层
  34. uchar lutThird[256];
  35. for (int i = 0; i<256; i++)
  36. {
  37. if (i <= 100)
  38. lutThird[i] = 100;
  39. if (i > 100 && i <= 200)
  40. lutThird[i] = 200;
  41. if (i > 200)
  42. lutThird[i] = 255;
  43. }
  44. Mat lutThree(1, 256, CV_8UC1, lutThird);
  45. //拥有三通道的LUT查找表矩阵
  46. vector<Mat> mergeMats;
  47. mergeMats.push_back(lutOne);
  48. mergeMats.push_back(lutTwo);
  49. mergeMats.push_back(lutThree);
  50. Mat LutTree;
  51. merge(mergeMats, LutTree);
  52. //计算图像的查找表
  53. Mat img = imread("lena.png");
  54. if (img.empty())
  55. {
  56. cout << "请确认图像文件名称是否正确" << endl;
  57. return -1;
  58. }
  59. Mat gray, out0, out1, out2;
  60. cvtColor(img, gray, COLOR_BGR2GRAY);
  61. LUT(gray, lutOne, out0);
  62. LUT(img, lutOne, out1);
  63. LUT(img, LutTree, out2);
  64. imshow("out0", out0);
  65. imshow("out1", out1);
  66. imshow("out2", out2);
  67. waitKey(0);
  68. return 0;
  69. }