問題描述
我必須使用 OpenCV 編寫一個對象檢測器(在本例中為一個球).問題是,谷歌上的每一次搜索都會給我返回一些帶有人臉檢測的東西.所以我需要關于從哪里開始、使用什么等方面的幫助.
I have to code an object detector (in this case, a ball) using OpenCV. The problem is, every single search on google returns me something with FACE DETECTION in it. So i need help on where to start, what to use etc..
一些信息:
- 球沒有固定的顏色,它可能是白色的,但可能會改變.
- 我必須使用機器學習,不一定是復雜可靠的機器學習,建議是 KNN(它更簡單、更容易).
- 經過我的所有搜索,我發現計算樣本球圖像的直方圖并將其教授給機器學習可能很有用,但我在這里主要擔心的是球的大小可能并且將會改變(離球越來越近,越來越遠)相機),我不知道要傳遞給 ML 什么來為我分類,我的意思是..我不能(或者我可以?)只測試每個可能尺寸的圖像的每個像素(從,可以說,5x5 到 WxH)并希望找到一個積極的結果.
- 可能存在不統一的背景,例如人、球后面的布料等.
- 正如我所說,我必須使用機器學習算法,這意味著沒有 Haar 或 Viola 算法.
所以...建議?
提前致謝.;)
推薦答案
嗯,基本上你需要檢測圈子.你見過 cvHoughCircles()
嗎?可以使用嗎?
Well, basically you need to detect circles. Have you seen cvHoughCircles()
? Are you allowed to use it?
這個頁面有關于如何檢測東西的很好的信息使用 OpenCV.您可能對第 2.5 節一>.
This page has good info on how detecting stuff with OpenCV. You might be more interested on section 2.5.
這是我剛寫的一個小演示,用于檢測這張圖片中的硬幣.希望您可以利用代碼的某些部分來發揮自己的優勢.
This is a small demo I just wrote to detect coins in this picture. Hopefully you can use some part of the code to your advantage.
輸入:
輸出:
// compiled with: g++ circles.cpp -o circles `pkg-config --cflags --libs opencv`
#include <stdio.h>
#include <cv.h>
#include <highgui.h>
#include <math.h>
int main(int argc, char** argv)
{
IplImage* img = NULL;
if ((img = cvLoadImage(argv[1]))== 0)
{
printf("cvLoadImage failed
");
}
IplImage* gray = cvCreateImage(cvGetSize(img), IPL_DEPTH_8U, 1);
CvMemStorage* storage = cvCreateMemStorage(0);
cvCvtColor(img, gray, CV_BGR2GRAY);
// This is done so as to prevent a lot of false circles from being detected
cvSmooth(gray, gray, CV_GAUSSIAN, 7, 7);
IplImage* canny = cvCreateImage(cvGetSize(img),IPL_DEPTH_8U,1);
IplImage* rgbcanny = cvCreateImage(cvGetSize(img),IPL_DEPTH_8U,3);
cvCanny(gray, canny, 50, 100, 3);
CvSeq* circles = cvHoughCircles(gray, storage, CV_HOUGH_GRADIENT, 1, gray->height/3, 250, 100);
cvCvtColor(canny, rgbcanny, CV_GRAY2BGR);
for (size_t i = 0; i < circles->total; i++)
{
// round the floats to an int
float* p = (float*)cvGetSeqElem(circles, i);
cv::Point center(cvRound(p[0]), cvRound(p[1]));
int radius = cvRound(p[2]);
// draw the circle center
cvCircle(rgbcanny, center, 3, CV_RGB(0,255,0), -1, 8, 0 );
// draw the circle outline
cvCircle(rgbcanny, center, radius+1, CV_RGB(0,0,255), 2, 8, 0 );
printf("x: %d y: %d r: %d
",center.x,center.y, radius);
}
cvNamedWindow("circles", 1);
cvShowImage("circles", rgbcanny);
cvSaveImage("out.png", rgbcanny);
cvWaitKey(0);
return 0;
}
圓的檢測很大程度上取決于cvHoughCircles()
的參數.請注意,在這個演示中,我也使用了 Canny.
The detection of the circles depend a lot on the parameters of cvHoughCircles()
. Note that in this demo I used Canny as well.
這篇關于使用 OpenCV 和機器學習進行簡單的對象檢測的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!