clarkk
clarkk

Reputation: 27709

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

Upvotes: 1

Views: 2379

Answers (2)

Bradley Wadas
Bradley Wadas

Reputation: 1

opencv2/imgproc/types_c.h includes the old constants in opencv 4.X, so there is not a need to change any constant that was used in opencv 3.X.

How I solved this problem was finding the opencv package through CMake, then I added #include <opencv2/imgproc/types_c.h> to the header file of where I was getting the XXXXX was not declared in this scope; errors from.

EDIT: There are some functions that have changed names that are not defined in the types_c.h file. These functions will have to be changed according to the answer Christoph posted. (Even though this isn't directly correlated to the question, this is common behavior in OpenCV 3.X -> 4.X migration)

Upvotes: 0

Christoph Rackwitz
Christoph Rackwitz

Reputation: 15456

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.

Upvotes: 3

Related Questions