📄 objectextractiondoc.cpp
字号:
CString filepath=GetFilePath(str);
if(filepath=="")
{
writer=cvCreateVideoWriter(str,CV_FOURCC('X','V','I','D'),
cvGetCaptureProperty(pCapture,CV_CAP_PROP_FPS),cvSize(width,height),1);
}
else
{
writer=cvCreateVideoWriter(filepath,CV_FOURCC('X','V','I','D'),
cvGetCaptureProperty(pCapture,CV_CAP_PROP_FPS),cvSize(width,height),1);
}
// ofstream output("w.txt",ios::ate);
cvSetCaptureProperty(pCapture,CV_CAP_PROP_POS_AVI_RATIO,0);
int frameNUM=0;
cvNamedWindow("result",CV_WINDOW_AUTOSIZE);
//cvNamedWindow("pImage", CV_WINDOW_AUTOSIZE);
cvMoveWindow("result",250+width, 200);
cvMoveWindow("pImage", 120, 200);
while(pImage = cvQueryFrame(pCapture))
{
frameNUM=(int)cvGetCaptureProperty(pCapture,CV_CAP_PROP_POS_FRAMES);
for(int x=0;x<width;x++)
for(int y=0;y<height;y++)
{
value[0] = (float)(iplGetPixelRed(pImage,x,y));
value[1] = (float)(iplGetPixelGreen(pImage,x,y));
value[2] = (float)(iplGetPixelBlue(pImage,x,y));
if(bgModel[y*width+x].Process_No_Update(value))
{
iplSetPixelColor(pResultImage,x,y,0xffffffff);
}
else
{
iplSetPixelColor(pResultImage,x,y,0xff000000);
}
bgModel[y*width+x].Norm();
float redV=0;
float greenV=0;
float blueV=0;
for(int i=0;i<K;i++)
{
redV+=bgModel[y*width+x].m_ptr[i]->m_mean[0]*bgModel[y*width+x].m_ptr[i]->m_weight;
greenV+=bgModel[y*width+x].m_ptr[i]->m_mean[1]*bgModel[y*width+x].m_ptr[i]->m_weight;
blueV+=bgModel[y*width+x].m_ptr[i]->m_mean[2]*bgModel[y*width+x].m_ptr[i]->m_weight;
}
int pixel=getRGB((int)redV,(int)greenV,(int)blueV);
iplSetPixelColor(back,x,y,pixel);
}
SmoothFilter(pResultImage);
shadowElimination(pImage,back,pResultImage);
IplImage* pResultImage1=cvCreateImage(cvSize(width,height),IPL_DEPTH_8U,3);
pResultImage1->origin=1;
cvDilate(pResultImage,pResultImage1,NULL,1);
cvErode(pResultImage1,pResultImage,NULL,1);
cvShowImage("pImage",pImage);
cvShowImage("result",pResultImage);
cvWriteFrame(writer,pResultImage);//写入一帧
cvReleaseImage(&pResultImage1);
if(cvWaitKey(1) >= 0 || frameNUM>(int)FrameCount)
{
break;
}
}
delete [] bgModel;
cvReleaseVideoWriter(&writer);
cvReleaseImage(&back);
back=NULL;
cvReleaseImage(&pResultImage);
pResultImage=NULL;
// if(pCapture!=NULL)
// {
// cvReleaseCapture(&pCapture);
// }
//
cvDestroyWindow("pImage");
cvDestroyWindow("result");
}
void CObjectExtractionDoc::OnAlgSgmmdcnoupdate()
{
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;//重建的背景图像
IplImage* pDcImage=NULL;
IplImage* pDcResult=NULL;
int dcwidth;
int dcheight;
double FrameCount;//=cvGetCaptureProperty(pCapture,CV_CAP_PROP_FRAME_COUNT);
if (pImage = cvQueryFrame(pCapture))
{
//meanFilter(pImage);
FrameCount=cvGetCaptureProperty(pCapture,CV_CAP_PROP_FRAME_COUNT);
width = pImage->width;
height = pImage->height;
dcwidth=width/RATIO;
dcheight=height/RATIO;
pResultImage=cvCreateImage(cvSize(width,height),IPL_DEPTH_8U,3);
pResultImage->origin=1;
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;
cvNamedWindow("pImage", CV_WINDOW_AUTOSIZE);
cvMoveWindow("pImage", 120, 200);
//cvMoveWindow("result",250+width, 200);
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(5, 3, value);
}
//模型初始化
k = 0;
while(pImage = cvQueryFrame(pCapture))//30->15
{
//meanFilter(pImage);
cvShowImage("pImage",pImage);
cvWaitKey(1);
getDCImage(pImage,pDcImage);
//pImage = cvQueryFrame(pCapture);
ASSERT(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(k>=FrameCount)
{
break;
}
}
//频率归一化为权重
for(i=0; i<dcwidth; i++)
for(j=0; j<dcheight; j++)
{
bgModel[j * dcwidth+i].Norm();
}
}
CString str(m_path);
int pos=str.Find(".");
str.Insert(pos,"_bin");
CString filepath=GetFilePath(str);
if(filepath=="")
{
writer=cvCreateVideoWriter(str,CV_FOURCC('X','V','I','D'),
cvGetCaptureProperty(pCapture,CV_CAP_PROP_FPS),cvSize(width,height),1);
}
else
{
writer=cvCreateVideoWriter(filepath,CV_FOURCC('X','V','I','D'),
cvGetCaptureProperty(pCapture,CV_CAP_PROP_FPS),cvSize(width,height),1);
}
// ofstream output("w.txt",ios::ate);
cvSetCaptureProperty(pCapture,CV_CAP_PROP_POS_AVI_RATIO,0);
int frameNUM=0;
cvNamedWindow("result",CV_WINDOW_AUTOSIZE);
//cvNamedWindow("pImage", CV_WINDOW_AUTOSIZE);
cvMoveWindow("pImage", 120, 200);
cvMoveWindow("result",250+width, 200);
while(pImage = cvQueryFrame(pCapture))
{
frameNUM=(int)cvGetCaptureProperty(pCapture,CV_CAP_PROP_POS_FRAMES);
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_No_Update(value))
{
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<10;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);
cvResize(pDcResult,pResultImage,CV_INTER_CUBIC);
cvShowImage("pImage",pImage);
cvShowImage("result",pResultImage);
cvWriteFrame(writer,pResultImage);//写入一帧
cvReleaseImage(&pResultImage1);
if(cvWaitKey(1) >= 0 || frameNUM>=(int)FrameCount)
{
break;
}
}
delete [] bgModel;
cvReleaseVideoWriter(&writer);
cvReleaseImage(&back);
back=NULL;
cvReleaseImage(&pResultImage);
pResultImage=NULL;
cvReleaseImage(&pDcResult);
pDcResult=NULL;
cvReleaseImage(&pDcImage);
pDcImage=NULL;
// if(pCapture!=NULL)
// {
// cvReleaseCapture(&pCapture);
// }
cvDestroyWindow("pImage");
cvDestroyWindow("result");
}
void CObjectExtractionDoc::OnFileSave()
{
CFileDialog dlg(FALSE,"avi",0,OFN_OVERWRITEPROMPT,"avi file(*.avi)",NULL);
if (dlg.DoModal()==IDOK)
{
CString lpszPathName=dlg.GetPathName();
//CString right4=lpszPathName.Right(4);
if(lpszPathName.IsEmpty())
{
AfxMessageBox("路径错误,请检查文件路径");
return;
}
lpszPathName.MakeLower();
//CString avi(".avi");
if(lpszPathName.Find(".avi")<0)
{
lpszPathName+=".avi";
}
char* path=(LPTSTR)(LPCTSTR)(lpszPathName);
if(pCapture==NULL)
{
AfxMessageBox("请打开文件或摄像头");
return;
}
CvVideoWriter* writer;
pImage = cvQueryFrame(pCapture);
double frame_ratio=0;
if(!m_isCam)
{
frame_ratio=cvGetCaptureProperty(pCapture,CV_CAP_PROP_FPS);
}
else
{
frame_ratio=10;
}
writer=cvCreateVideoWriter(path,CV_FOURCC('X','V','I','D'),
frame_ratio,cvSize(pImage->width,pImage->height),1);
if(writer==NULL)
{
AfxMessageBox("in OnFileSave(),the pCapture is null");
return;
}
cvNamedWindow("image",CV_WINDOW_AUTOSIZE);
if(!m_isCam)
{
cvSetCaptureProperty(pCapture,CV_CAP_PROP_POS_FRAMES,0);
int count=(int)cvGetCaptureProperty(pCapture,CV_CAP_PROP_FRAME_COUNT);
cvCreateTrackbar("image","image",0,count,NULL);
int pos=(int)cvGetCaptureProperty(pCapture,CV_CAP_PROP_POS_FRAMES)-1;
while(pImage = cvQueryFrame(pCapture))
{
pos=(int)cvGetCaptureProperty(pCapture,CV_CAP_PROP_POS_FRAMES)-1;
cvShowImage("image",pImage);
cvWriteFrame(writer,pImage);
cvSetTrackbarPos("image","image",pos);
if(cvWaitKey(1)>=0 || pos>=count)
{
break;
}
}
}
else
{
while(pImage = cvQueryFrame(pCapture))
{
cvShowImage("image",pImage);
cvWriteFrame(writer,pImage);
if(cvWaitKey(1)>=0)
{
break;
}
}
}
cvReleaseVideoWriter(&writer);
cvDestroyWindow("image");
//ASSERT(pCapture);
// if(cvGetWindowHandle("pImage")==NULL)
// {
// cvNamedWindow("pImage",CV_WINDOW_AUTOSIZE);
// }
}
}
void CallBackTrackbar2(int pos)
{
pos=cvGetTrackbarPos("image","image");
cvSetCaptureProperty(pCapture1,CV_CAP_PROP_POS_FRAMES,(double)pos);
IplImage* image=cvQueryFrame(pCapture1);
cvShowImage("image",image);
}
void CObjectExtractionDoc::OnFileSaveAs()
{
pCapture1=pCapture;
CFileDialog dlg(FALSE,"avi",0,OFN_OVERWRITEPROMPT,"avi file(*.avi)",NULL);
if (dlg.DoModal()==IDOK)
{
CString lpszPathName=dlg.GetPathName();
//CString right4=lpszPathName.Right(4);
if(lpszPathName.IsEmpty())
{
AfxMessageBox("路径错误,请检查文件路径");
return;
}
lpszPathName.MakeLower();
//CString avi(".avi");
if(lpszPathName.Find(".avi")<0)
{
lpszPathName+=".avi";
}
char* path=(LPTSTR)(LPCTSTR)(lpszPathName);
if(pCapture==NULL)
{
AfxMessageBox("请打开文件或摄像头");
return;
}
CvVideoWriter* writer;
pImage = cvQueryFrame(pCapture);
double frame_ratio=0;
if(!m_isCam)
{
frame_ratio=cvGetCaptureProperty(pCapture,CV_CAP_PROP_FPS);
}
else
{
frame_ratio=10;
}
writer=cvCreateVideoWriter(path,CV_FOURCC('X','V','I','D'),
frame_ratio,cvSize(pImage->width,pImage->height),1);
if(writer==NULL)
{
AfxMessageBox("in OnFileSave(),the pCapture is null");
return;
}
cvNamedWindow("image",CV_WINDOW_AUTOSIZE);
int y = ::GetSystemMetrics(SM_CYSCREEN) ;
int x = ::GetSystemMetrics(SM_CXSCREEN) ;
cvMoveWindow("image",x/2-x/4,200);
// cs.cy = 100;
// cs.cx = x/2;
// cs.y = 100;
// cs.x = x/2-x/4;
if(!m_isCam)
{
int count=(int)cvGetCaptureProperty(pCapture,CV_CAP_PROP_FRAME_COUNT);
cvSetCaptureProperty(pCapture,CV_CAP_PROP_POS_FRAMES,0);
cvCreateTrackbar("image","image",0,count,CallBackTrackbar2);
CDlgOnSaveAs dlg(pCapture1, 0,writer,"image", "image",NULL);
dlg.DoModal();
}
else
{
while(pImage = cvQueryFrame(pCapture))
{
cvShowImage("image",pImage);
cvWriteFrame(writer,pImage);
if(cvWaitKey(1)>=0)
{
break;
}
}
}
cvReleaseVideoWriter(&writer);
cvDestroyWindow("image");
}
}
void CObjectExtractionDoc::OnProProcess()
{
if(m_isCam)
{
AfxMessageBox("此功能只对视频文件有效,请先打开一个视频文件");
return;
}
if(pCapture==NULL)
{
AfxMessageBox("请打开文件");
return;
}
CString lpszPathName=m_path;
if(lpszPathName.IsEmpty())
{
AfxMessageBox("错误,请检查文件路径");
return;
}
lpszPathName.MakeLower();
//CString avi(".avi");
int pos;
if((pos=lpszPathName.Find(".avi"))<0)
{
lpszPathName+="_result.avi";
}
else
{
lpszPathName.Insert(pos,"_result");
}
// CvVideoWriter* writer;
//pImage = cvQueryFrame(pCapture);
//double frame_ratio=cvGetCaptureProperty(pCapture,CV_CAP_PROP_FPS);
// writer=cvCreateVideoWriter(path,CV_FOURCC('X','V','I','D'),
// frame_ratio,cvSize(pImage->width,pImage->height),1);
// if(writer==NULL)
// {
// AfxMessageBox("in OnFileSave(),the pCapture is null");
// return;
// }
cvNamedWindow("image",CV_WINDOW_AUTOSIZE);
int y = ::GetSystemMetrics(SM_CYSCREEN) ;
int x = ::GetSystemMetrics(SM_CXSCREEN) ;
cvMoveWindow("image",x/2-9*x/20,200);
// cs.cy = 100;
// cs.cx = x/2;
// cs.y = 100;
// cs.x = x/2-x/4;
pCapture1=pCapture;
if(!m_isCam)
{
int count=(int)cvGetCaptureProperty(pCapture,CV_CAP_PROP_FRAME_COUNT);
cvSetCaptureProperty(pCapture,CV_CAP_PROP_POS_FRAMES,0);
cvCreateTrackbar("image","image",0,count,CallBackTrackbar2);
CDlgImageProcess dlg(pCapture1, 0,lpszPathName,"image", "image",NULL);
dlg.DoModal();
}
//cvReleaseVideoWriter(&writer);
cvDestroyWindow("image");
}
void CObjectExtractionDoc::OnAlgLgmmgraphcut()
{
CCameraVideo ccv(pCapture,m_path);
ccv.process();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -