回答
- 一(見函數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檢測圖像中的圓圈?
- 4. 圖像python中的圓形輪廓檢測opencv
- 5. 在C++中的圓形區域中的Opencv對象檢測
- 6. 如何檢測圖像中的橢圓而不使用openCv中的fitEllipse()?
- 7. 在opencv中檢測半圓
- 8. 檢測彩色圖像中的水平圓形邊緣
- 9. 使用OpenCv檢測圖像中的矩形明亮區域
- 10. 使用openCV檢測圖像中部分模糊的矩形
- 11. Opencv - 圓形圖像扭曲
- 12. 在C++中使用OpenCV檢測剪貼畫或矢量圖像
- 13. 從圖像中檢測三角形,橢圓和矩形
- 14. 使用OpenCV檢測圖像上人物的長方形肖像
- 15. 如何使用opencv檢測圖像中的文本樣式?
- 16. 圓檢測用的OpenCV
- 17. c#檢測圖像中的矩形
- 18. 如何檢測圖像中的形狀?
- 19. 使用openCV與java從圖像中提取Android圓形區域
- 20. 形狀檢測爲在OpenCV中設定點的(不使用圖像)?
- 21. 使用openCV檢測特定顏色(或灰度級)的圓形
- 22. 圖像邊緣/形狀檢測在OpenCV中
- 23. OpenCV C++:如何找到圖像中的所有圓圈
- 24. 檢測圖像中的切口/半圓
- 25. 如何在OpenCV中保存檢測到的對象的圖像?
- 26. Java中的OpenCV橢圓檢測問題
- 27. OpenCV:如何檢測圖像上的菱形?
- 28. 使用OpenCV在android中檢測圖像的邊緣?
- 29. Android,OpenCV:檢測圖像中的人臉
- 30. 如何使用PHP檢測圖像中的連續形狀?
我覺得你的問題太研究者廣闊d。 –
**查找輪廓** - > **擬合橢圓** –
霍夫圓圈不起作用duh ....首先沒有圓圈。你唯一的賭注是近似值,使用省略號 –