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 <stdio.h>
#include <stdlib.h>
#include <ctype.h>

#include <cv.h>
#include <cxcore.h>
#include <highgui.h>


#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 <iostream> // for standard I/O
#include <string>   // for strings
#include <iomanip>  // for controlling float print precision
#include <sstream>  // string to number conversion


#include <opencv2/core/core.hpp>        // Basic OpenCV structures (cv::Mat, Scalar)
#include <opencv2/imgproc/imgproc.hpp>  // Gaussian Blur
#include <opencv2/highgui/highgui.hpp>  // 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);


}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

chinabinlang

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值