Opencv + MFC display images/videos

Source: Internet
Author: User

How to Use opencv to read and display images and videos

Image File Reading and display: http://www.opencv.org.cn/index.php/%E5%9B%BE%E5%83%8F%E6%96%87%E4%BB%B6%E8%AF%BB%E5%85%A5%E5%92%8C%E6%98%BE%E7%A4%BA

Read video files: http://www.opencv.org.cn/index.php/%E8%AF%BB%E8%A7%86%E9%A2%91%E6%96%87%E4%BB%B6%E5%92%8C%E8%BF%90%E5%8A%A8%E7%89%A9%E4%BD%93%E6%A3%80%E6%B5%8B

However, its display is a simple GUI in the opencv library, which can be used for testing and verification. If you want to make a visual project, you still need to use a dedicated GUI development tool. This section describes how to use MFC to display images and videos.

1. Read and display the image

The picture control in VC is used to display images or videos, and the device context (CDC type) using the VC graphical device interface is used ). Lean.jpg

Perform initialization first. The Code is as follows:

1 // demodlg. H 2 # include 

As shown above, the device context handle of the picture control is associated with the rectangular area displayed in the image/video.

The following shows the image. In the dialog box, add an Open button and click this button to trigger the event of reading the displayed image. The event response is as follows:

1 void cdemodlg: onopen () 2 {3 iplimage * IMG = cvloadimage ("lena.jpg"); // load image from file4 cvvimage cvvimg; // cvvimage class
5 cvvimg. copyof (IMG); // copy the image to the current cvvimage object. drawtohdc (HDC, & rect); // draw the ROI of the image to the DC rect7}

2. Read and display the video

Because the video is a frame-by-frame image, the video can be understood as reading and displaying images of different frames continuously. Here is a key point. In order to make rational use of resources, try not to use an endless loop to read the frame data in the video. You should set a proper Clock Based on the frame rate of the video, regularly read and display frame images in the video. Because the frame rate is usually very low, each frame of the displayed image looks continuous.

For video (video files/cameras) operations, opencv Chinese website reference manual for your detailed usage, see http://www.opencv.org.cn/index.php/HighGUI%E8%A7%86%E9%A2%91%E8%AF%BB%E5%86%99%E5%87%BD%E6%95%B0

The following describes how to read video files and how to use them (because there is no camera, the camera has not been tested ).

The initialization work is the same as reading and displaying the image above. The difference is that the Code in the cdemodlg: onopen () Event Response Function is different, as shown below:

1 void cdemodlg: onopen () 2 {3 if (! Capture) 4 {5 capture = cvcapturefromavi ("D: // otheruse // 11.avi"); // initialize a video file catcher 6} 7 if (! Capture) 8 {9 afxmessagebox ("unable to get camera ");
Return; 10} 11 settimer (, null); // timer, the timer time and frame rate are consistent 12}

Then set the time response. After the scheduled time arrives, process the ontimer (uint nidevent) event. The video is displayed here.

1 void cdemodlg: ontimer (uint nidevent) 2 {3 // todo: add your message handler code here and/or call default 4 if (1 = nidevent) 5 {6 iplimage * IMG = 0; 7 IMG = cvqueryframe (capture); // capture from the camera or file and return a frame of 8 // cvgrabframe (capture ); // capture frame 9 from the camera or video file // IMG = cvretrieveframe (capture); // retrieve the image 10 cvvimage m_cvvimage captured by the cvgrabframe function; 11 m_cvvimage.copyof (IMG, 1); // copy the frame image 12 m_cvvimage.drawtohdc (HDC, & rect); // display it in the rectangle of the device 13} 14 cdialog: ontimer (nidevent );

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.