⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 capturewnd.cpp

📁 基于视觉的道路识别技术的智能小车导航源代码
💻 CPP
📖 第 1 页 / 共 2 页
字号:
int CCaptureWnd::GetParameter(int Item)
{
	BITMAPINFO bmi;
	int result;
	capGetVideoFormat(m_hCapWnd,&bmi,sizeof(bmi));
	switch(Item)
	{
	case 0:
    	result=bmi.bmiHeader.biHeight;
		break;
	case 1:
		result=bmi.bmiHeader.biWidth ;
		break;
	case 2:
		result=bmi.bmiHeader.biBitCount;
		break;
	case 3:
		result=bmi.bmiHeader .biCompression;
	default:
		result=0;
		break;
	}
	
	return result;
}


//回调函数使应用能够直接使用音频和视频
//应用程序可用捕获窗来登记回调函数
BOOL CCaptureWnd::SetCallbackproc(int Function ,BOOL bOperation)
{ 
	if(GetParameter(2)!=24||GetParameter(3)!=BI_RGB)
	{
		AfxMessageBox("视频格式设定不正确!!\n\n  请设为RGB 24");
			return FALSE;
	}
	switch(Function)
	{

	case 1:
           // capSetCallbackOnFrame(m_hCapWnd,&CallBackOnFrameForAlign);
		    break;
	case 2:
          
          // capSetCallbackOnVideoStream(m_hCapWnd,NULL);  
		   //首先登记回调函数,然后定义回调函数&CallBackOnVideoProc
          /*先登记回调函数*/  //capSetCallbackOnFrame
		//capSetCallbackOnVideoStream(m_hCapWnd,NULL);  
		if(bOperation==FALSE)

		{
           	StopCapture(TRUE);		
			if(!capSetCallbackOnFrame(m_hCapWnd,NULL))
			{
				AfxMessageBox("无法取消捕捉帧!");
			
			    
			}
		}

		else
		{
		 if(capSetCallbackOnFrame(m_hCapWnd,&CallBackOnVideoProc)==FALSE)
		 {
               AfxMessageBox("无法进行动态测量!");
			return FALSE;
		 }
		}
		 //若想不经过文件存储而实时处理数据,就用capCaptureSequenceNoFile
	   
	    break;
	default:
			break;
	}
	//再取消已经定义的回调函数
     //capSetCallbackOnFrame(m_hCapWnd,NULL);
    //capSetCallbackOnVideoStream(m_hCapWnd,NULL);  
   return TRUE;
}

void CCaptureWnd::StartCapture(BOOL bFile)
{   //设置一些视频捕获参数
	CAPTUREPARMS capParam;
	capCaptureGetSetup(m_hCapWnd,&capParam,sizeof(capParam));
    capParam.fAbortLeftMouse=FALSE;
	capParam.fAbortRightMouse=FALSE;
	capParam.fYield=TRUE;
	capParam.wNumVideoRequested =1;
//	capParam.wPercentDropForError=10;
//	capParam.dwRequestMicroSecPerFrame=4000;//设置每帧时间为40ms
	capParam.fLimitEnabled=FALSE;//TRUE;
	capParam.fCaptureAudio=FALSE;
		  // capParam.wTimeLimit=10;
    capCaptureSetSetup(m_hCapWnd,&capParam,sizeof(capParam));
    
  if(bFile)
	{
		SaveAVI();
	}
	else
	{
		capCaptureSequenceNoFile(m_hCapWnd);
	}
}

void CCaptureWnd::StopCapture(BOOL bSave)
{
  if(bSave)
	{
		capCaptureStop(m_hCapWnd);
	}
	else
	{
		capCaptureAbort(m_hCapWnd);
	}
}

BOOL CCaptureWnd::SetVideoStreamParam(int Framespersecond=15)
{
  if(Framespersecond<1)
	{
		AfxMessageBox("参数设置不当,请重新设置!");
		return FALSE;
	}
	 CAPTUREPARMS params;
	 capCaptureGetSetup(m_hCapWnd,&params,sizeof(params));
	 //Requested frame rate, in microseconds. 
	 //The default value is 66667, which corresponds to 15 frames per second. 

	    // dwRequestMicroSecPerFrame 为每帧捕捉的秒数
	  params.dwRequestMicroSecPerFrame=DWORD(1.0e6/Framespersecond);
	  //wTimeLimit指示是否有最大时间限制,单位为秒
	  //params.wTimeLimit=nCapturetime;

	  params.fAbortLeftMouse=FALSE;//default is TRUE
	  params.fAbortRightMouse=FALSE;//default is TRUE
	  params.fYield=TRUE ;//defaultFALSE
	  //wNumVideoRequested的意义是Maximum number of video buffers to allocate
	  params.wNumVideoRequested =1;
	 // params.wPercentDropForError=10;default is 10
	  params.fLimitEnabled=FALSE;
	  params.fCaptureAudio=FALSE;
	//设置新的捕获参数	   
      if(!capCaptureSetSetup(m_hCapWnd,&params,sizeof(params)))
		   {
			    AfxMessageBox("setup is uncorrect!");
				 return FALSE;
		   }
		 
	return TRUE;
}

