OpenCV get the webcam video and display it on the picture control of MFC

Source: Internet
Author: User

Divided into two steps: OpenCV get camera Data + image display on picture

Step one: OpenCV get camera data

Reference:http://www.cnblogs.com/epirus/archive/2012/06/04/2535190.html

Http://blog.sina.com.cn/s/blog_6dbe9bdb0100nii7.html

http://blog.csdn.net/augusdi/article/details/8762961

#include"stdafx.h"#include<atltime.h>#includeintMain () {intC;    CTime time; Iplimage*img; Cvcapture* Capture = Cvcapturefromcam (1); Cvnamedwindow ("Mainwin", cv_window_autosize); Cvmovewindow ("Mainwin", -, -);  while(1) {img=Cvqueryframe (Capture); Cvshowimage ("Mainwin", IMG); C=cvwaitkey (Ten); //Press S to save as JPG files        if(c=='s') { time=Ctime::getcurrenttime (); CStringA filename (time. Format (CString ("%y%m%d%h%m%s"))+". jpg");        Cvsaveimage (FILENAME,IMG); }        //Press ESC to detach        if(c = = -)         Break; } cvreleaseimage (&img); Cvdestroywindow ("Mainwin"); return 0;}

Here, here are some important functions to be introduced:

1.CvCapture

Video acquisition structure typedef struct CVCAPTURE cvcapture;

Release this structure, using the function cvreleasecapture.

struct Cvcapture does not have a public interface, it can only be used as a parameter to the video fetch function. This is a very important structure that must be used when reading an existing video or getting it from the camera.

2.cvCaptureFromCAM

cvcapture* cvcapturefromcam (int index);

Parameters: Index of the camera to be used by index. It doesn't matter if you have only one camera or a camera, so use the parameter 1.

The function assigns and initializes the cvcapture structure to the video stream from the camera. There are currently two interfaces available under Windows: Video for Windows (VFW) and Matrox Imaging Library (MIL), and two interfaces under Linux: v4l and FireWire (IEEE1394).

Read the camera by: cvcapture* Capture=cvcapturefromcam (0);//parameter can also be-1,

Read AVI file demo by: cvcapture* capture=cvcapturefromfile ("Xxx.avi");

Cvcreatecameracapture seems to have no difference.

3.cvQueryFrame

Grab and return a frame from the camera or file

iplimage* Cvqueryframe (cvcapture* capture);
Capture video capture structure.

The function Cvqueryframe fetches a frame from the camera or file and then extracts and returns the frame. This function is simply a combination of the function Cvgrabframe and the function cvretrieveframe called together. The returned image can not be released or modified by the user. After crawling, capture is pointed to the next frame and can be cvsetcaptureproperty adjusted to the appropriate frame.

Attention:the pointer returned by Cvqueryframe always points to the same piece of memory。 A copy is recommended after Cvqueryframe. And the returned frame needs flip to conform to the OpenCV coordinate system. If the return value is NULL, the last frame or failure of the video is indicated! Proof of the same piece of memory: http://blog.sina.com.cn/s/blog_6d1fb09e0100wcm0.html

4.cvGrabFrame

