Search code examples
c++opencv

Upgrade OpenCV 4.5 - constants not declared


After upgrading from OpenCV 3.2 to 4.5 I get a couple of compile errors. A few constants seems to have changed names

CV_ADAPTIVE_THRESH_GAUSSIAN_C
CV_FILLED

Error

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

Solution

  • It's a matter of versions. OpenCV started as a heap of C code. Those preprocessor #defines all started with CV_... as a scoping hack.

    OpenCV v2.0 introduced the C++ API. Constants now live in the cv namespace.

    The old #defines were kept in v2 and v3 so people could transition more easily. In OpenCV v4, all the old C API was axed. It is an ex-API *whack*

    Practically, for everything that can't be found, try replacing like so:

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

    Exception: the data type/depth codes (CV_8UC3 ...) are still valid. Bulk string replacement on anything CV_ is not recommended.