📄 dlgimageprocess.cpp
字号:
// DlgImageProcess.cpp : implementation file
//
#include "stdafx.h"
#include "ObjectExtraction.h"
#include "DlgImageProcess.h"
#include "io.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CDlgImageProcess dialog
CDlgImageProcess::CDlgImageProcess(CWnd* pParent /*=NULL*/)
: CDialog(CDlgImageProcess::IDD, pParent)
{
//{{AFX_DATA_INIT(CDlgImageProcess)
// NOTE: the ClassWizard will add member initialization here
//}}AFX_DATA_INIT
}
CDlgImageProcess::CDlgImageProcess(CvCapture* pCapture, int pos,CString path,
CString winName,CString tracbarName,CWnd* pParent /*=NULL*/)
: CDialog(CDlgImageProcess::IDD, pParent)
{
this->pCapture=pCapture;
this->pos=pos;
this->winName=winName;
this->tracBarName=tracbarName;
this->m_path=path;
m_count=(int)cvGetCaptureProperty(pCapture,CV_CAP_PROP_FRAME_COUNT);
m_isChanged=new bool[m_count];
for(int i=0;i<m_count;i++)
{
m_isChanged[i]=false;
}
CString path1=m_path;
int index=path1.Find(".avi");
m_path_tmp=path1.Left(index);
}
BOOL CDlgImageProcess::OnInitDialog()
{
CDialog::OnInitDialog();
int y = ::GetSystemMetrics(SM_CYSCREEN) ;
int x = ::GetSystemMetrics(SM_CXSCREEN) ;
/*cvMoveWindow("image",x/2-x/4,200);*/
int width=(int)cvGetCaptureProperty(pCapture,CV_CAP_PROP_FRAME_WIDTH);
int height=(int)cvGetCaptureProperty(pCapture,CV_CAP_PROP_FRAME_HEIGHT);
CRect rect;
rect.left=x/2-9*x/20+width+10;
rect.top=200;
rect.right=x/2-x/4+width+300;
rect.bottom=200+height+200;
MoveWindow(&rect);
return TRUE; // return TRUE unless you set the focus to a control
// EXCEPTION: OCX Property Pages should return FALSE
}
CDlgImageProcess::~CDlgImageProcess()
{
if(m_isChanged!=NULL)
{
delete [] m_isChanged;
}
}
void CDlgImageProcess::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CDlgImageProcess)
// NOTE: the ClassWizard will add DDX and DDV calls here
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CDlgImageProcess, CDialog)
//{{AFX_MSG_MAP(CDlgImageProcess)
ON_BN_CLICKED(IDC_BUTTON_START, OnButtonStart)
ON_BN_CLICKED(IDC_BUTTON_STOP, OnButtonStop)
ON_BN_CLICKED(IDC_BUTTON_FORFRAME, OnButtonForframe)
ON_BN_CLICKED(IDC_BUTTON_LASTFRAME, OnButtonLastframe)
ON_WM_CLOSE()
ON_BN_CLICKED(IDC_BUTTON_SHARP, OnButtonSharp)
ON_BN_CLICKED(IDC_BUTTON_SAVE, OnButtonSave)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CDlgImageProcess message handlers
void CDlgImageProcess::OnButtonStart()
{
isStart=true;
pos=(int)cvGetCaptureProperty(pCapture,CV_CAP_PROP_POS_FRAMES)-1;
cvSetCaptureProperty(pCapture,CV_CAP_PROP_POS_FRAMES,pos);
int count=(int)cvGetCaptureProperty(pCapture,CV_CAP_PROP_FRAME_COUNT);
//cvDestroyWindow(winName);
cvNamedWindow(winName,CV_WINDOW_AUTOSIZE);
while(pImage=cvQueryFrame(pCapture))
{
if(!isStart)
{
break;
}
pos=(int)cvGetCaptureProperty(pCapture,CV_CAP_PROP_POS_FRAMES)-1;
cvShowImage(winName,pImage);
//cvWriteFrame(writer,pImage);
cvSetTrackbarPos("image","image",pos);
if(cvWaitKey(1)>=0 || pos>=count)
{
break;
}
}
}
void CDlgImageProcess::OnButtonStop()
{
isStart=false;
}
void CDlgImageProcess::OnButtonForframe()
{
pos=cvGetTrackbarPos(tracBarName,winName);
pos-=1;
cvSetTrackbarPos(tracBarName,winName,pos);
cvSetCaptureProperty(pCapture,CV_CAP_PROP_POS_FRAMES,pos);
pImage=cvQueryFrame(pCapture);
cvShowImage(winName,pImage);
}
void CDlgImageProcess::OnButtonLastframe()
{
pos=cvGetTrackbarPos(tracBarName,winName);
pos+=1;
cvSetTrackbarPos(tracBarName,winName,pos);
cvSetCaptureProperty(pCapture,CV_CAP_PROP_POS_FRAMES,pos);
pImage=cvQueryFrame(pCapture);
cvShowImage(winName,pImage);
}
void CDlgImageProcess::OnClose()
{
// TODO: Add your message handler code here and/or call default
isStart=false;
CDialog::OnClose();
}
void CDlgImageProcess::OnButtonSharp()
{
CString str;
pos=cvGetTrackbarPos(tracBarName,winName);
cvSetCaptureProperty(pCapture,CV_CAP_PROP_POS_FRAMES,pos);
pImage=cvQueryFrame(pCapture);
m_isChanged[pos]=true;
str.Format("%d",pos);
cvSaveImage(m_path_tmp+str+".jpeg",pImage);
}
void CDlgImageProcess::OnButtonSave()
{
CFileDialog dlg(FALSE,"avi",m_path,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=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;
}
cvSetCaptureProperty(pCapture,CV_CAP_PROP_POS_FRAMES,0);
int num=0;
while(pImage=cvQueryFrame(pCapture))
{
num=(int)cvGetCaptureProperty(pCapture,CV_CAP_PROP_POS_FRAMES)-1;
if(num>m_count)
{
break;
}
if(m_isChanged[num])
{
CString str;
str.Format("%d",num);
if(_access(m_path_tmp+str+".jpeg",0)!=0)
{
AfxMessageBox("文件存储错误");
return;
}
pImage=cvLoadImage(m_path_tmp+str+".jpeg",CV_LOAD_IMAGE_COLOR);
if(DeleteFile(m_path_tmp+str+".jpeg")==0)
{
return;
}
//cvWriteFrame(writer,pImage);
}
cvWriteFrame(writer,pImage);
}
cvReleaseVideoWriter(&writer);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -