c++ - opencv:在二值图像中拟合最小封闭椭圆

标签 c++ opencv image-processing

我已经使用 OpenCV 和 grabcut 实现生成了前景的二进制掩码。这表示为 CV_8UC1 的 opencv 矩阵,其中属于前景的像素值为 255,背景为零(即它是二进制掩码)。所以,一张像附加的图像:enter image description here

我想找到这个蒙版图像的最小封闭椭圆。我在网上找到的例子似乎有点复杂,我无法将其转化为我的需要。我尝试简单地使用

// result is my OpenCV array of 
cv::RotatedRect e = cv::fitEllipse(result);

OpenCV Error: Assertion failed (points.checkVector(2) >= 0 && 
(points.depth() == CV_32F || points.depth() == CV_32S)) in fitEllipse, 
file /home/luca/Downloads/opencv-2.4.10/modules/imgproc
/src/contours.cpp, line 2019

terminate called after throwing an instance of 'cv::Exception'
what():  /home/luca/Downloads/opencv-2.4.10/modules/imgproc
/src/contours.cpp:2019: error: (-215) points.checkVector(2) >= 0 && 
(points.depth() == CV_32F || points.depth() == CV_32S) in function 
fitEllipse

即使我将其转换为 32 位有符号整数,错误仍然存​​在:

cv::Mat r;
result.convertTo(r, CV_32S);
cv::RotatedRect e = cv::fitEllipse(r);

最佳答案

函数fitEllipse采用 cv::Point 数组,而不是图像。所以你想通过 findContours 运行你的图像第一的。请注意,findContours 会修改图像,因此您可能需要先复制一份。

std::vector< std::vector<cv::Point> > contours;
cv::Mat tmp = result.clone();
cv::findContours(tmp, contours, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_NONE); // edited to change from CV_CHAIN_APPROX_SIMPLE
cv::RotatedRect e = cv::fitEllipse(contours[0]);

以上假设您的图像中只有一个轮廓。您可能想在 contours 中搜索最大的轮廓(使用大小或面积)以防出现任何噪音(并验证您是否至少获得了一个轮廓)。

关于c++ - opencv:在二值图像中拟合最小封闭椭圆,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30194640/

相关文章:

opencv - OpenCV数据类型

c#将图像格式转换为jpg

python - OpenCV:如何在图像上绘制圆形轮廓

在 C 程序中使用的 C++ DLL

c++使用带有枚举类和重载转换运算符的模板函数

c++ - 这个 C++ 模板函数在做什么

c++ - operator void*() 转换仍然是 C++ 库的一部分吗?

c++ - OpenCV Mat::operator= - 它支持写时复制吗?

python - 在 Python 中使用音频流 RTMP 通过管道和 OpenCV 到 FFmpeg

Python OpenCV 如何去除行与行之间的空格