Grab the frame int cvgrabframe (cvcapture* capture) from the camera or video file, and the function Cvgrabframe grab the frame from the camera or file. The captured frames are stored internally. The purpose of this function is to quickly grab the frame, whichSimultaneous reading of data from several camerasis very important. The captured frame may be in a compressed format (defined by the camera/driver), so it is not exposed.If you want to retrieve the captured frames, use the Cvretrieveframe。 Cvqueryframe is a combination call of function cvgrabframe and function cvretrieveframe that returns an image directly. Alone cvgrabframe cannot. Reference: http://blog.csdn.net/autumn20080101/article/details/7515520Examples of programs:
intMainintargcChar**argv) {  //if (argc = = 2 && (pImg = Cvloadimage (argv[1], cv_load_image_unchanged))! = 0)//return 0; cvcapture* capture = Cvcapturefromcam (0);//capture from video device #0//cvcapture* capture = Cvcapturefromavi ("Infile.avi"); iplimage* img =0; //cvapi (int) cvgrabframe (cvcapture* capture); Cvnamedwindow ("Image", cv_window_autosize); CharC=cvwaitkey (3);  while(c!= -)  {  if(!cvgrabframe (Capture)) {//capture A Frameprintf"Could not grab a frame\n\7"); Exit (0); }      //Cvapi (iplimage*) cvretrieveframe (cvcapture* capture); Img=cvretrieveframe (Capture);//retrieve the captured frameCvshowimage ("Image", IMG); C=cvwaitkey ( -); }  //the image obtained by the video stream catcher is an operation that allocates and frees memory by the trap and does not need to free the memory separately from the image.//cvreleaseimage (&IMG); Cvdestroywindow ("Image"); Cvreleasecapture (&capture); return 0; }  

Step Two: Iplimage display on the picture controlHere, the camera video if displayed on the picture, and real-time update, implementation method: Multithreading or timer, here with the timer to achieve video data updates. Reference: http://www.cnblogs.com/leven20061001/archive/2012/10/17/2727865.html, (Timer implementation, more detailed) http://blog.csdn.net/poem_ of_sunshine/article/details/17123577Examples of programs:
//to display iplimage to a controlvoidCPALMVEINRECOGDLG::IPLDRAWTOHDC (iplimage* image, UINT ID)//ID is the ID number of the picture control control{CDC* PDC = GetDlgItem (ID),GetDC (); HDC PHDC= Pdc->getsafehdc ();//gets the drawing handle of the picture control from the MFC interface, HDC,CRect rect; GetDlgItem (ID)->getclientrect (&rect);//rect is the size of the control, obtaining the rectangular area where the Pictrue control resides//Setstretchbltmode (PHDC, stretch_halftone);Cvvimage cimg; //converts a picture of the Iplimage type to a picture format that MFC can display Cvvimagecimg.    CopyOf (image); Cimg.    DRAWTOHDC (PHDC, rect); ReleaseDC (PDC);//Be sure to remember to release the DC, otherwise it will cause a memory leak}

The third step: Complete program implementation

//turn on camera event handlingvoidCpalmveinrecogdlg::onbnclickedopencam () {CString cStr; CWnd* Pwndopencam =GetDlgItem (Idc_opencam); //www = GetDlgItem (idc_showimage2);Pwndopencam->GetWindowText (CSTR); if(CStr = ="turn on the camera")    {        if(!m_capture) {M_capture= Cvcapturefromcam (0); if(!m_capture) {M_strdebuginfo.setwindowtext (_t ("failed to turn on the camera! ")); return; } M_iscamopen=true; Pwndopencam->setwindowtext ("turn off the camera"); M_strdebuginfo.setwindowtext (_t ("turn on the camera successfully! ")); //the image obtained by the video stream catcher is an operation that allocates and frees memory by the trap and does not need to free the memory separately from the image.M_currentframe =Cvqueryframe (m_capture); //to display iplimage to a controlIpldrawtohdc (M_currentframe, idc_showimage); } settimer (Id_timercam, Nclocktime, NULL); //Timer id:1, timer delay:    }    Else{cvreleasecapture (&m_capture);        KillTimer (Id_timercam); Pwndopencam->setwindowtext ("turn on the camera"); M_strdebuginfo.setwindowtext (_t ("turn off the camera successfully! ")); }}/******************************************** Setting Timer *********************************************/voidCpalmveinrecogdlg::ontimer (uint_ptr nidevent) {//TODO: Add Message Handler code and/or call default values here    Switch(nidevent) { CaseId_timercam://Camera Display Timer        {            if(m_iscamopen) {m_currentframe=Cvqueryframe (m_capture); //to display iplimage to a controlIpldrawtohdc (M_currentframe, idc_showimage); }             Break; }} cdialogex::ontimer (Nidevent);}

PS: Follow-up supplement

OpenCV get the webcam video and display it on the picture control of MFC

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.