#include "stdafx.h"
#include "cv.h"
#include "highgui.h"
#include <time.h>
#include <math.h>
#include <ctype.h>
#include <stdio.h>
#include <string.h>
const double MHI_DURATION = 0.1//定义运动跟踪的最大时间
const double MAX_TIME_DELTA = 0.5
const double MIN_TIME_DELTA = 0.05
const int N = 3//定义数组的维度为3
const int CONTOUR_MAX_AERA = 10//定义的阈值
IplImage **buf = 0
int last = 0
IplImage *mhi = 0
CvFilter filter = CV_GAUSSIAN_5x5//高斯卷积滤波
CvConnectedComp *cur_comp, min_comp//定义连通域 *** 作的存储
CvConnectedComp comp//定义连通域 *** 作的存储
CvMemStorage *storage//定义内存分配
CvPoint pt[4]//定义点的存储
/*****************************
*下面update_mhi函数输入img,输出识别结果dst,阈值diff_threshold
*/
void update_mhi( IplImage* img, IplImage* dst, int diff_threshold )
{
double timestamp = clock()/1.//返回从“开启这个程序进程”到“程序中调用clock()函数”时蠢罩之间的CPU时钟计时单元
CvSize size = cvSize(img->width,img->height)//获取图像的宽和高
int i, idx1, idx2
IplImage* silh
IplImage* pyr = cvCreateImage( cvSize((size.width &-2)/2, (size.height &-2)/2), 8, 1 )//
CvMemStorage *stor//申请内存
CvSeq *cont//定义保存数据的结构
/*先进行数据的初始化*/
if( !mhi || mhi->width != size.width || mhi->height != size.height )
{
//分配内存 *** 作:如果buf是空值,则分配存储空间
if( buf == 0 )
{
buf = (IplImage**)malloc(N*sizeof(buf[0]))//利用桐银malloc动态分配内存
memset( buf, 0, N*sizeof(buf[0]))//作用是在一段内存块中填充某个给定的值,此处值为0
}
//创建通道为N=3,大小为size的图像存储
for( i = 0i <Ni++ )
{
cvReleaseImage( &buf[i] )//释放buf
buf[i] = cvCreateImage( size, IPL_DEPTH_8U, 1 )//创建buf[i]
cvZero( buf[i] )//初始化为0
}
cvReleaseImage( &mhi )//释放变量mhi
mhi = cvCreateImage( size, IPL_DEPTH_32F, 1 )//创建mhi,大小为size,深度为IPL_DEPTH_32F,1个通道
cvZero( mhi )///初始化为0
}
cvCvtColor( img, buf[last], CV_BGR2GRAY )//将RGB图像img转换成gray灰度图像buf
idx1 = last//将last赋值到idx1
idx2 = (last + 1) % N//计算(last + 1)除以N的余数
last = idx2//将idx2赋值到last
silh = buf[idx2]//将buf[idx2]赋值到silh
//下面计算buf[idx1]与buf[idx2]差的绝对值,输出结果存入带轮闹silh
cvAbsDiff( buf[idx1], buf[idx2], silh )
//下面对单通道数组silh应用固定阈值 *** 作,阈值为30,阈值化类型为CV_THRESH_BINARY最大值为255
cvThreshold( silh, silh, 30, 255, CV_THRESH_BINARY )
//去掉影像(silh) 以更新运动历史图像为mhi,当前时间为timestamp,运动跟踪的最大时间为MHI_DURATION=0.1
cvUpdateMotionHistory( silh, mhi, timestamp, MHI_DURATION )
//下面对mhi进行线性变换 *** 作,输出结果存入dst:dst(I)=mhi(I)*第二个参数 + 第三个参数
cvCvtScale( mhi, dst, 255./MHI_DURATION,
(MHI_DURATION - timestamp)*255./MHI_DURATION )
cvCvtScale( mhi, dst, 255./MHI_DURATION, 0 )
cvSmooth( dst, dst, CV_MEDIAN, 3, 0, 0, 0 )//对dst进行中值滤波
cvPyrDown( dst, pyr, 7 )//利用卷积滤波器对dst进行下采样
cvDilate( pyr, pyr, 0, 1 )//对图像pyr使用3*3长方形进行膨胀 *** 作
cvPyrUp( pyr, dst, 7 )//利用卷积滤波器对dst进行上采样
stor = cvCreateMemStorage(0)//动态内存存储创建内存块
cont = cvCreateSeq(CV_SEQ_ELTYPE_POINT, sizeof(CvSeq), sizeof(CvPoint) , stor)//创建存储结构
//函数cvFindContours为寻找图像dst的角点,数据存入cont中,其中包含角点的坐标值
cvFindContours( dst, stor, &cont, sizeof(CvContour),
CV_RETR_LIST, CV_CHAIN_APPROX_SIMPLE, cvPoint(0,0))
for(contcont = cont->h_next)
{
CvRect r = ((CvContour*)cont)->rect//创建矩形区域
if(r.height * r.width >CONTOUR_MAX_AERA)
{
//下面是在图像Img上绘制红色的矩形框,利用左上点和右下点
cvRectangle( img, cvPoint(r.x,r.y),
cvPoint(r.x + r.width, r.y + r.height),
CV_RGB(255,0,0), 1, CV_AA,0)
}
}
cvReleaseMemStorage(&stor)//释放内存
cvReleaseImage( &pyr )//释放结构体
}
int _tmain(int argc, _TCHAR* argv[])
{
IplImage* motion = 0
CvCapture* capture = 0
capture = cvCaptureFromFile("D://Capture1.avi")//获取视频文件
if( capture )
cvNamedWindow( "视频分析", 1 )//创建窗口
{
for()
{
IplImage* image
if( !cvGrabFrame( capture ))//如果读取视频失败,则退出
break
image = cvRetrieveFrame( capture )//获取图像
if( image )
{
if( !motion )
{
motion = cvCreateImage( cvSize(image->width,image->height), 8, 1 )
cvZero( motion )
motion->origin = image->origin
}
}
update_mhi( image, motion, 60)//运动目标检测,阈值为60
cvShowImage( "视频分析", image )//在窗口中显示图像
if( cvWaitKey(10) >= 0 )
break
}
cvReleaseCapture( &capture )//释放
cvDestroyWindow( "视频分析" )//释放窗口
}
return 0
}
记得给我分,急需#include "cv.h"
#include <cxcore.h>
#include "highgui.h"
#include <time.h>
#include <math.h>
#include <ctype.h>
#include <stdio.h>
#include <string.h>
// various tracking parameters (in seconds)
const double MHI_DURATION = 0.5
const double MAX_TIME_DELTA = 0.5
const double MIN_TIME_DELTA = 0.05
const int N = 3
//
const int CONTOUR_MAX_AERA = 16
// ring image buffer
IplImage **buf = 0
int last = 0
// temporary images
IplImage *mhi = 0
/锋缓袜/ MHI: motion history image
int filter = CV_GAUSSIAN_5x5
CvConnectedComp *cur_comp, min_comp
CvConnectedComp comp
CvMemStorage *storageCvPoint pt[4]
// 参数:
// img – 输入视频帧
// dst – 检测结果
void update_mhi( IplImage* img, IplImage* dst, int diff_threshold )
{
double timestamp = clock()/100.
/银激/ get current time in seconds
CvSize size = cvSize(img->width,img->哪嫌height)
// get current frame size
int i, j, idx1, idx2
IplImage* silh
uchar val
float temp
IplImage* pyr = cvCreateImage( cvSize((size.width &-2)/2, (size.height &-2)/2), 8, 1 )
CvMemStorage *stor
CvSeq *cont, *result, *squares
CvSeqReader reader
if( !mhi || mhi->width != size.width || mhi->height != size.height )
{
if( buf == 0 )
{
buf = (IplImage**)malloc(N*sizeof(buf[0]))
memset( buf, 0, N*sizeof(buf[0]))
}
for( i = 0i <Ni++ )
{
cvReleaseImage( &buf[i] )
buf[i] = cvCreateImage( size, IPL_DEPTH_8U, 1 )
cvZero( buf[i] )
}
cvReleaseImage( &mhi )
mhi = cvCreateImage( size, IPL_DEPTH_32F, 1 )
cvZero( mhi )
// clear MHI at the beginning
}
// end of if(mhi)
cvCvtColor( img, buf[last], CV_BGR2GRAY )
// convert frame to grayscale
idx1 = last
idx2 = (last + 1) % N
// index of (last - (N-1))th frame
last = idx2
// 做帧差
silh = buf[idx2]
cvAbsDiff( buf[idx1], buf[idx2], silh )
// get difference between frames
// 对差图像做二值化
cvThreshold( silh, silh, 30, 255, CV_THRESH_BINARY )
// and threshold it
cvUpdateMotionHistory( silh, mhi, timestamp, MHI_DURATION )
// update MHI
cvCvtScale( mhi, dst, 255./MHI_DURATION,
(MHI_DURATION - timestamp)*255./MHI_DURATION )
cvCvtScale( mhi, dst, 255./MHI_DURATION, 0 )
// 中值滤波,消除小的噪声
cvSmooth( dst, dst, CV_MEDIAN, 3, 0, 0, 0 )
// 向下采样,去掉噪声
cvPyrDown( dst, pyr, 7 )
cvDilate( pyr, pyr, 0, 1 )
// 做膨胀 *** 作,消除目标的不连续空洞
cvPyrUp( pyr, dst, 7 )
//
// 下面的程序段用来找到轮廓
//
// Create dynamic structure and sequence.
stor = cvCreateMemStorage(0)
cont = cvCreateSeq(CV_SEQ_ELTYPE_POINT, sizeof(CvSeq), sizeof(CvPoint) , stor)
// 找到所有轮廓
cvFindContours( dst, stor, &cont, sizeof(CvContour), CV_RETR_LIST, CV_CHAIN_APPROX_SIMPLE, cvPoint(0,0))
// 直接使用CONTOUR中的矩形来画轮廓
for(contcont = cont->h_next)
{
CvRect r = ((CvContour*)cont)->rect
if(r.height * r.width >CONTOUR_MAX_AERA) // 面积小的方形抛弃掉
{
cvRectangle( img, cvPoint(r.x,r.y),
cvPoint(r.x + r.width, r.y + r.height),
CV_RGB(255,0,0), 1, CV_AA,0)
}
} // free memory
cvReleaseMemStorage(&stor)
cvReleaseImage( &pyr )
}
int main(int argc, char** argv)
{
IplImage* motion = 0
CvCapture* capture = 0//视频获取结构
if( argc == 1 || (argc == 2 &&strlen(argv[1]) == 1 &&isdigit(argv[1][0])))
//原型:extern int isdigit(char c) //用法:#include <ctype.h> 功能:判断字符c是否为数字说明:当c为数字0-9时,返回非零值,否则返回零。
capture = cvCaptureFromCAM( argc == 2 ? argv[1][0] - '0' : 1 )
else if( argc == 2 )
capture = cvCaptureFromAVI( argv[1] )
if( capture )
{
cvNamedWindow( "Motion", 1 )
for()
{
IplImage* image
if( !cvGrabFrame( capture )) //从摄像头或者视频文件中抓取帧
break
image = cvRetrieveFrame( capture )
//取回由函数cvGrabFrame抓取的图像,返回由函数cvGrabFrame 抓取的图像的指针
if( image )
{
if( !motion )
{
motion = cvCreateImage( cvSize(image->width,image->height), 8, 1 )
cvZero( motion )
motion->origin = image->origin
///* 0 - 顶—左结构, 1 - 底—左结构 (Windows bitmaps 风格) */
}
}
update_mhi( image, motion, 60 )
cvShowImage( "Motion", image )
if( cvWaitKey(10) >= 0 )
break
}
cvReleaseCapture( &capture )
cvDestroyWindow( "Motion" )
}
return 0
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)