opencv读取摄像头图像和读取视频文件图像

vc6+opencv1.0;

 

#include "cv.h"
#include "highgui.h"
#pragma comment(lib,"cxcore.lib")
#pragma comment(lib,"cv.lib")
#pragma comment(lib,"highgui.lib")

 


void CTOpenCVDlg::OnOpencvCapture()
{
 // TODO: Add your control notification handler code here
 

 CvCapture* capture = 0;
    IplImage *frame = 0;

    capture = cvCaptureFromCAM( 0 ); //´ÓÉãÏñÍ·¶ÁȡͼÏñ£»
// capture = cvCaptureFromAVI( "G:\\t.avi" ); //´ÓÊÓƵÎļþ¶ÁȡͼÏñ(δѹËõµÄÊÓƵÎļþ)£»×¢£ºopencvÑ­»·ËٶȾÍÊǶÁÈ¡ÎļþËٶȣ¬ËùÒÔÒªÔÚÑ­»·ÖÐÐÝÃߣ¬ÈçӰƬ²¥·ÅËÙ¶ÈΪ25Ö¡/Ã룺 Sleep( 1000/25 );

 if( capture )
    {
        for(;;)
        {
   if ( 0 )
   {
    if( !cvGrabFrame( capture ) )
    {
     break;
    }
    
    frame = cvRetrieveFrame( capture );
    if( !frame )
    {
     break; 
    }
   }
   else
   {
    frame = cvQueryFrame( capture ); //Just a combination of cvGrabFrame and cvRetrieveFrame;

    if( !frame )
    {
     break; 
    }
   }
  
   
   CRect rect;  GetClientRect(&rect);

   CvvImage cvimage;
   cvimage.CopyOf( frame );
   cvimage.DrawToHDC( CClientDC(this), &rect); 
    
   //Sleep( 1000/25 );//²¥·ÅÊÓƵ£»
        }
  
        cvReleaseCapture( &capture );
    }

}

//------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

VC2008简单封装一个类:

 

#include "cvaux.h"
#include "cxmisc.h"
//#include "highgui.h"
#include
#include
#include

#include
#include
#include


#pragma comment(lib,"cv210d.lib")
#pragma comment(lib,"cvaux210d.lib")
//#pragma comment(lib,"cvhaartraining.lib")
#pragma comment(lib,"cxcore210d.lib")
#pragma comment(lib,"cxts210d.lib")
#pragma comment(lib,"highgui210d.lib")
#pragma comment(lib,"ml210d.lib")
#pragma comment(lib,"opencv_ffmpeg210d.lib")

 


class COpenCVCapture
{
public:
 COpenCVCapture( CWnd * pImageShowWnd )
 {
  m_pDrawImage2Wnd = pImageShowWnd;

  Init();
 }

 ~COpenCVCapture()
 {

  cvReleaseCapture( &capture );

 }

private:

 CWnd * m_pDrawImage2Wnd;

 IplImage* rawImage ;  
 CvCapture* capture ;  

 void Init()
 {
  rawImage = NULL;
  capture = NULL;

  capture = cvCaptureFromCAM( 1 );  //显示摄像头;
 }

public:

 IplImage * get_QueryFrame()
 {
  rawImage = cvQueryFrame( capture );  //获取图像;

  if( m_pDrawImage2Wnd )//绘图;
  {
   CvvImage cvImage;
   cvImage.CopyOf( rawImage  );  //处理 IplImage, CvvImage重新获取;

   CRect rect; m_pDrawImage2Wnd->GetClientRect(&rect);
   cvImage.DrawToHDC( CClientDC(m_pDrawImage2Wnd), &rect );
  }

  return rawImage;
 }

};

 
void CtOpenCVDlg::OnBnClickedButton()
{
 // TODO: 在此添加控件通知处理程序代码


  static COpenCVCapture COpenCVCaptureObj(this);

  COpenCVCaptureObj.get_QueryFrame();

}

 

 




// opencv 2.4.11 版本后的方法,当然,前面的方法依然可以用,但是用CvvImage绘图的方法不用了;


#include // for standard I/O
#include   // for strings
#include  // for controlling float print precision
#include  // string to number conversion


#include        // Basic OpenCV structures (cv::Mat, Scalar)
#include  // Gaussian Blur
#include  // OpenCV window I/O


#pragma comment(lib,"opencv_calib3d2411d.lib")
#pragma comment(lib,"opencv_contrib2411d.lib")
#pragma comment(lib,"opencv_core2411d.lib")
#pragma comment(lib,"opencv_features2d2411d.lib")
#pragma comment(lib,"opencv_flann2411d.lib")
#pragma comment(lib,"opencv_gpu2411d.lib")
#pragma comment(lib,"opencv_highgui2411d.lib")
#pragma comment(lib,"opencv_imgproc2411d.lib")
#pragma comment(lib,"opencv_legacy2411d.lib")
#pragma comment(lib,"opencv_ml2411d.lib")
#pragma comment(lib,"opencv_nonfree2411d.lib")
#pragma comment(lib,"opencv_objdetect2411d.lib")
#pragma comment(lib,"opencv_ocl2411d.lib")
#pragma comment(lib,"opencv_photo2411d.lib")
#pragma comment(lib,"opencv_stitching2411d.lib")
#pragma comment(lib,"opencv_superres2411d.lib")
#pragma comment(lib,"opencv_ts2411d.lib")
#pragma comment(lib,"opencv_video2411d.lib")
#pragma comment(lib,"opencv_videostab2411d.lib")
using namespace std;
using namespace cv;




UINT videoproc( PVOID param )
{
GdiplusStartupInput gdiplusStartupInput;
ULONG_PTR           gdiplusToken;
GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL);




VideoCapture  * pvideo1 = new VideoCapture(0);


if ( pvideo1 )
{
if ( pvideo1->isOpened()==0 )
{
return 0;
}
}


while(1)
{
Mat image;


pvideo1->read(image);


Bitmap bmp(  image.cols,  image.rows,  image.step, PixelFormat24bppRGB ,  image.data );


Graphics g( theApp.m_pMainWnd->m_hWnd );
g.DrawImage( &bmp, 0,0 ); 

}


pvideo1->release();


return 0;
}




 


void Ctest_opencv2411Dlg::OnBnClickedButton1()
{
// TODO: 在此添加控件通知处理程序代码


AfxBeginThread(videoproc,0);


}

你可能感兴趣的:(OpenCV)