www.pudn.com > backmode824.rar > backmodelView.cpp
// backmodelView.cpp : implementation of the CBackmodelView class // #include "stdafx.h" #include "backmodel.h" #include "backmodelDoc.h" #include "backmodelView.h" #include "TsetXy.h" #include/*..........................*/ #include "highgui.h" #include "cv.h" // include core library interface #ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif /*........................................................*/ IplImage *image = 0, *hsv = 0, *hue = 0, *mask = 0, *backproject = 0, *histimg = 0; char* lpfimage=NULL; ///////////////////////////////////////////////////////////////////////////// // CBackmodelView IMPLEMENT_DYNCREATE(CBackmodelView, CView) BEGIN_MESSAGE_MAP(CBackmodelView, CView) //{{AFX_MSG_MAP(CBackmodelView) ON_COMMAND(ID_FILE_OPENOBJECT, OnFileOpenobject) ON_COMMAND(ID_FILE_OPENBACK, OnFileOpenback) ON_COMMAND(ID_IMAGE_DIFF, OnImageDiff) ON_COMMAND(ID_IMAGE_MEDIAN, OnImageMedian) ON_COMMAND(ID_IMAGE_DIFFCLUSTER, OnImageDiffcluster) ON_COMMAND(ID_IMAGE_MIXGAUSS, OnImageMixgauss) ON_COMMAND(ID_VIDEO_HISTOGRAM, OnVideoHistogram) ON_WM_TIMER() ON_COMMAND(ID_FILE_OPENVIDEO, OnFileOpenvideo) //}}AFX_MSG_MAP // Standard printing commands ON_COMMAND(ID_FILE_PRINT, CView::OnFilePrint) ON_COMMAND(ID_FILE_PRINT_DIRECT, CView::OnFilePrint) ON_COMMAND(ID_FILE_PRINT_PREVIEW, CView::OnFilePrintPreview) END_MESSAGE_MAP() ///////////////////////////////////////////////////////////////////////////// // CBackmodelView construction/destruction CBackmodelView::CBackmodelView() { // TODO: add construction code here m_bHaveBack=false; //打开背景frame m_bHaveObject=false; //打开目标frame m_bHaveSub=false; m_dShowScale=1.0;//初始化显示比例 m_iShowNumber=2;//初始化显示图像数量 m_iThreshold=20;//10; m_iShowImageMode=0; if(m_Memory.InitMem()) { m_ImageProcess.InitData(&m_Memory); m_ImageProcess.m_iThreshold=m_iThreshold; m_bInitSuccess=TRUE; } } CBackmodelView::~CBackmodelView() { } BOOL CBackmodelView::PreCreateWindow(CREATESTRUCT& cs) { // TODO: Modify the Window class or styles here by modifying // the CREATESTRUCT cs return CView::PreCreateWindow(cs); } ///////////////////////////////////////////////////////////////////////////// // CBackmodelView drawing void CBackmodelView::OnDraw(CDC* pDC) { CBackmodelDoc* pDoc = GetDocument(); ASSERT_VALID(pDoc); // TODO: add draw code for native data here CRect srcRect,destRect1,destRect2,destRect3; srcRect.top=5; srcRect.left=5; srcRect.right=m_ImageProcess.m_lpMapHeader->biWidth; srcRect.bottom=m_ImageProcess.m_lpMapHeader->biHeight; destRect1=srcRect; destRect1.right=(int)(destRect1.right*m_dShowScale); destRect1.bottom=(int)(destRect1.bottom*m_dShowScale); destRect2=destRect1; CString str; if(m_bHaveObject) { m_ImageProcess.BuildBitmap(m_ImageProcess.m_lpImage); m_ImageProcess.Display(pDC,destRect1,srcRect,SRCCOPY); str.Format(m_sFileName); pDC->TextOut(destRect2.left+25,destRect2.bottom+10,str); } destRect2=srcRect; //画目标图像 if(m_bHaveBack) //画背景 { destRect2.left=destRect1.right+5; destRect2.right=destRect2.left+destRect2.right; m_ImageProcess.BuildBitmap(m_ImageProcess.m_lpBackImage); // m_ImageProcess.BuildBitmap(m_ImageProcess.m_lpMask);//合成背景 m_ImageProcess.Display(pDC,destRect2,srcRect,SRCCOPY); str.Format("背景"); pDC->TextOut(destRect2.left+25,destRect2.bottom+10,str); } if(m_iShowImageMode==2) //画差分后的图像 { destRect2=srcRect; destRect2.top=destRect1.bottom+30; destRect2.bottom=destRect2.top+destRect2.bottom; m_ImageProcess.BuildBitmap(m_ImageProcess.m_lpDMap); m_ImageProcess.Display(pDC,destRect2,srcRect,SRCCOPY); str.Format("差分"); pDC->TextOut(destRect2.left+25,destRect2.bottom+10,str); } if(m_iShowImageMode==3) //画混合高斯分布后的图像 { destRect2=srcRect; destRect2.left=destRect1.right+5; destRect2.right=destRect2.left+destRect2.right; destRect2.top=destRect1.bottom+30; destRect2.bottom=destRect2.top+destRect2.bottom; m_ImageProcess.BuildBitmap(m_ImageProcess.m_lpMask);//draw.... m_ImageProcess.Display(pDC,destRect2,srcRect,SRCCOPY); str.Format("混合高斯"); pDC->TextOut(destRect2.left+25,destRect2.bottom+10,str); destRect2=srcRect; destRect2.left=destRect1.right+m_ImageProcess.m_lpMapHeader->biWidth+5+5; destRect2.right=destRect2.left+destRect2.right; destRect2.top=destRect1.bottom+30; destRect2.bottom=destRect2.top+destRect2.bottom; m_ImageProcess.BuildBitmap(m_ImageProcess.m_lpGrayMask); m_ImageProcess.Display(pDC,destRect2,srcRect,SRCCOPY); str.Format("connected"); pDC->TextOut(destRect2.left+25,destRect2.bottom+10,str); } if(m_iShowImageMode==4) //画背景平均后的图像 { destRect2=srcRect; destRect2.left=destRect1.right+m_ImageProcess.m_lpMapHeader->biWidth+5+5; destRect2.right=destRect2.left+destRect2.right; destRect2.top=destRect1.bottom+5; destRect2.bottom=destRect2.top+destRect2.bottom; m_ImageProcess.BuildBitmap(m_ImageProcess.m_lpDMap); m_ImageProcess.Display(pDC,destRect2,srcRect,SRCCOPY); str.Format("均值"); pDC->TextOut(destRect2.left+25,destRect2.bottom+10,str); } if(m_iShowImageMode==5) //画坐标系 { drawXY(pDC); drawCurve(pDC); } //Invalidate(FALSE); /* //画处理图像 if((m_bHaveBack)&&(m_bHaveObject)&&(m_iShowImageMode==2)) { destRect2=srcRect; destRect2.top=destRect1.bottom; destRect2.bottom=destRect2.top+destRect2.bottom; //why not????/ m_ImageProcess.BuildBitmap(m_ImageProcess.m_lpDMap); m_ImageProcess.Display(pDC,destRect2,srcRect,SRCCOPY); }*/ } ///////////////////////////////////////////////////////////////////////////// // CBackmodelView printing BOOL CBackmodelView::OnPreparePrinting(CPrintInfo* pInfo) { // default preparation return DoPreparePrinting(pInfo); } void CBackmodelView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/) { // TODO: add extra initialization before printing } void CBackmodelView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/) { // TODO: add cleanup after printing } ///////////////////////////////////////////////////////////////////////////// // CBackmodelView diagnostics #ifdef _DEBUG void CBackmodelView::AssertValid() const { CView::AssertValid(); } void CBackmodelView::Dump(CDumpContext& dc) const { CView::Dump(dc); } CBackmodelDoc* CBackmodelView::GetDocument() // non-debug version is inline { ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CBackmodelDoc))); return (CBackmodelDoc*)m_pDocument; } #endif //_DEBUG ///////////////////////////////////////////////////////////////////////////// // CBackmodelView message handlers void CBackmodelView::OnFileOpenobject() { // TODO: Add your command handler code here static char szFilter[] = "位图文件 (*.bmp)|*.bmp|所有文件 (*.*)|*.*||"; CFileDialog* opendlg=new CFileDialog(TRUE,NULL,NULL, OFN_FILEMUSTEXIST|OFN_HIDEREADONLY, szFilter,AfxGetMainWnd()); if(opendlg->DoModal()==IDOK) { CFile f; CFileException e; if( !f.Open( opendlg->GetPathName(), CFile::modeRead |CFile::typeBinary , &e ) ) { #ifdef _DEBUG afxDump << "File could not be opened " << e.m_cause << "\n"; #endif } m_ImageProcess.LoadImage(&f,m_ImageProcess.m_lpImage); m_bHaveObject=TRUE; CString ls,lsStr=opendlg->GetPathName(); m_sPath=lsStr.Left(lsStr.GetLength()-13); m_sFileName=lsStr.Right(13); ls=lsStr.Right(8); ls=ls.Left(4); //----显示图像名称---- //m_wndStatusBar. m_iFrameNo=atoi((LPCTSTR)ls); f.Close(); Invalidate(FALSE); } } void CBackmodelView::OnFileOpenback() { // TODO: Add your command handler code here static char szFilter[] = "位图文件 (*.bmp)|*.bmp|所有文件 (*.*)|*.*||"; CFileDialog* opendlg=new CFileDialog(TRUE,NULL,NULL, OFN_FILEMUSTEXIST|OFN_HIDEREADONLY, szFilter,AfxGetMainWnd()); if(opendlg->DoModal()==IDOK) { CFile f; CFileException e; if( !f.Open( opendlg->GetPathName(), CFile::modeRead |CFile::typeBinary , &e ) ) { #ifdef _DEBUG afxDump << "File could not be opened " << e.m_cause << "\n"; #endif } m_ImageProcess.LoadImage(&f,m_ImageProcess.m_lpBackImage); m_bHaveBack=TRUE; f.Close(); Invalidate(FALSE); } } void CBackmodelView::OnImageDiff() { // TODO: Add your command handler code here DWORD t1,t2; double time; CString s; t1=GetTickCount(); m_ImageProcess.simpleFrameDiff(); t2=GetTickCount(); time=(t2-t1)/1000.0; s.Format("消耗时间%.3f秒",time); MessageBox(s); m_iShowImageMode=2; Invalidate(FALSE); } void CBackmodelView::OnImageMedian() { // TODO: Add your command handler code here // 得到当前图像属于多少桢 N // 对前N-1桢进行背景建模 if(!m_bHaveObject) { CString s="打开目标文件先"; MessageBox(s); return; } int startFrame; if((m_iFrameNo-20)<0) { //load image from 0~m_iFrame-1 startFrame=0; //framesNum=m_iFrameNo-1; } else { startFrame=m_iFrameNo-20; //framesNum=5; } DWORD t1,t2; double time; CString s; t1=GetTickCount(); LPBYTE lsImage; float aFa=0.05; lsImage= new BYTE[m_ImageProcess.m_lpMapHeader->biSizeImage]; int w=m_ImageProcess.m_lpMapHeader->biWidth; int h=m_ImageProcess.m_lpMapHeader->biHeight; for(int i=startFrame;i biSizeImage); } else { if(m_ImageProcess.m_lpMapHeader->biBitCount==24) { for(int j=0;j biBitCount==8) { for(int j=0;j biSizeImage); //----sub--- m_ImageProcess.simpleFrameDiff(); t2=GetTickCount(); time=(t2-t1)/1000.0; s.Format("消耗时间%.3f秒",time); MessageBox(s); m_iShowImageMode=4; Invalidate(FALSE); } void CBackmodelView::OnImageDiffcluster() { // TODO: Add your command handler code here CString s; if(m_ImageProcess.m_lpMapHeader->biBitCount!=24) { s="只支持24位真彩色"; MessageBox(s); return; } DWORD t1,t2; double time; t1=GetTickCount(); //m_ImageProcess.simpleFrameDiff(); double MeanValue[500]; int i; int m_iClusterNumb=10; for(i=0;i<500;i++) MeanValue[i]=0.0; t1=GetTickCount(); m_ImageProcess.KCluster(2,MeanValue,m_iClusterNumb,20,0.01); t2=GetTickCount(); time=(t2-t1)/1000.0; s.Format("迭代次数20,聚类数%d,消耗时间%.3f秒",m_iClusterNumb,time); MessageBox(s); m_iShowImageMode=2; Invalidate(FALSE); } void CBackmodelView::OnImageMixgauss() { // TODO: Add your command handler code here CString s; if(m_ImageProcess.m_lpMapHeader->biBitCount!=24) { s="只支持24位彩色图像"; MessageBox(s); return; } DWORD t1,t2; double time; //t1=GetTickCount(); //m_ImageProcess.simpleFrameDiff(); t1=GetTickCount(); if(!m_ImageProcess.doGaussSub(m_ImageProcess.m_lpImage,50)) s="Error"; else { t2=GetTickCount(); time=(t2-t1)/1000.0; s.Format("消耗时间%.3f秒",time); } MessageBox(s); m_iShowImageMode=3; Invalidate(FALSE); } void CBackmodelView::OnVideoHistogram() { // TODO: Add your command handler code here //----进行某一个像素点随着时间变化,rgb的变化图 以及统计图 // use mouse to select a pixel return the x,y CString s; if(!m_bHaveObject) { s="Open the Last image first"; MessageBox(s); return; } TSetXY dlg; //dlg.m_nPoint=m_ImageProcess.m_CystalCluster.m_dTemperature; //dlg.m_iEditClusterNumb=m_iClusterNumb; if(dlg.DoModal()==1) { m_iPointX=dlg.m_nPointX; m_iPointY=dlg.m_nPointY; } m_iCurFrame=0; if(m_iCurFrame>=m_iFrameNo) { s="Open the Last Frame!"; MessageBox(s); return; } else m_vMyXY.resize(m_iFrameNo+1); m_iShowImageMode=5; SetTimer(1,100,NULL); Invalidate(FALSE); //for(int } void CBackmodelView::OnTimer(UINT nIDEvent) { // TODO: Add your message handler code here and/or call default Invalidate(FALSE); CView::OnTimer(nIDEvent); } void CBackmodelView::capturePoint() { if(m_iCurFrame>m_iFrameNo) { KillTimer(1); return; } CFile f; CFileException e; CString FileName; FileName.Format("image%04d.bmp", m_iCurFrame); FileName= m_sPath+FileName; if( !f.Open( FileName, CFile::modeRead |CFile::typeBinary , &e ) ) { #ifdef _DEBUG afxDump << "File could not be opened " << e.m_cause << "\n"; #endif } //current image Fi m_ImageProcess.LoadImage(&f,m_ImageProcess.m_lpBackImage); f.Close(); int r,g,b; int w=m_ImageProcess.m_lpMapHeader->biWidth; int h=m_ImageProcess.m_lpMapHeader->biHeight; r=*(m_ImageProcess.m_lpBackImage+w*m_iPointX*3+m_iPointY); g=*(m_ImageProcess.m_lpBackImage+w*m_iPointX*3+m_iPointY+1); b=*(m_ImageProcess.m_lpBackImage+w*m_iPointX*3+m_iPointY+2); m_vMyXY[m_iCurFrame].setRgb(r,g,b); m_iCurFrame++; } void CBackmodelView::drawCurve(CDC* pDC) { CPen Pen; CPoint m_Point,m_PointOld; int i; Pen.CreatePen(PS_SOLID,1,RGB(255,0,0)); pDC->SelectObject(&Pen); for(i=1;i MoveTo(m_PointOld); pDC->LineTo(m_Point); } } void CBackmodelView::drawXY(CDC* pDC) { CPen Pen; CPoint m_Point; CBrush Brush; int i; Pen.CreatePen(PS_SOLID,1,RGB(0,0,0)); pDC->SelectObject(&Pen); //---Y-------------- pDC->MoveTo(400,10); pDC->LineTo(400,265); //---arrow---- pDC->MoveTo(400,10); pDC->LineTo(395,20); pDC->MoveTo(400,10); pDC->LineTo(405,20); //---labels---- pDC->MoveTo(400,65); pDC->LineTo(410,65); pDC->MoveTo(400,165); pDC->LineTo(410,165); CString s; s="200"; pDC->TextOut(370,65,s); s="100"; pDC->TextOut(370,165,s); //-------X------------- pDC->MoveTo(400,265); pDC->LineTo(800,265); //---arrow-------------- pDC->MoveTo(800,265); pDC->LineTo(790,260); pDC->MoveTo(800,265); pDC->LineTo(790,270); } void CBackmodelView::OnFileOpenvideo() { int i,j; char* s; CFile f; CFileException e; char filename[]="E:\\Sample256-1.bmp"; int w=m_ImageProcess.m_lpMapHeader->biWidth; int h=m_ImageProcess.m_lpMapHeader->biHeight; double FrameNum;//视频帧数 CFileDialog dlg(TRUE, _T("*.avi"), "", OFN_FILEMUSTEXIST|OFN_PATHMUSTEXIST|OFN_HIDEREADONLY, "AVI files (*.avi) |*.avi|All Files (*.*)|*.*||",NULL); CString strFileName; char title[]= {"打开视频文件"}; dlg.m_ofn.lpstrTitle= title; if (dlg.DoModal() == IDOK) { strFileName= dlg.GetPathName(); } IplImage* tmp_frame = NULL; //IplImage* image = NULL; //IplImage* image1=0; //IplImage* image2=0 ; CvCapture* capture = NULL; capture= cvCaptureFromFile(strFileName); FrameNum=cvGetCaptureProperty(capture,CV_CAP_PROP_FRAME_COUNT); //处理视频文件 for(i=0;i origin=1; //image=cvCreateImage(cvGetSize(tmp_frame),IPL_DEPTH_8U,3); //image=cvCreateImage(cvGetSize(tmp_frame),8,3); //image1=cvCreateImage(cvGetSize(tmp_frame),8,1); //image2=cvCreateImage(cvGetSize(tmp_frame),8,1); //image1->origin = 1; //image2->origin = 1; //image->imageDataOrigin = tmp_frame->imageDataOrigin; //image->imageData = tmp_frame->imageData; //cvCvtColor(image, image1, CV_RGB2GRAY); //cvSmooth(image1,image2,CV_GAUSSIAN,3,0,0);*/ //取第一幅图像初始背景 /////////////////////////////////////////////////////////// if(i==0) { cvSaveImage(filename,tmp_frame); if( !f.Open(filename, CFile::modeRead |CFile::typeBinary , &e ) ) { #ifdef _DEBUG afxDump << "File could not be opened " << e.m_cause << "\n"; #endif } m_ImageProcess.LoadImage(&f,m_ImageProcess.m_lpBackImage); m_bHaveBack=TRUE; f.Close(); } //////////////////////////////////////////////////////////////// //判断是前景还是背景(是否可以把视频的第一帧作为背景) m_ImageProcess.doGaussSub(tmp_frame->imageDataOrigin,i); /* if(i==10) { m_bHaveBack=TRUE; }*/ m_iShowImageMode=3; //cvSetData(image,m_ImageProcess.m_lpMask,image->width*3); /*//rgbtogray cvCvtColor(image, image1, CV_RGB2GRAY); //高斯平滑 cvSmooth(image1,image1,CV_GAUSSIAN,3,0,0); //二值化前景图 cvThreshold(image1, image1, 60, 255.0, CV_THRESH_BINARY); //进行形态学滤波,去掉噪音 cvErode(image1, image1, 0, 1); cvDilate(image1, image1, 0, 1);*/ //cvNamedWindow( "ObjectTracking", 1 ); //cvShowImage("ObjectTracking", image1); //cvWaitKey(250); // cvReleaseImage(&image); //cvReleaseImage(&image1); // cvReleaseImage(&image2); //cvReleaseImageHeader(&image); Invalidate(FALSE); } /*G,B,R可以这样提取: image->imageData[ (i*width+j)*3] image->imageData[(i*width+j)*3+1] image->imageData[(i*width+j)*3+2] */ /* image->imageDataOrigin[i*3] image->imageDataOrigin[i*3+1] image->imageDataOrigin[i*3+2] */ cvReleaseCapture( &capture ); cvDestroyWindow("ObjectTracking"); cvDestroyWindow("Raw Video"); }