升级 OpenCV 4.5 - 未声明常量

Upgrade OpenCV 4.5 - constants not declared

从 OpenCV 3.2 升级到 4.5 后,我遇到了几个编译错误。几个常量好像改名了

CV_ADAPTIVE_THRESH_GAUSSIAN_C
CV_FILLED

错误

g++ -O3 -std=c++17 txtbin.cpp -o txtbin `pkg-config opencv4 --cflags --libs`
In file included from txtbin.hpp:8,
                 from txtbin.cpp:11:
deskew.hpp: In member function ‘cv::Mat Deskew::preprocess(const cv::Mat&)’:
deskew.hpp:85:42: error: ‘CV_ADAPTIVE_THRESH_GAUSSIAN_C’ was not declared in this scope
   85 |  cv::adaptiveThreshold(img, thresh, 255, CV_ADAPTIVE_THRESH_GAUSSIAN_C, cv::THRESH_BINARY, 15, -2);
      |                                          ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
In file included from txtbin.cpp:11:
txtbin.hpp: In member function ‘void Txtbin::remove_boxes(cv::Mat&)’:
txtbin.hpp:217:79: error: ‘CV_FILLED’ was not declared in this scope; did you mean ‘CLD_KILLED’?
  217 |       cv::drawContours(mask, contours.contours, i, cv::Scalar(255, 255, 255), CV_FILLED);
      |                                                                               ^~~~~~~~~
      |                                                                               CLD_KILLED
txtbin.hpp: In member function ‘void Txtbin::remove_noise(cv::Mat&)’:
txtbin.hpp:262:78: error: ‘CV_FILLED’ was not declared in this scope; did you mean ‘CLD_KILLED’?
  262 |      cv::drawContours(mask, contours.contours, i, cv::Scalar(255, 255, 255), CV_FILLED);
      |                                                                              ^~~~~~~~~
      |                                                                              CLD_KILLED
txtbin.hpp: In member function ‘void Txtbin::remove_artifacts(cv::Mat&)’:
txtbin.hpp:289:77: error: ‘CV_FILLED’ was not declared in this scope; did you mean ‘CLD_KILLED’?
  289 |     cv::drawContours(mask, contours.contours, i, cv::Scalar(255, 255, 255), CV_FILLED);
      |                                                                             ^~~~~~~~~
      |                                                                             CLD_KILLED
txtbin.hpp: In member function ‘Txtbin::Bbox Txtbin::detect_textblock()’:
txtbin.hpp:352:76: error: ‘CV_FILLED’ was not declared in this scope; did you mean ‘CLD_KILLED’?
  352 |    cv::drawContours(mask, contours.contours, i, cv::Scalar(255, 255, 255), CV_FILLED);
      |                                                                            ^~~~~~~~~
      |                                                                            CLD_KILLED
txtbin.hpp: In member function ‘void Txtbin::detect_background_invert()’:
txtbin.hpp:498:79: error: ‘CV_FILLED’ was not declared in this scope; did you mean ‘CLD_KILLED’?
  498 |       cv::drawContours(mask, contours.contours, c, cv::Scalar(255, 255, 255), CV_FILLED);
      |                                                                               ^~~~~~~~~
      |                                                                               CLD_KILLED

这是版本问题。 OpenCV 最初是一堆 C 代码。这些预处理器 #defines 都以 CV_... 作为作用域 hack 开头。

OpenCV v2.0 引入了 C++ API。常量现在位于 cv 命名空间中。

旧的#defines 保留在 v2 和 v3 中,以便人们可以更轻松地转换。在 OpenCV v4 中,所有旧的 C API 都被砍掉了。这是一个 ex-API *whack*

实际上,对于找不到的所有内容,请尝试这样替换:

  • CV_FILLED => cv::FILLED
  • CV_ADAPTIVE_THRESH_GAUSSIAN_C => cv::ADAPTIVE_THRESH_GAUSSIAN_C
  • CV_* => cv::*

异常: 数据 type/depth 代码 (CV_8UC3 ...) 仍然有效CV_ 不推荐.

批量替换字符串