Wouter Florijn Wouter Florijn - 28 days ago 11
C++ Question

OpenCV #define type for Mat

I'm using C++ and OpenCV. Now I want to define a certain

Mat
type (such as
CV_32FC3
) to be used by lots of Mats in my code.

To achieve this, I tried:

#define FLOW_TYPE CV_32FC3;

cv::Mat BaoOpticalFlow::initialize(cv::Mat & frame1, cv::Mat & frame2)
{
cv::Mat flow = cv::Mat(frame1.rows, frame1.cols, FLOW_TYPE);
}


However, this causes errors


expected a ')'

expected an expression


Then I tried,

#define FLOW_TYPE CV_32FC3;

cv::Mat BaoOpticalFlow::initialize(cv::Mat & frame1, cv::Mat & frame2)
{
int test = FLOW_TYPE;
cv::Mat flow = cv::Mat(frame1.rows, frame1.cols, test);
}


Which works, but is ugly in my opinion.

I don't really understand why or how this works, and why the first snippet causes errors. I'm not a C++ expert, so any help is appreciated.

Answer

Your mistake is in the ; at the end of the define. Your code must be:

#define FLOW_TYPE CV_32FC3

cv::Mat BaoOpticalFlow::initialize(cv::Mat & frame1, cv::Mat & frame2)
{
    cv::Mat flow = cv::Mat(frame1.rows, frame1.cols, FLOW_TYPE);
}

More nice if you remove at all the define like this:

constexpr auto FLOW_TYPE = CV_32FC3;

cv::Mat BaoOpticalFlow::initialize(cv::Mat & frame1, cv::Mat & frame2)
{
    cv::Mat flow = cv::Mat(frame1.rows, frame1.cols, FLOW_TYPE);
}