The errors you're encountering stem from OpenCV not recognizing your custom Point2D type. OpenCV's functions, such as cv::fitEllipse, expect specific data types like cv::Point, cv::Point2f, or cv::Point2d. Here's a breakdown of the issue and how to resolve it:
Understanding the Errors
-
Error Message 1:
no member named 'type' in 'cv::DataType<Point2D>'
- Cause: OpenCV's
DataType trait is not specialized for your custom Point2D type. This means OpenCV doesn't know how to handle Point2D within its templated functions and classes.
-
Error Message 2:
incomplete definition of type 'cv::traits::Type<Point2D>'
- Cause: Similar to the first error, this indicates that OpenCV lacks the necessary type traits to work with
Point2D.
Solution
To resolve these errors, you have two primary options:
1. Use OpenCV's Built-in Point Types
Replace your custom Point2D type with one of OpenCV's predefined point types, such as cv::Point, cv::Point2f, or cv::Point2d. These types are fully supported by OpenCV's functions and templates.
Example Modification:
If you have a vector of Point2D objects:
std::vector<Point2D> XY;
// Change it to:
std::vector<cv::Point2f> XY;
And ensure that wherever you construct or manipulate these points, you use OpenCV's point constructors:
XY.emplace_back(cv::Point2f(x, y));
2. Specialize OpenCV's DataType for Point2D (Advanced)
If you must use a custom Point2D type, you need to inform OpenCV how to handle it by specializing the cv::DataType and cv::traits::Type for your type.
Step-by-Step Guide:
-
Define Your Point2D Class:
Ensure it has at least two members representing the coordinates, typically x and y.
struct Point2D {
float x;
float y;
};
-
Specialize cv::DataType and cv::traits::Type:
#include <opencv2/core.hpp>
namespace cv {
template <>
struct DataType<Point2D> {
typedef Point2D value_type;
typedef Point2D work_type;
typedef Point2D channel_type;
static inline int depth() { return CV_32F; } // Assuming float
static inline int type() { return CV_32FC2; }
static inline const char* name() { return "Point2D"; }
};
namespace traits {
template <>
struct Type<Point2D> {
enum { value = DataType<Point2D>::type() };
};
}
}
Note:
- Ensure that the
depth() and type() match the underlying data type of your Point2D. In the example above, it's assumed that Point2D uses float for both x and y (CV_32FC2).
-
Use Your Point2D with OpenCV Functions:
After specialization, OpenCV should recognize Point2D similar to its built-in types.
cv::RotatedRect ellipse = cv::fitEllipse(cv::Mat(XY).reshape(1));
Caution: This method is more advanced and requires a deep understanding of OpenCV's type system. If possible, prefer using OpenCV's built-in types to avoid complications.
Recommendation
For simplicity and to ensure compatibility with OpenCV's extensive library of functions, it's recommended to use OpenCV's built-in point types (cv::Point, cv::Point2f, or cv::Point2d) instead of a custom Point2D type. This approach minimizes potential issues and leverages the full power of OpenCV without additional overhead.
Example Correction
Assuming you decide to switch to cv::Point2f, here's how you might adjust your code:
#include <opencv2/opencv.hpp>
#include <vector>
int main() {
std::vector<cv::Point2f> XY;
// Populate XY with points
// Ensure XY has enough points for fitEllipse
if (XY.size() >= 5) {
cv::RotatedRect ellipse = cv::fitEllipse(cv::Mat(XY).reshape(1));
// Use the ellipse as needed
}
return 0;
}
Final Compilation Command
After making the changes, your compilation command should work as expected:
g++ -std=c++17 -o shape_regularizer regularisation.cpp `pkg-config --cflags --libs opencv4`
If you still encounter issues after these changes, please provide more details or the relevant portions of your regularisation.cpp file for further assistance.