我的目标是识别图像中存在的所有形状.这个想法是:
示例图片:

我用它fitEllipse()来找到轮廓最合适的椭圆,但结果有点乱:

可能正确的椭圆用蓝色填充,边界椭圆用黄色填充.可能不正确的轮廓用绿色填充,(错误的)边界椭圆是青色.
正如您所看到的,在第一行中限定三角形的椭圆看起来非常适合最佳拟合.第三行中三角形的边界椭圆似乎不是最合适的,但仍然可以作为拒绝不正确椭圆的标准.
但我无法理解为什么剩下的三角形在其轮廓之外完全具有边界椭圆.最坏的情况是最后一行中的第三个三角形:椭圆是完全错误的,但碰巧有一个区域靠近轮廓的区域,因此三角形被错误地识别为椭圆.
我想念什么吗?我的代码:
#include <iostream>
#include <opencv/cv.h>
#include <opencv/highgui.h>
using namespace std;
using namespace cv;
void getEllipses(vector<vector<Point> >& contours, vector<RotatedRect>& ellipses) {
ellipses.clear();
Mat img(Size(800,500), CV_8UC3);
for (unsigned i = 0; i<contours.size(); i++) {
if (contours[i].size() >= 5) {
RotatedRect temp = fitEllipse(Mat(contours[i]));
if (area(temp) <= 1.1 * contourArea(contours[i])) {
//cout << area(temp) << " < 1.1* " << contourArea(contours[i]) << endl;
ellipses.push_back(temp);
drawContours(img, contours, i, Scalar(255,0,0), -1, 8);
ellipse(img, temp, …Run Code Online (Sandbox Code Playgroud)