Learning OpenCV learning notes series (3) display pictures and videos, opencv learning notes

Source: Internet
Author: User

Learning OpenCV learning notes series (3) display pictures and videos, opencv learning notes

OpenCV is a computer vision library, so there are only two objects to process: "Images" and "videos" (in fact, videos are also extracted into single-frame images for processing. In general, or image processing ).

To learn OpenCV, you must first know how OpenCV opens the "image" and "video" files and then displays them.


To implement these functions, perform the following steps:


1. Create a project


Open VS2010, create a project, and select"Win32 console application"(Using the console can save a lot of trouble), named"HelloOpenCV", Click"OK"Button.


Next step


Remember to check"Empty Project", And then click"Complete"


After the project is created, the project has four folders: "external dependencies", "header files", "source files", and "resource files"





Now, the project is created, and the source code file is also created. The code is written below.


2. write code


Enter the following code in the "main. cpp" file:


// Created by JimmyGong20140903 // main. cpp: Implemented file // * The OpenCV header file contains */# include <stdio. h> # include <iostream> # include <opencv2/core. hpp> # include <opencv2/highgui. hpp> using namespace cv; using namespace std; // load the image and display void LoadImage_Display () {Mat myImage = imread ("airplane.jpg "); // load the specified image namedWindow ("Single Image Display"); // create a window named "Single Image Display" imshow ("Single Image Display", myImage ); // display the specified image waitKey (0) in the specified window;} // load the video and display void L OadVideo_Display () {Mat myFrame; CvCapture * myCap; namedWindow ("video playback", WINDOW_AUTOSIZE); // create a window named "video playback" myCap = cvCreateFileCapture ("Megamind. avi "); myFrame = cvQueryFrame (myCap); while (1) {myFrame = cvQueryFrame (myCap); if (myFrame. empty () // determines whether the frame is null {cout <"video file playback completed" <endl; break;} char chKeyCode = cvWaitKey (20 ); if (chKeyCode = 27) {break;} imshow ("video playback", myFrame) ;}cout <"press any key to exit! "<Endl; waitKey (0) ;}int main () {int whichTypeFile = 0; char cstr; bool isGoodInput = false; cout <"Enter the file type to be displayed! \ N input "0" as the image file, input "1" as the video file, other invalid! \ N "<endl; // cstr = cin. get (); while (! IsGoodInput) {cin> cstr; if (! Isdigit (cstr) {cout <"enter" 0 "or" 1 ". Thank you! "<Endl ;}else {cout <" \ n input successful! "<Endl; isGoodInput = true ;}// whichTypeFile = atoi (cstr); whichTypeFile = int (cstr-'0 '); // char --> intif (0 = whichTypeFile) {LoadImage_Display ();} else if (1 = whichTypeFile) {LoadVideo_Display ();} else {cout <"the input is invalid! \ N "<" Please set any key to exit! "<Endl; waitKey (0);} return 0 ;}



3. Add the resource file


After the encoding is completed, you must put the desired image and video files in the project directory. Otherwise, the program fails to read the specified image and video files.

In this example, the "airplane.jpg" and "Megamind. avi" files in OpenCV2.4.9 source code are copied to the project directory ("H: \ Practice \ HelloOpenCV.


4. debugging and Testing

When the code and resource files are correct, start to debug the program and compile the program smoothly. The running screen is as follows:




Now, the simplest function of Image Display and video playback has been implemented. Is it easy?


The following shows the source code project for this demonstration:

HelloOpenCV


OK. Come here first! Goodbye to next article!


Want to download some videos or images used for learning Opencv

Opencv Official Website
Source code of learning Opencv: www.oreilly.com/catalog/9780596516130
Opencv code management website sourceforge.net/projects/opencvlibrary/files/

Learning the second program in opencv-play an avi video. The video cannot be played.

If it cannot be displayed, the path or video format may be incorrect.

The video can be normally displayed, and an error will be displayed. After playing the video, the image in the video is obtained. The error is displayed and the loop is displayed.
 

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.