void CroadvisionDlg::OnButtonOpen()
{
// TODO: 在此添加控件通知处理程序代码
//CFileDialog *dlgopen=new CFileDialog(true,"*.avi",NULL,NULL,"*.avi|*.avi||");
CFileDialog *dlgopen=new CFileDialog(true);
CString m_filename;
if(dlgopen->DoModal()==IDOK)
{
m_filename=dlgopen->GetPathName();
}
else
{
MessageBox("所要打开的文件无法打开。");
return;
}
//delete dlgopen;
if(m_filename.Find(".jpg")!=-1||m_filename.Find(".JPEG")!=-1||m_filename.Find(".bmp")!=-1)
{
imagesmooth(m_filename);
return;
}
//m_filename.Replace(_T("\\"),_T("\\\\"));
//char* m_fileName=(char*)m_filename.GetBuffer(m_filename.GetLength());
// if (!(pcapture=cvCaptureFromFile((char *)m_videoPath.GetBuffer(m_videoPath.GetLength()))))
if( !(pCapture = cvCaptureFromFile(m_filename)))
//if(!(pCapture = cvCaptureFromFile((char *)m_filename.GetBuffer(m_filename.GetLength()))))
{
MessageBox("打开出错!");
return;
}
//m_filename.ReleaseBuffer();
int framecount=static_cast<int>(cvGetCaptureProperty(pCapture,CV_CAP_PROP_FRAME_COUNT));
cvNamedWindow( "原始视频", 0);
int trackval=40;
cvCreateTrackbar("播放进度","原始视频",&trackval,100,ont_trackbar);
cvMoveWindow("原始视频",0,0);
cvResizeWindow("原始视频",300,300);
int currentpos=0,index=0;
pos=0;
InitializeCriticalSection(&CriticalSection);**//调试的时候缺了这句,结果一直报错
//处有未经处理的异常: 0xC0000005: 读取位置 0xfefeff66 时发生访问冲突,后来加上就好了**
EnterCriticalSection(&CriticalSection);
while(pFrame = cvQueryFrame(pCapture))
{
LeaveCriticalSection(&CriticalSection);
//DeleteCriticalSection(&CriticalSection);
currentpos=cvGetTrackbarPos("播放进度","原始视频");
if(abs(currentpos-pos)>5)
{
cvSetTrackbarPos("播放进度","原始视频",pos);
cvSetCaptureProperty(pCapture,CV_CAP_PROP_POS_FRAMES,pos*framecount/100);
index=pos*framecount/100;
}
else
{
pos=(index++)*100/framecount;
cvSetTrackbarPos("播放进度","原始视频",pos);
}
// event1.SetEvent();
cvShowImage("原始视频",pFrame);
if(!WaitKey(5,"原始视频"))
break;
//InitializeCriticalSection(&CriticalSection);
EnterCriticalSection(&CriticalSection);
}
// event1.SetEvent();
EnterCriticalSection(&CriticalSection);
cvReleaseImage(&pFrame);
LeaveCriticalSection(&CriticalSection);
cvReleaseCapture(&pCapture);
pCapture=NULL;
cvDestroyWindow("原始视频");
}
另外在预处理线程中也有一个问题:
void CroadvisionDlg::OnButtonPreprocess()
{
// TODO: 在此添加控件通知处理程序代码
AfxBeginThread(PreProcessProc,(LPVOID)&pFrame);
}
//******************************************************////////////
//**********************预处理线程**********************////////////
//******************************************************////////////
UINT PreProcessProc(LPVOID image)
{
IplImage** tempimage=(IplImage**)image;
cvNamedWindow( "预处理", 0);
cvMoveWindow("预处理",400,0);
cvResizeWindow("预处理",300,300);
PreProcessImg=cvCreateImage(cvSize((*tempimage)->width,(*tempimage)->height),IPL_DEPTH_8U,1);
EnterCriticalSection(&CriticalSection);
IplImage* greyimage=cvCreateImage(cvSize((*tempimage)->width,(*tempimage)->height),IPL_DEPTH_8U,1);
while(*tempimage)
{
cvCvtColor(*tempimage, greyimage, CV_BGR2GRAY);
// cvSobel(PreProcessImg,PreProcessImg,1,0,CV_SCHARR);
//cvSmooth(greyimage,greyimage,CV_BILATERAL,3,0,3,50);
//把cvSmooth函数注释掉就好了,还不知是为什么,还在学习中
ImproveOTSU(greyimage,PreProcessImg);
cvAnd(PreProcessImg,greyimage,PreProcessImg);
// cvSmooth(PreProcessImg,PreProcessImg,CV_GAUSSIAN);
LeaveCriticalSection(&CriticalSection);
cvShowImage("预处理",PreProcessImg);
if(!WaitKey(5,"预处理"))
{
break;
}
EnterCriticalSection(&CriticalSection);
}
// EnterCriticalSection(&CriticalSection);
cvReleaseImage(&PreProcessImg);
LeaveCriticalSection(&CriticalSection);
cvDestroyWindow("预处理");
AfxEndThread(0);
return 0;
}