1
0

cv06_cleanLight.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. #include <windows.h>
  2. #include <opencv2/opencv.hpp>
  3. #include <stdio.h>
  4. #include <string.h>
  5. void readOrWriteConfig(const char *filename, double *alpha, int *beta);
  6. #pragma comment(lib, "opencv_world4100.lib")
  7. using namespace cv;
  8. using namespace std;
  9. int main(int argc, char *argv[])
  10. {
  11. if (argc < 2) {
  12. puts("文稿一键美白App By 蘭雅 捞鱼吧 bbs.lyvba.com\n");
  13. puts("Usage: cleanLight.exe image [new_image]\n");
  14. int i = getchar();
  15. return 8;
  16. }
  17. Mat src = imread(argv[1]);
  18. if (src.empty()) {
  19. puts("Error: Could not open or find the image.\n");
  20. return 1;
  21. }
  22. // 1. 将图像转换为 HSV 色彩空间
  23. Mat hsv;
  24. cvtColor(src, hsv, COLOR_BGR2HSV);
  25. // 分离通道
  26. vector<Mat> hsvChannels;
  27. split(hsv, hsvChannels);
  28. // 2. 对 V 通道进行处理
  29. Mat vChannel = hsvChannels[2];
  30. Mat element = getStructuringElement(MORPH_RECT, Size(3, 3));
  31. int iteration = 9;
  32. // 3. 膨胀和腐蚀操作
  33. Mat dilateMat;
  34. morphologyEx(vChannel, dilateMat, MORPH_DILATE, element, Point(-1, -1), iteration);
  35. Mat erodeMat;
  36. morphologyEx(dilateMat, erodeMat, MORPH_ERODE, element, Point(-1, -1), iteration);
  37. // 4. 计算去阴影效果
  38. Mat calcMat = ~(erodeMat - vChannel);
  39. normalize(calcMat, calcMat, 0, 255, NORM_MINMAX);
  40. // 5. 将处理后的 V 通道与原始 H 和 S 通道合并
  41. hsvChannels[2] = calcMat;
  42. Mat result;
  43. merge(hsvChannels, hsv);
  44. // 6. 将 HSV 转换回 BGR 色彩空间
  45. cvtColor(hsv, result, COLOR_HSV2BGR);
  46. // 设置对比度和亮度参数
  47. double alpha = 1.2; // 对比度因子
  48. int beta = 8; // 亮度偏移
  49. const char *inifile = "cleanLight.ini";
  50. readOrWriteConfig(inifile, &alpha, &beta);
  51. convertScaleAbs(result, result, alpha, beta);
  52. // 7. 将结果保存为图像
  53. char filename[256]="removeShadow_color.png";
  54. if (argc == 3) strcpy(filename, argv[2]);
  55. imwrite(filename, result);
  56. // 去阴影结果保存为图像
  57. imwrite("removeShadow.png", calcMat);
  58. imshow("removeShadow.png", calcMat);
  59. waitKey(50);
  60. MessageBoxA(NULL, "文稿一键美白完成,感谢您的试用!\n原创软件->捞鱼吧 bbs.lyvba.com\n亮度和对比调整请修改cleanLight.ini", "文稿一键美白App By 蘭雅", MB_ICONINFORMATION);
  61. return 0;
  62. }
  63. void readOrWriteConfig(const char *filename, double *alpha, int *beta) {
  64. // 尝试打开文件
  65. FILE *file = fopen(filename, "r");
  66. if (file) {
  67. // 如果文件存在,从中读取变量
  68. fscanf(file, "%lf %d", alpha, beta);
  69. fclose(file);
  70. printf("读取成功: alpha = %.2f, beta = %d\n", *alpha, *beta);
  71. } else {
  72. // 如果文件不存在,创建文件并写入变量
  73. *alpha = 1.2;
  74. *beta = 8;
  75. file = fopen(filename, "w");
  76. if (file) {
  77. fprintf(file, "%.2f %d\n", *alpha, *beta);
  78. fclose(file);
  79. printf("文件未找到,已创建并写入: alpha = %.2f, beta = %d\n", *alpha, *beta);
  80. } else {
  81. perror("无法创建文件");
  82. exit(EXIT_FAILURE);
  83. }
  84. }
  85. }