📄 action.cpp
字号:
// Action.cpp: implementation of the CAction class.
//
//////////////////////////////////////////////////////////////////////
#include "Action.h"
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CAction::CAction(int w,int h)
{
matForEvenRows=NULL;
m_imgCur=NULL;
m_imgPrev=NULL;
matForEvenRows=cvCreateMat(w,h/2,CV_8UC1);
cvNamedWindow("Original");
}
CAction::~CAction()
{
if(m_imgCur)
{
cvReleaseImage(&m_imgCur);
}
if(m_imgPrev)
{
cvReleaseImage(&m_imgPrev);
}
if(matForEvenRows)
{
cvReleaseMat(&matForEvenRows);
}
cvDestroyWindow("Original");
}
IplImage* CAction::Action(IplImage *frameRaw)
{
// return frameRaw;
static int w=frameRaw->width;
static int h=frameRaw->height;
static IplImage * frameCur=NULL;
static IplImage * frameOut=NULL;
// CString str;
// str.Format("w=%d h=%d",w,h);
// AfxMessageBox(str);
if(frameCur==NULL)
{
frameCur=cvCreateImage(cvSize(w,h/2),IPL_DEPTH_8U,3);
frameOut=cvCreateImage(cvSize(w,h/2),IPL_DEPTH_8U,3);
}
else if(frameRaw==NULL)
{
cvReleaseImage(&frameCur);
cvReleaseImage(&frameOut);
return NULL;;
}
IplImage * framePrev=cvCloneImage(frameCur);
GetEvenRows(frameRaw,&frameCur);
//////////////////////////////////////////////////////////
doAction(frameCur,framePrev,frameOut);
cvShowImage("Original",frameRaw);
//////////////////////////////////////////////////////////
cvReleaseImage(&framePrev);
return frameOut;
}
void CAction::GetEvenRows(IplImage * src, IplImage** dst)
{
int w=src->width;
int h=src->height;
cvGetRows( src, matForEvenRows, 0 , h, 2 );
int x,y;
for ( x=0; x<matForEvenRows->width; x++)
{
for ( y=0; y<matForEvenRows->height; y++)
{
CvScalar val = cvGet2D( matForEvenRows, y, x);
cvSet2D(*dst,y,x,cvScalarAll(val.val[0]));
}
/* for ( ; y<(*dst)->height; y++)
{
cvSet2D(*dst,y,x,cvScalarAll(255));
}
*/
}
//////////////////////////////////////////////////
}
void CAction::doAction(IplImage *cur, IplImage *prev, IplImage *res)
{
// int h=ComputeVelocity(cur,prev);
cvSub(cur,prev,res);
// h=5;
//SubByVelocityShift(cur,prev,res,h,5);
// cvAbs(res,res);
// cvErode(res,res);
// cvDilate(res,res);
//cvAdaptiveThreshold(res,res,255,CV_ADAPTIVE_THRESH_MEAN_C,CV_THRESH_BINARY_INV,7);
// cvThreshold(res,res,60,255,CV_THRESH_TRUNC);
// cvNormalize(res,res,0,255,CV_MINMAX);
// cvNormalize(res,res,10000,0,CV_L1);
// cvSubRS(res,cvScalarAll(255),res);
// cvThreshold(res,res,220,255,CV_THRESH_BINARY);
// cvInvert(res,res);
// cvSubRS(res,cvScalarAll(255),res);
// cvThreshold(res,res,60,255,CV_THRESH_BINARY);
/*
cvErode(res,res);
cvErode(res,res);
cvDilate(res,res);
cvDilate(res,res);
// SubByVelocityShift(cur,prev,res,0);
CString str;
str.Format("%d", h);
CvFont font;
cvInitFont(&font, CV_FONT_HERSHEY_PLAIN,1,1);
cvPutText(prev,str,cvPoint(4,15), &font, cvScalarAll(255));
// cvShowImage("Filtered",res);
*/
}
int CAction::ComputeVelocity(IplImage *cur, IplImage *prev)
{
CvMat tmpCur,tmpPrev,tmpRes;
int h=10;
int minh=10;
double minAvg=100000000;
cvGetSubRect(prev, &tmpPrev, cvRect(prev->width/2-70,20,140,30));
IplImage *tt=cvCloneImage(prev);
cvGetSubRect(tt, &tmpRes, cvRect(prev->width/2-70,20,140,30));
for(;h>=0;h--)
{
cvGetSubRect(cur, &tmpCur, cvRect(cur->width/2-70,20+h,140,30));
cvSub(&tmpCur,&tmpPrev,&tmpRes);
cvAbs(&tmpRes,&tmpRes);
double avg=cvAvg(&tmpRes).val[0];
if(avg<minAvg)
{
minAvg=avg;
minh=h;
}
}
cvReleaseImage(&tt);
return minh;
}
void CAction::SubByVelocityShift(IplImage *cur, IplImage *prev, IplImage *res, int step, int thres)
{
// CvMat tmpCur,tmpPrev,tmpRes;
// cvGetSubRect(cur, &tmpCur, cvRect(0,h,cur->width,cur->height-h));
int w=cur->width;
int h=cur->height;
int x,y;
CvScalar val2=cvScalarAll(0);
for ( x=0; x<w; x++)
{
for ( y=0; y<h; y++)
{
CvScalar val1 = cvGet2D( cur, y, x);
if(y+step>=0 && y+step<h)
val2= cvGet2D( prev, y+step, x);
else
val2=cvScalarAll(0);
// double d=(fabs(val1.val[0]-val2.val[0])+fabs(val1.val[1]-val2.val[1])+fabs(val1.val[2]-val2.val[2]))/3.0;
// if(d<thres)
// val1=cvScalarAll(0);
// val1=cvScalarAll(min(255, val1.val[0]*floor(log(d+1)/2.5)));
val1=cvScalar(fabs(val1.val[0]-val2.val[0]),fabs(val1.val[1]-val2.val[1]),fabs(val1.val[2]-val2.val[2]));
cvSet2D(res,y,x,val1);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -