回答
- 一(见函数CV :: findContours)。
- 您必须分析这些轮廓(根据您的要求检查它)。
P.S.图中的数字绝对不是圆圈。所以我不能确切地说你如何检查收到的轮廓。
从这个图像开始(我去掉了边框):
你可以按照这个方法:
1)使用findContour
得到的轮廓。
2)只保留内部轮廓。您可以这样做,检查由contourArea(..., true)
返回的区域的标志。你会得到2个内部轮廓:
3)现在,你有两个轮廓,你可以找到一个圆圈minEnclosingCircle
(蓝色),或适合椭圆fitEllipse
(红色) :
这里供参考全码:
#include <opencv2/opencv.hpp>
#include <vector>
using namespace std;
using namespace cv;
int main()
{
Mat1b img = imread("path_to_image", IMREAD_GRAYSCALE);
// Get contours
vector<vector<Point>> contours;
findContours(img, contours, RETR_TREE, CHAIN_APPROX_NONE);
// Create output image
Mat3b out;
cvtColor(img, out, COLOR_GRAY2BGR);
Mat3b outContours = out.clone();
// Get internal contours
vector<vector<Point>> internalContours;
for (size_t i = 0; i < contours.size(); ++i) {
// Find orientation: CW or CCW
double area = contourArea(contours[i], true);
if (area >= 0) {
// Internal contour
internalContours.push_back(contours[i]);
// Draw with different color
drawContours(outContours, contours, i, Scalar(rand() & 255, rand() & 255, rand() & 255));
}
}
// Get circles
for (const auto& cnt : internalContours) {
Point2f center;
float radius;
minEnclosingCircle(cnt, center, radius);
// Draw circle in blue
circle(out, center, radius, Scalar(255, 0, 0));
}
// Get ellipses
for (const auto& cnt : internalContours) {
RotatedRect rect = fitEllipse(cnt);
// Draw ellipse in red
ellipse(out, rect, Scalar(0, 0, 255), 2);
}
imshow("Out", out);
waitKey();
return 0;
}
非常感谢你的善意考虑。我试着用你的代码。但是我得到这个。我无法找到错误。 :(“未处理的异常在0x000007FDA3294388(ucrtbase.dll)在ConsoleApplication1.exe中:一个无效的参数传递给一个函数,该函数认为无效的参数是致命的。”此问题发生在行findContours(img,轮廓,RETR_TREE,CHAIN_APPROX_NONE); –
确保你正确加载了输入图像(我的答案中的一个,而不是你问题中的那个)......否则我不知道......这在我的电脑上完美地工作 – Miki
是的,“右击(https://i.stack.imgur.com/7qN0z.png) – Miki
- 1. 检测图像中的圆形图案
- 2. OpenCV - 检测圆形形状
- 3. 如何检测图像中的椭圆而不使用openCv中的fitEllipse()?
- 4. 使用openCV检测图像中部分模糊的矩形
- 5. Opencv - 圆形图像扭曲
- 6. 从图像中检测三角形,椭圆和矩形
- 7. 使用OpenCV检测图像上人物的长方形肖像
- 8. 如何使用opencv检测图像中的文本样式?
- 9. 圆检测用的OpenCV
- 10. c#检测图像中的矩形
- 11. 如何检测图像中的形状?
- 12. 图像边缘/形状检测在OpenCV中
- 13. 使用openCV检测特定颜色(或灰度级)的圆形
- 14. OpenCV C++:如何找到图像中的所有圆圈
- 15. OpenCV:如何检测图像上的菱形?
- 16. Android,OpenCV:检测图像中的人脸
- 17. 检测图像中的切口/半圆
- 18. 使用OpenCV在android中检测图像的边缘?
- 19. 使用OpenCV在ios应用程序中进行圆检测
- 20. 如何使用python3检测图像中的对象形状?
- 21. 如何使用PHP检测图像中的连续形状?
- 22. 使用houghCircles检测小圆圈(OpenCV)
- 23. HoughCircles圆检测使用opencv和python-
- 24. openCV:使用findContours检测圆圈
- 25. OpenCV:使用cvHoughCircle进行圆检测
- 26. 使用Python/OpenCV检测开放圆圈
- 27. 如何用Matlab检测图像中的椭圆?
- 28. 使用OpenCV进行图像检测
- 29. HoughCircles能否在opencv中检测圆圈内的圆圈?
- 30. OpenCV中的图像变形
我觉得你的问题太研究者广阔d。 –
**查找轮廓** - > **拟合椭圆** –
霍夫圆圈不起作用duh ....首先没有圆圈。你唯一的赌注是近似值,使用省略号 –