0
我試圖從攝像機中找到橙色的球。HoughCircle函數是如何工作的?我無法讓它正常工作
我將每幀轉換爲HSV,模糊它,並使用inRange過濾橙色。然後應用HoughCircles函數,但找不到正確的參數。
當它工作時,它不是很穩定:圓不總是被檢測到。
下面是截圖加上設置的代碼:
#include <cv.h>
#include <highgui.h>
#include <stdio.h>
#include <vector>
using namespace cv;
int hmin[2] = {2,0}, smin[2] = {163,104}, vmin[2] = {16,29};
int hmax[2] = {16,25}, smax[2] = {255, 255}, vmax[2] = {212,255};
int p1 = 6, p2 = 12;
int f1 = 9, f2 = 3;
int main() {
VideoCapture cap(0);
if(!cap.isOpened()) {
fprintf(stderr, "ERROR: capture is NULL \n");
return -1;
}
namedWindow("couleur", CV_WINDOW_AUTOSIZE);
namedWindow("filtre1", CV_WINDOW_AUTOSIZE);
createTrackbar("hmin[0]", "filtre1", &hmin[0], 180, NULL);
createTrackbar("smin[0]", "filtre1", &smin[0], 255, NULL);
createTrackbar("vmin[0]", "filtre1", &vmin[0], 255, NULL);
createTrackbar("hmax[0]", "filtre1", &hmax[0], 180, NULL);
createTrackbar("smax[0]", "filtre1", &smax[0], 255, NULL);
createTrackbar("vmax[0]", "filtre1", &vmax[0], 255, NULL);
namedWindow("hough", CV_WINDOW_AUTOSIZE);
createTrackbar("p1", "hough", &p1, 500, NULL);
createTrackbar("p2", "hough", &p2, 500, NULL);
createTrackbar("f1", "hough", &f1, 30, NULL);
createTrackbar("f2", "hough", &f2, 30, NULL);
std::vector<Vec3f> cercle;
while (1) {
Mat frame, hsv, orange;
cap >> frame;
f1 = f1%2?f1:f1+1;
cvtColor(frame, hsv, CV_BGR2HSV);
GaussianBlur(hsv, hsv, Size(f1, f1), f2, f2);
inRange(hsv, Scalar(hmin[0],smin[0],vmin[0]), Scalar(hmax[0],smax[0],vmax[0]), orange);
if(p1 <= 0)
p1 = 1;
if(p2 <= 0)
p2 = 1;
HoughCircles(orange, cercle, CV_HOUGH_GRADIENT, 2, orange.rows/4, p1, p2, 0, 30);
for(uint i = 0; i < cercle.size(); i++) {
Point c(cvRound(cercle[i][0]), cvRound(cercle[i][1]));
int r = cvRound(cercle[i][2]);
circle(frame, c, 3, Scalar(0,255,0), -1, 8, 0);
circle(frame, c, r, Scalar(255,0,0), 3, 8, 0);
printf("%d) %d %d %d\n", i, c.x, c.y, r);
}
printf("---\n");
cercle.clear();
imshow("couleur", frame);
imshow("filtre1", orange);
if ((waitKey(10) & 255) == 27) break;
}
return 0;
}
如果你想找到球只是使用輪廓掃描和cvMomnet – Haris