Opencv reads video and image sequences

Source: Internet
Author: User

When using opencv, we first obtain the image to be processed, this image may come from stored video files, real-time camera images, or a series of images in a folder. You may think that this is a piece of cake, because the first step of learning opencv is generally to learn how to load and display an image. For programs that open cameras or read videos, the Internet is also a hit. If you are a beginner, take a look at my summary below. You may not have noticed it.

Prepare this file, you will have a clearer structure of opencv. The following is only an exploration of reading and displaying images and videos.

1. Read Images

Which formats of images can be read by opencv? How to read?

In opencv, for reading a single image, we generally use the imread () function. first look at the following code:

 

#include <opencv2/core/core.hpp>#include <opencv2/highgui/highgui.hpp>#include <iostream>using namespace cv;using namespace std;int _tmain(int argc, _TCHAR* argv[]){Mat img;//img = imread("images//frame_0001.jpg");img = imread("images//frame_0001.jpg",0);if (img.empty()){cout<<"cannot load image"<<endl;return 0;}//namedWindow("image");imshow("image",img);waitKey(0);return 0;}

 

For codes highlighted in red and blue, try to see what is different? You will find that namedwindow ("image"); Are there all the same statements, while the second parameter of imread will display different images, one is the original image, and the other is the grayscale image. Why? Let's take a look at the imread () function interface:

C ++: mat imread (conststring & filename, int flags = 1)

Parameters

Filename-name of file tobe loaded.

Flags-flags specifyingthe color type of a loaded image:

-> 0 return a 3-channelcolor image.

Note: In the currentimplementation the alpha channel, if any, is stripped from

Output image. Use negativevalue if you need the alpha channel.

-= 0 return a grayscaleimage.

-<0 return the loadedimage as is (with alpha channel). </span>

 

You can see that filename stores the image name. If the image is not in this project file, you must provide the absolute path of the image. The flags parameter has three values. If the value is 0, the output is a grayscale image. If the value is greater than 0, the output value is a three-channel image. If you need an image with an alpha channel, you must use a negative value.
We know that there are many encapsulation formats for images, such as JPG, BMP, and PNG. But what can be read by opencv? In fact, there are differences in the types of images that can be read when using opencv in different operating systems. In the opencv2refman.pdf document, we will introduce the following:
The function imread loads an image from the specified file and returns it. If the image cannot be read (because
of missing file, improper permissions, unsupported or invalid format), the function returns an empty matrix
( Mat::data==NULL ). Currently, the following file formats are supported:
? Windows bitmaps - *.bmp, *.dib (always supported)
? JPEG files - *.jpeg, *.jpg, *.jpe (see the Notes section)
? JPEG 2000 files - *.jp2 (see the Notes section)
? Portable Network Graphics - *.png (see the Notes section)
? Portable image format - *.pbm, *.pgm, *.ppm (always supported)
? Sun rasters - *.sr, *.ras (always supported)
? TIFF files - *.tiff, *.tif (see the Notes section)
In addition, there are some differences when using opencv to read images in Windows and Linux, regardless of the format or opencv configuration, but these contents will be detailed in the relevant sections of opencv porting.
2. Read video
Let's take a look at a piece of code.
 
#include <opencv2/core/core.hpp>#include <opencv2/highgui/highgui.hpp>#include <iostream>using namespace cv;using namespace std;int _tmain(int argc, _TCHAR* argv[]){Mat img;VideoCapture cap(0);if (!cap.isOpened()){cout<<"cannot open camera"<<endl;return 0;}while (true){cap >> img;imshow("image",img);waitKey(30);}return 0;}


This is the most common method for reading videos, and the <claim to be the most convenient and quick method.
The above content can be seen in the development manual of opencv, but some are common in actual development, but not in the development manual. Let's take a look:
 
3. Read the image folder
The test data we downloaded from some databases, some of which are video files and some are images with one frame, for example, in this case:
 
How can I design a program so that the program can easily read these image files one by one? We may first exclude the videocapture class. We think it can only read video files or cameras. We usually choose to use the imread function, but we must replace the filename parameter when reading and removing an image. In fact, this is not difficult, we can try to store the file names of each image in a text file sequentially, and then extract the file names one by one from the text file. We can also convert numbers into strings and combine them with Frame _ and jpg to rent out a file name. I tried many methods when I encountered this problem for the first time, however, in the samples of opencv, I found that opencv can use the videocapture class to read image sequences. See the following code:
 
#include <opencv2/core/core.hpp>#include <opencv2/highgui/highgui.hpp>#include <iostream>using namespace cv;using namespace std;int main(int argc, char** argv){string first_file = "images//frame_%04d.jpg"; VideoCapture sequence(first_file);if (!sequence.isOpened()){cerr << "Failed to open the image sequence!\n" << endl;return 1;}Mat image;namedWindow("Image sequence", 1);for(;;){sequence >> image;if(image.empty()){cout << "End of Sequence" << endl;break;}imshow("Image sequence", image);waitKey(0);}return 0;}


Is it a good method! In fact, videocapture can do a lot of things, including reading streaming media files, network cameras, etc. This is not a problem for the moment! Will be covered in later sections!



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.