BOOL CCaptureWnd::SetVideoFormat(int nWidth, int nHeight)
{
    BITMAPINFO bmi;
	capGetVideoFormat(m_hCapWnd,&bmi,sizeof(bmi));
	bmi.bmiHeader.biBitCount=24;
	bmi.bmiHeader .biHeight=nHeight;
	bmi.bmiHeader .biWidth =nWidth;
	bmi.bmiHeader .biCompression=BI_RGB;
	capSetVideoFormat(m_hCapWnd,&bmi,sizeof(bmi));
                        
	return TRUE;
}



LRESULT CALLBACK CCaptureWnd::CallBackOnVideoProc(HWND h_Wnd, VIDEOHDR *lpVHDR)
{
    BITMAPINFO *lpBmp;
	DWORD VideoFormatSize;
	DWORD nWidth;
	DWORD nHeight;
	WORD  bitsCount;
	LPBYTE lpImage;
	/*
	DWORD x;                                        
	DWORD y;
	DWORD nXpos;
	DWORD nYpos;
	unsigned char  r,g,b,rr,gg,bb;
	float hue,hue1,hue2,temp;
	*/
	CVideoPro VideoPro;
//	DWORD result;
//	RGB rgb;
 //   HSI hsi;
    WINDOW window;
	//------------------------------------
   
	VideoFormatSize=capGetVideoFormatSize(h_Wnd);
	lpBmp=(BITMAPINFO *)new unsigned char[VideoFormatSize];
	capGetVideoFormat(h_Wnd,lpBmp,VideoFormatSize);
	nWidth=(lpBmp->bmiHeader).biWidth;
	nHeight=lpBmp->bmiHeader.biHeight;
	bitsCount=lpBmp->bmiHeader.biBitCount;
	if(bitsCount!=24||lpBmp->bmiHeader.biCompression!=BI_RGB) 
	{
		capSetCallbackOnFrame(h_Wnd,NULL);
		AfxMessageBox("视频格式设定不正确!!\n\n  请设为RGB 24");

		return FALSE;
	}
	//------------------------------------
	
	  lpImage=lpVHDR->lpData;
	  window.LeftX=0;
      window.RightX=int(0.5*nWidth);
      window.BottomY=0;
	  window.TopY=int(0.5*nHeight);
	  window.MinAngle=0;
	  window.MaxAngle=180;
	  
	 //注意:在静态成员函数的实现中不能直接引用类的非静态成员,
	  //也不能引用this 指针,但可以引用类中声明的静态成员
	  //如果静态静态成员函数中需要引用非静态成员时,可以
	  //通过对象来引用
	  
    /* VideoPro.ToGray(lpImage,nHeight,nWidth);
    if( VideoPro.Sobel(lpImage,nHeight,nWidth)==FALSE)
	 {
	     AfxMessageBox("不正确!");

		return FALSE;
	 }
	 */ 
      
	  
	 
	  //计算函数处理花费时间*********************
	  LARGE_INTEGER litmp;
      LONG QPart1,QPart2;
      double dfMinus,dfFreq,dfTim;
      QueryPerformanceFrequency(&litmp);
	  // 获得计数器的时钟频率dfFreq
	  dfFreq = (double)litmp.QuadPart;
      
      VideoPro.ToGray(lpImage,nHeight,nWidth);
	  VideoPro.MedianFilter(lpImage,nHeight,nWidth);
//	  VideoPro.TempSobel(lpImage,nHeight,nWidth);
	  
	  /********************/
	  QueryPerformanceCounter(&litmp);
      QPart1 = litmp.QuadPart;// 获得初始值
	  /*******************/
//	  VideoPro.Threshold(lpImage,nHeight,nWidth);
	  QueryPerformanceCounter(&litmp);
      QPart2 = litmp.QuadPart;// 获得END值
	  VideoPro.Hough(lpImage,nHeight,nWidth,window);
	  
	 // Sleep(2500);
	 // VideoPro.RgbTrans(lpImage,nHeight,nWidth);
      
	  dfMinus=(double)(QPart2-QPart1);
      //dfTim=dfMinus/dfFreq;dfTim单位是秒
	  dfTim=1000*dfMinus/dfFreq;//dfTim单位是毫秒
	  //char buf[80];
	  //sprintf(buf,"rate=%d",dfTim);
	  //AfxMessageBox(buf);
    //  VideoPro.Hough(lpImage,nHeight,nWidth);
      //计算函数处理花费时间*********************
/*	  clock_t start, finish;
    double  duration;
	start=clock();
	 //VideoPro.MedianFilter(lpImage,nHeight,nWidth);
	Sleep(2500);
	finish=clock();
	  duration = (double)(finish - start) / CLOCKS_PER_SEC;
	  start=0;
*/	  
	
   	return (LRESULT)TRUE;

}



void CCaptureWnd::Overlay(BOOL bOverlay)
{
    capPreviewRate (m_hCapWnd,15);
    capOverlay(m_hCapWnd, bOverlay);
}



void CCaptureWnd::SetPreviewRate(int rate)
{
  if(rate<0)
  {
	  char buf[80];
	  sprintf(buf,"rate=%d",rate);
	  AfxMessageBox(buf);
	  return;
  }


  
	capPreviewRate(m_hCapWnd,1000/rate);
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -