📄 objectextractiondoc.cpp
字号:
}
else
{
cvWriter=cvCreateVideoWriter(str,CV_FOURCC('X','V','I','D'),
cvGetCaptureProperty(pCapture,CV_CAP_PROP_FPS),cvSize(width,height),1);
}
}
else
{
if(m_isCam)
{
cvWriter=cvCreateVideoWriter(filepath,CV_FOURCC('X','V','I','D'),
10,cvSize(width,height),1);
}
else
{
cvWriter=cvCreateVideoWriter(filepath,CV_FOURCC('X','V','I','D'),
cvGetCaptureProperty(pCapture,CV_CAP_PROP_FPS),cvSize(width,height),1);
}
}
ASSERT(cvWriter!=NULL);
cvWriteFrame(cvWriter,pResultImage);
while(pImage = cvQueryFrame(pCapture))
{
if(!m_isCam)
{
FrameNum=(int)cvGetCaptureProperty(pCapture,CV_CAP_PROP_POS_FRAMES);
}
getDCImage(pImage,pResultImage);
cvShowImage("pImage",pImage);
cvShowImage("result",pResultImage);
cvMoveWindow("result",130+pImage->width, 200);
cvMoveWindow("pImage", 120, 200);
cvWriteFrame(cvWriter,pResultImage);
if(cvWaitKey(1) >= 0 || ((!m_isCam) && FrameNum>=FramesCount))
{
break;
//break;
}
}
cvReleaseVideoWriter(&cvWriter);
cvReleaseImage(&pImage);
pImage=NULL;
cvReleaseImage(&pResultImage);
if(pCapture!=NULL)
{
cvReleaseCapture(&pCapture);
}
pResultImage=NULL;
cvDestroyWindow("pImage");
cvDestroyWindow("result");
}
void CObjectExtractionDoc::OnSegScenechange()
{
#define winSize ((int)(FrameRatio/4))
if(pCapture!=NULL)
{
cvReleaseCapture(&pCapture);
pCapture=NULL;
}
//int pos=m_path.Find(".");
//m_path.Insert(pos,"_DC");
if(m_isCam)
{
AfxMessageBox("此功能只能应用于视频文件,请打开一个视频文件");
return;
}
if((pCapture=cvCreateFileCapture(m_path))==NULL)
{
AfxMessageBox("in OnFileOpenfile(),the pCapture is null");
}
ASSERT(pCapture);
if(pImage!=NULL)
{
cvReleaseImage(&pImage);
pImage=NULL;
}
if(pResultImage!=NULL)
{
cvReleaseImage(&pResultImage); //这里,pResultImage是作为pImage的前一帧图像
pResultImage=NULL;
}
pImage = cvQueryFrame(pCapture);
width=pImage->width;
height=pImage->height;
pResultImage=cvCreateImage(cvGetSize(pImage),IPL_DEPTH_8U,3);
memcpy(pResultImage->imageData,pImage->imageData,pImage->widthStep*pImage->height);
double FramesNum=cvGetCaptureProperty(pCapture,CV_CAP_PROP_FRAME_COUNT);
double FrameRatio=cvGetCaptureProperty(pCapture,CV_CAP_PROP_FPS);
int* frameDiff=new int[(int)FramesNum-1];
bool* isChange=new bool[(int)FramesNum-1];//true is change,false is not change.
int num=0;
while((pImage=cvQueryFrame(pCapture)))
{
frameDiff[num]=getHistogramDifference(pResultImage,pImage);
isChange[num]=false;//the initial data
num++;
memcpy(pResultImage->imageData,pImage->imageData,pImage->widthStep*pImage->height);
if(cvWaitKey(1) >= 0 || num>=(int)(FramesNum))
{
break;
}
}
int i;
for(i=winSize-1; i<FramesNum-1-winSize+1;)
{
int max1=0;//窗口内第一与第二大值
int max2=0;
int x1=0,x2=0;//第一与第二大值的位置
for(int j=i-winSize+1;j<i+winSize-1;j++)
{
if(max1<=frameDiff[j])
{
max2=max1;
x2=x1;
max1=frameDiff[j];
x1=j;
}
else
{
if(max2<=frameDiff[j])
{
max2=frameDiff[j];
x2=j;
}
}
}
if(max1>2.5*max2)
{
isChange[x1]=true;
i=x1+winSize;
}
else
{
i+=winSize;
}
}
//处理余下的几帧,个数小于2倍的帧率。
int max1=0;//窗口内第一与第二大值
int max2=0;
int x1=0,x2=0;//第一与第二大值的位置
for(int j=i-winSize+1;j<FramesNum-1;j++)
{
if(max1<=frameDiff[j])
{
max2=max1;
x2=x1;
max1=frameDiff[j];
x1=j;
}
else
{
if(max2<=frameDiff[j])
{
max2=frameDiff[j];
x2=j;
}
}
}
if(max1>=2.5*max2)
{
isChange[x1]=true;
}
/*ofstream output("F:\\C++\\ObjectExtraction\\order.txt",ios::ate);
for(i=0;i<num;i++)
{
output<<frameDiff[i]<<",";
}
output<<endl;*/
int videonum=0;
CString str("");
if((str=GetFolderPath())==CString(""))
{
str=m_path;
}
else
{
str+=(CString("\\")+m_fileName);
}
int pos=str.Find(".");
str.Insert(pos,"_seg");
pos=str.Find(".");
CString cstr;
CString constSTR(str);
cstr.Format("%d",videonum);
str.Insert(pos,cstr);
CvVideoWriter* writer=cvCreateVideoWriter(str,CV_FOURCC('X','V','I','D'),
cvGetCaptureProperty(pCapture,CV_CAP_PROP_FPS),cvSize(width,height),1);
cvSetCaptureProperty(pCapture,CV_CAP_PROP_POS_AVI_RATIO,0);
for(i=0;i<FramesNum-1;i++)
{
if(!isChange[i])
{
if((pImage=cvQueryFrame(pCapture))==NULL)
{
break;
}
cvWriteFrame(writer,pImage);
}
else
{
if((pImage=cvQueryFrame(pCapture))==NULL)
{
break;
}
cvWriteFrame(writer,pImage);
cvReleaseVideoWriter(&writer);
videonum++;
cstr.Format("%d",videonum);
str=constSTR;
str.Insert(pos,cstr);
writer=cvCreateVideoWriter(str,CV_FOURCC('X','V','I','D'),
cvGetCaptureProperty(pCapture,CV_CAP_PROP_FPS),cvSize(width,height),1);
}
}
//if(pCapture!=NULL)
//{
//}
cvReleaseVideoWriter(&writer);
cvReleaseImage(&pResultImage);
pResultImage=NULL;
cvReleaseCapture(&pCapture);
pCapture=NULL;
}
void CObjectExtractionDoc::OnAlgSgmmdcimage()
{
ASSERT(pCapture!=NULL);
double FrameCount;
int FrameNum;
if(!m_isCam)
{
cvSetCaptureProperty(pCapture,CV_CAP_PROP_POS_AVI_RATIO,0);
FrameCount=cvGetCaptureProperty(pCapture,CV_CAP_PROP_FRAME_COUNT);
FrameNum=(int)cvGetCaptureProperty(pCapture,CV_CAP_PROP_POS_FRAMES);
}
cvNamedWindow("result",CV_WINDOW_AUTOSIZE);
cvNamedWindow("pImage", CV_WINDOW_AUTOSIZE);
//cvMoveWindow("pResultImage",130+width, 200);
int i, j, k, width, height;
float value[3];
CvVideoWriter* cvWriter;
sGMM *bgModel;
IplImage* back=NULL;//重建的背景图像
IplImage* pDcImage;
IplImage* pDcResult;
int dcwidth;
int dcheight;
if (pImage = cvQueryFrame(pCapture))
{
if(!m_isCam)
{
FrameNum=(int)cvGetCaptureProperty(pCapture,CV_CAP_PROP_POS_FRAMES);
}
width = pImage->width;
height = pImage->height;
dcwidth=width/RATIO;
dcheight=height/RATIO;
pResultImage=cvCreateImage(cvSize(width,height),IPL_DEPTH_8U,3);
pResultImage->origin=1;
//DCimage
pDcImage=cvCreateImage(cvSize(dcwidth,dcheight),IPL_DEPTH_8U,3);
pDcImage->origin=1;
pDcResult=cvCreateImage(cvSize(dcwidth,dcheight),IPL_DEPTH_8U,3);
pDcResult->origin=1;
//重建的背景图像
back=cvCreateImage(cvSize(dcwidth,dcheight),IPL_DEPTH_8U,3);
back->origin=1;
CString str(m_path);
if(m_isCam)
{
str="c:\\cam_tmp_foreground.avi";
}
int pos=str.Find(".");
str.Insert(pos,"_bin");
CString filepath=GetFilePath(str);
if(filepath=="")
{
if(m_isCam)
{
cvWriter=cvCreateVideoWriter(str,CV_FOURCC('X','V','I','D'),
10,cvSize(width,height),1);
}
else
{
cvWriter=cvCreateVideoWriter(str,CV_FOURCC('X','V','I','D'),
cvGetCaptureProperty(pCapture,CV_CAP_PROP_FPS),cvSize(width,height),1);
}
}
else
{
if(m_isCam)
{
cvWriter=cvCreateVideoWriter(filepath,CV_FOURCC('X','V','I','D'),
10,cvSize(width,height),1);
}
else
{
cvWriter=cvCreateVideoWriter(filepath,CV_FOURCC('X','V','I','D'),
cvGetCaptureProperty(pCapture,CV_CAP_PROP_FPS),cvSize(width,height),1);
}
}
getDCImage(pImage,pDcImage);
bgModel = new sGMM[dcwidth * dcheight];
ASSERT(bgModel != NULL);
for(i=0; i<dcwidth; i++)
for(j=0; j<dcheight; j++)
{
value[0] = (float)(iplGetPixelRed(pDcImage,i,j));
value[1] = (float)(iplGetPixelGreen(pDcImage,i,j));
value[2] = (float)(iplGetPixelBlue(pDcImage,i,j));
bgModel[j * dcwidth + i].Init(K, 3, value);
}
//模型初始化
k = 0;
while(k < 15)//30->15
{
//meanFilter(pImage);
pImage = cvQueryFrame(pCapture);
if(!m_isCam)
{
FrameNum=(int)cvGetCaptureProperty(pCapture,CV_CAP_PROP_POS_FRAMES);
}
getDCImage(pImage,pDcImage);
ASSERT(pImage!=NULL && pDcImage!=NULL);
for(i=0; i<dcwidth; i++)
for(j=0; j<dcheight; j++)
{
value[0] = (float)(iplGetPixelRed(pDcImage,i,j));
value[1] = (float)(iplGetPixelGreen(pDcImage,i,j));
value[2] = (float)(iplGetPixelBlue(pDcImage,i,j));
bgModel[j * dcwidth + i].Init_Process(value);
}
k++;
if(((!m_isCam) && k>FrameCount) || ((!m_isCam) && FrameNum>=FrameCount))
{
break;
}
}
//频率归一化为权重
for(i=0; i<dcwidth; i++)
for(j=0; j<dcheight; j++)
{
bgModel[j * dcwidth+i].Norm();
}
}
// ofstream output("w.txt",ios::ate);
int frameNUM=0;
//double FrameCount=cvGetCaptureProperty(pCapture,CV_CAP_PROP_FRAME_COUNT);
if(!m_isCam)
{
cvSetCaptureProperty(pCapture,CV_CAP_PROP_POS_AVI_RATIO,0);
}
cvMoveWindow("result",250+width, 200);
cvMoveWindow("pImage", 120, 200);
while(pImage = cvQueryFrame(pCapture))
{
// cvSetCaptureProperty(pCapture,CV_CAP_PROP_POS_FRAMES,
// cvGetCaptureProperty(pCapture,CV_CAP_PROP_POS_FRAMES)+5);
//meanFilter(pImage);
frameNUM++;
getDCImage(pImage,pDcImage);
for(int x=0;x<dcwidth;x++)
for(int y=0;y<dcheight;y++)
{
value[0] = (float)(iplGetPixelRed(pDcImage,x,y));
value[1] = (float)(iplGetPixelGreen(pDcImage,x,y));
value[2] = (float)(iplGetPixelBlue(pDcImage,x,y));
if(bgModel[y*dcwidth+x].Process(value,frameNUM))
{
iplSetPixelColor(pDcResult,x,y,0xffffffff);
}
else
{
iplSetPixelColor(pDcResult,x,y,0xff000000);
}
bgModel[y*dcwidth+x].Norm();
float redV=0;
float greenV=0;
float blueV=0;
for(int i=0;i<K;i++)
{
redV+=bgModel[y*dcwidth+x].m_ptr[i]->m_mean[0]*bgModel[y*dcwidth+x].m_ptr[i]->m_weight;
greenV+=bgModel[y*dcwidth+x].m_ptr[i]->m_mean[1]*bgModel[y*dcwidth+x].m_ptr[i]->m_weight;
blueV+=bgModel[y*dcwidth+x].m_ptr[i]->m_mean[2]*bgModel[y*dcwidth+x].m_ptr[i]->m_weight;
}
int pixel=getRGB((int)redV,(int)greenV,(int)blueV);
iplSetPixelColor(back,x,y,pixel);
}
SmoothFilter(pDcResult);
shadowElimination(pDcImage,back,pDcResult);
IplImage* pResultImage1=cvCreateImage(cvSize(dcwidth,dcheight),IPL_DEPTH_8U,3);
pResultImage1->origin=1;
cvDilate(pDcResult,pResultImage1,NULL,1);
cvErode(pResultImage1,pDcResult,NULL,1);
cvShowImage("pImage",pImage);
cvResize(pDcResult,pResultImage,CV_INTER_CUBIC);
cvShowImage("result",pResultImage);
cvWriteFrame(cvWriter,pResultImage);//写入一帧
cvReleaseImage(&pResultImage1);
if(cvWaitKey(1) >= 0 || ((!m_isCam) && frameNUM>=(int)(FrameCount)))
{
cvReleaseCapture(&pCapture);
pCapture=NULL;
}
}
delete [] bgModel;
cvReleaseVideoWriter(&cvWriter);
cvReleaseImage(&back);
back=NULL;
cvReleaseImage(&pDcResult);
pDcResult=NULL;
cvReleaseImage(&pDcImage);
pDcImage=NULL;
cvReleaseImage(&pResultImage);
pResultImage=NULL;
// if(pCapture!=NULL)
// {
// cvReleaseCapture(&pCapture);
// }
cvDestroyWindow("pImage");
cvDestroyWindow("result");
}
void CObjectExtractionDoc::OnAlgSgmmnoupdate()
{
ASSERT(pCapture!=NULL);
cvSetCaptureProperty(pCapture,CV_CAP_PROP_POS_AVI_RATIO,0);
//高斯模型的个数
if(m_isCam)
{
AfxMessageBox("此功能只能对视频文件进行处理,请打开一个视频文件!");
return;
}
//cvMoveWindow("pResultImage",130+width, 200);
int i, j, k, width, height;
float value[3];
CvVideoWriter* writer;
sGMM *bgModel;
IplImage* back=NULL;//重建的背景图像
double FrameCount;//=cvGetCaptureProperty(pCapture,CV_CAP_PROP_FRAME_COUNT);
cvNamedWindow("pImage", CV_WINDOW_AUTOSIZE);
//cvMoveWindow("result",250+width, 200);
cvMoveWindow("pImage", 120, 200);
if (pImage = cvQueryFrame(pCapture))
{
//meanFilter(pImage);
FrameCount=cvGetCaptureProperty(pCapture,CV_CAP_PROP_FRAME_COUNT);
width = pImage->width;
height = pImage->height;
pResultImage=cvCreateImage(cvSize(width,height),IPL_DEPTH_8U,3);
pResultImage->origin=1;
//重建的背景图像
back=cvCreateImage(cvSize(width,height),IPL_DEPTH_8U,3);
back->origin=1;
// writer=cvCreateVideoWriter("f:\\tmp.avi",-1,
// cvGetCaptureProperty(pCapture,CV_CAP_PROP_FPS),cvSize(width,height),1);
bgModel = new sGMM[width * height];
ASSERT(bgModel != NULL);
for(i=0; i<width; i++)
for(j=0; j<height; j++)
{
value[0] = (float)(iplGetPixelRed(pImage,i,j));
value[1] = (float)(iplGetPixelGreen(pImage,i,j));
value[2] = (float)(iplGetPixelBlue(pImage,i,j));
bgModel[j * width + i].Init(10, 3, value);
}
//模型初始化
k = 0;
while(pImage = cvQueryFrame(pCapture))//30->15
{
//meanFilter(pImage);
//pImage = cvQueryFrame(pCapture);
ASSERT(pImage!=NULL);
cvShowImage("pImage",pImage);
cvWaitKey(1);
for(i=0; i<width; i++)
for(j=0; j<height; j++)
{
value[0] = (float)(iplGetPixelRed(pImage,i,j));
value[1] = (float)(iplGetPixelGreen(pImage,i,j));
value[2] = (float)(iplGetPixelBlue(pImage,i,j));
bgModel[j * width + i].Init_Process(value);
}
k++;
if(k>=FrameCount)
{
break;
}
}
//频率归一化为权重
for(i=0; i<width; i++)
for(j=0; j<height; j++)
{
bgModel[j * width+i].Norm();
}
}
CString str(m_path);
int pos=str.Find(".");
str.Insert(pos,"_bin");
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -