资源简介
利用霍夫变换实现了在一小段的足球视频中对足球的识别和跟踪,并将追踪到的足球画面截屏按帧保存。里面包含了可以实现的完整的代码和视频文件,运行环境是VS2010和OpenCV2.4.9(VS2012也能运行出来),这个是图像处理课老师给的一个课题,有需要的可以下载参考!
代码片段和文件信息
#include
#include
#include “opencv2/highgui/highgui.hpp“
#include “opencv2/imgproc/imgproc.hpp“
#include
#include
#include
using namespace std;
using namespace cv;
void colorFilter(CvMat *inputImage CvMat *&outputImage)
{
int i j;
IplImage* image = cvCreateImage(cvGetSize(inputImage) 8 3);
cvGetImage(inputImage image);
IplImage* hsv = cvCreateImage( cvGetSize(image) 8 3 );
cvCvtColor(imagehsvCV_BGR2HSV);
int width = hsv->width;
int height = hsv->height;
for (i = 0; i < height; i++)
for (j = 0; j < width; j++)
{
CvScalar s_hsv = cvGet2D(hsv i j);//获取像素点为(j i)点的HSV的值
/*
opencv 的H范围是0~180,红色的H范围大概是(0~8)∪(160180)
S是饱和度,一般是大于一个值S过低就是灰色(参考值S>80),
V是亮度,过低就是黑色,过高就是白色(参考值220>V>50)。
*/
CvScalar s;
// if (!(((s_hsv.val[0]>0)&&(s_hsv.val[0]<8)) || (s_hsv.val[0]>120)&&(s_hsv.val[0]<180)))
if(((s_hsv.val[0]>0)&&(s_hsv.val[0]<180))&&(s_hsv.val[1]>0)&&(s_hsv.val[1]<45)&&(s_hsv.val[2]>220)&&(s_hsv.val[2]<255))
{
s.val[0] =180;
s.val[1]=255;
s.val[2]=255;
cvSet2D(hsv i j s);
}
else
if(!((s_hsv.val[0]>35)&&(s_hsv.val[0]<77)))
{
s.val[0] =0;
s.val[1]=0;
s.val[2]=0;
cvSet2D(hsv i j s);
}
}
outputImage = cvCreateMat( hsv->height hsv->width CV_8UC3 );
cvConvert(hsv outputImage);
cvNamedWindow(“filter“);
cvShowImage(“filter“ hsv);
waitKey(0);
cvReleaseImage(&hsv);
}
bool in_cricle(int i int j Point center int radius)
{
double dis = sqrt((center.x - i)*(center.x - i) + (center.y - j)*(center.y - j));
return dis < radius;
}
void show_histogram()
{
}
string Int_to_String(int n)
{
ostringstream stream;
stream << n; //n为int类型
return stream.str();
}
Mat get_circle(Mat src Point center int radius)
{
Mat tmp = src;
for (int i = 0; i < src.rows; i++)
{
for (int j = 0; j < src.cols; j++)
{
if (!in_cricle(i j center radius))
{
tmp.at(i j)[0] = 0;
tmp.at(i j)[1] = 0;
tmp.at(i j)[2] = 0;
}
}
}
return tmp;
}
int main()
{
VideoCapture capture(“ball.avi“);
if (!capture.isOpened())
{
cout << “error!“ << endl;
return 0;
}
double rate = capture.get(CV_CAP_PROP_FPS);
int delay = 1000 / rate;
Mat framecannygary;
vector circles;
int cnt = 0;
string path_head = “dribble/“;
string path_tail = “.jpg“;
while (true)
{
if (!capture.read(frame))
break;
cvtColor(frame gary CV_BGR2GRAY);
GaussianBlur(gary gary Size(9 9) 2 2);
Canny(gary canny 10 50);
threshold(gary gary 190 255 THRESH_BINARY);
// bitwise_not(gary gary);
imshow(“canny“ canny);
imshow(“gary“ gary);
circles.clear();
// HoughCircles(gary circles CV_HOUGH_GRADIENT 1.5 20 90 20 10 45
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
目录 0 2017-04-12 08:43 hough\
目录 0 2017-04-12 08:43 hough\Debug\
文件 105472 2017-04-11 21:23 hough\Debug\following.exe
文件 731716 2017-04-11 21:23 hough\Debug\following.ilk
文件 2780160 2017-04-11 21:23 hough\Debug\following.pdb
目录 0 2017-04-12 08:53 hough\following\
文件 4869294 2017-04-10 18:29 hough\following\1.avi
目录 0 2017-04-12 08:44 hough\following\Debug\
文件 16912 2017-04-10 18:28 hough\following\Debug\CL.read.1.tlog
文件 748 2017-04-10 18:28 hough\following\Debug\CL.write.1.tlog
文件 2014 2017-04-10 18:28 hough\following\Debug\cl.command.1.tlog
文件 66 2017-04-10 18:28 hough\following\Debug\following.lastbuildstate
文件 734 2017-04-12 08:47 hough\following\Debug\following.log
目录 0 2017-04-12 08:44 hough\following\Debug\following.tlog\
文件 22648 2017-04-11 21:23 hough\following\Debug\following.tlog\CL.read.1.tlog
文件 1198 2017-04-11 21:23 hough\following\Debug\following.tlog\CL.write.1.tlog
文件 1982 2017-04-11 21:23 hough\following\Debug\following.tlog\cl.command.1.tlog
文件 163 2017-04-11 21:23 hough\following\Debug\following.tlog\following.lastbuildstate
文件 9080 2017-04-11 21:23 hough\following\Debug\following.tlog\li
文件 14526 2017-04-11 21:23 hough\following\Debug\following.tlog\li
文件 1130 2017-04-11 21:23 hough\following\Debug\following.tlog\li
文件 340463 2017-04-11 21:23 hough\following\Debug\followmain.obj
文件 2 2017-04-10 18:28 hough\following\Debug\li
文件 2 2017-04-10 18:28 hough\following\Debug\li
文件 2 2017-04-10 18:28 hough\following\Debug\li
文件 2 2017-04-10 18:28 hough\following\Debug\li
文件 2 2017-04-10 18:28 hough\following\Debug\li
文件 2 2017-04-10 18:28 hough\following\Debug\li
文件 2 2017-04-10 18:28 hough\following\Debug\li
文件 2 2017-04-10 18:28 hough\following\Debug\li
文件 2 2017-04-10 18:28 hough\following\Debug\li
............此处省略64个文件信息
评论
共有 条评论