Opencv entry level 2

Source: Internet
Author: User

In the previous two blog posts, we introduced the environment matching of opencv and the use of the most basic functions,

This blog post continues to use the demo to describe the opencv API,

Because some APIs are used a lot, you must be proficient,

You can download the necessary API documentation on the opencv official website to familiarize yourself with it,

In the previous section, I basically introduced the use of iplimage and cvcapture. What is the demo below,

In fact, it is similar to the previous one, but I only process the frames captured by the camera device,

In fact, this is not difficult. If you are interested, you can see it in the wait demo. For the processing of the camera device,

There is no big difference between processing common video files, but interest in this is amazing,

Only with a high degree of interest can you learn a technology well. Compared with video files,

Obviously, controlling photographic devices is even more exciting,

Therefore, the demo below uses a video recording device, which can simultaneously take photos and record videos.

First of all, I use vs2008 + opencv 2.0. At the same time, my computer does not have a camera because of camera equipment,

So I barely took a cell phone and used it as a camera, so the effect was very poor.

Photo Taking Function

Note the comments section. The unfamiliar APIs used includeCvpyrdown,Cvcreatecameracapture

AndCvcopyAndCvsaveimage

The following describes the specific use of these Apis:

Void cvpyrdown (const cvarr * SRC, cvarr * DST, int filter = cv_gaussian_5x5 );

SRC is of the cvarr * type. By the way, opencv is basically implemented using C. Of course, it includes certain C ++ components,

However, opencv still uses the object-oriented idea, which has the following inheritance relationships:

Therefore, in actual use, you can use the "subclass" cvmat or iplimage to replace cvarr,

That is to say, you can input an iplimage type in the parameter,

SRC represents the source image, that is, the input function image, while DST represents the output image, that is, the scaled image,

However, you must note that the DST image imported here must be passed in after initialization,

You can see this in the demo below. The default type is used for Convolution filters.

Cvcapture * cvcreatecameracapture (INT index );

Everyone should rememberCvcreatefilecaptureIs this function? This is used in my previous blog,

For more information, see:

Http://www.cnblogs.com/QinBaoBei/archive/2010/10/24/1859704.html

In fact,CvcreatefilecaptureIt is initialized based on a specified video file,

ForCvcreatecameracaptureThe difference is that it initializes a video device (or a camera ),

After initialization, you can get the video from this camera device.

For the index parameter, if multiple camera devices are connected to your computer, you must use this index to specify which camera to use.

Void cvcopy (const cvarr * SRC, cvarr * DST, const cvarr * mask = NULL );

This function is used to copy an array to another array. According to the previous inheritance relationship, iplimage inherits from cvmat,

Cvmat inherits from cvarr, so you can directly input an iplimage to copy it,

Obviously, SRC is the input array, while DST is the result array returned after the copy is complete,

As for the mask array, It is abnormal,

It specifies the elements in the array SRC that need to be copied to the DST array, and those elements cannot be copied to the DST array,

If the value is not 0 in the mask array, it can be copied to DST. If the value is 0, it will not be copied to the DST array.

Note that the DST array and SRC array must be of the same type and have the same dimension and size. Otherwise, the entireProgramWill crash.

Int cvsaveimage (const char * filename, const cvarr * image );

This function is very effective, that is, saving the image to the file. As for the file of the saved image, you can see what is the suffix specified by the filename parameter.

For the specific usage of the above APIs, see the demo below:

# Include <cv. h> # include  // Scale down the captured image by one time Iplimage * dopyrdown (iplimage * image, Int Filter = ipl_gaussian_5x5 ){ // Set to double the size Cvsize size = cvsize (image-> width/2, image-> height/2 ); // Initialize the image Iplimage * outimage = cvcreateimage (size, image-> depth, image-> nchannels ); // Implement Scaling Cvpyrdown (image, outimage); cvreleaseimage (& image ); Return Outimage ;} Int Main ( Int Argc, Char ** Argv) {cvnamedwindow (" Demo08 "); Cvcapture * capture; // Obtain a cvcapture object through the camera device.  If (Argc = 1) {capture = cvcreatecameracapture (0 );} Else {Capture = cvcreatecameracapture ( Atoi (Argv [1]);} assert (capture! = NULL); iplimage * frame; Char Keycode; // Capture the video image every 30 ms  While (Keycode = cvwaitkey (30 ))){ // Indicates that the ESC key is pressed.  If (Keycode = 27 ){ Break ;} // Indicates that the Enter key is pressed and the photo should be saved.  If (Keycode = 13 ){ // Initialize an image Iplimage * outimage = cvcreateimage (cvgetsize (FRAME), frame-> depth, frame-> nchannels ); // Copy the source Image Cvcopy (frame, outimage, null ); // Implement Scaling Outimage = dopyrdown (outimage ); Char * Outimagename =" Xiaozhen.jpg "; // Save the image Cvsaveimage (outimagename, outimage); cvreleaseimage (& outimage ); Printf (" Congratulations! The image is saved! \ N ");} // Obtain the next frame of the Video device. Frame = cvqueryframe (capture ); If (! Frame ){ Break ;} Cvshowimage (" Demo08 ", Frame) ;}cvreleaseimage (& frame); cvdestroyallwindows (); Return 0 ;}

Next let's take a look at the effect: (poor mobile phones, so the effect is not good)

When you press the Enter key, you can save the image.

Since I scaled the image from the camera, it looks worse.

Okay. This is the end of the first demo. Next we will introduce a demo,

This demo generates a video file from the content read by the camera.

Video Production

Like the first demo, we should start with API,

In the demo below, we mainly use the following special APIs, includingCvcreatevideowriterAndCvwriteframeSome other APIs have been touched in the previous blog.

Cvvideowriter * cvcreatevideowriter (const char * filename,

Int fourcc,

Double FPS,

Cvsize frame_size,

Int is_color = 1 );

This function is used to initialize a video file writer,

Needless to say, filename is the name of the video file to be created,

Fourcc indicates the encoding format of video compression,

Cv_fourcc ('P', 'I', 'M', '1') is the MPEG-1 codec,

Cv_fourcc ('M', 'J', 'P', and 'G') is motion-JPEG codec.

In Win32, if-1 is input, you can select the compression method and parameters from a dialog box.

FPS indicates the frame rate.

Is_color indicates whether to get a color frame or a gray frame.

Int cvwriteframe (cvvideowriter * Writer, const iplimage * image );

The role of this API is very obvious, that is, writing a frame to a video file. As for the parameters, I don't need to explain them much.

Here is the demo:

# Include  Int Main ( Int Argc, Char ** Argv) {cvnamedwindow (" Demo09 "); Cvcapture * capture = 0; If (Argc = 1) {capture = cvcreatecameracapture (0 );} Else {Capture = cvcreatecameracapture ( Atoi (Argv [1]);} If (! Capture ){ Return -1;} iplimage * frame; // Specify the size of each frame in the video (the image taken by my camera is 160*120) Cvsize size = cvsize (160,120 ); // You Need to initialize an object for writing a video file. The decoder format is mjpg.  // Set the frame rate to 5. Cvvideowriter * videowriter = cvcreatevideowriter (" Boyxiao. Avi ", Cv_fourcc ('M', 'J', 'P', 'G'), 5, size ); Char Keycode; // Retrieve a frame from the camera every 30 ms  While (Keycode = cvwaitkey (30 ))){If (Keycode = 27 ){ Break ;} // Get the frame obtained from the camera Frame = cvqueryframe (capture ); // Write frames into the video file Cvwriteframe (videowriter, frame); cvshowimage (" Demo09 ", Frame) ;}cvreleasevideowriter (& videowriter); cvreleaseimage (& frame); cvdestroywindow (" Demo09 "); Return 0 ;}

The following is the acceptance result:

The first thing is recording a video.

After the video is recorded, you can view the created video file in the project root directory.

Of course, video files can also be played.

This blog post is over now. Actually, this blog post has no technical content,

It mainly uses a camera to play with videos,

What I want to show here is a fun mentality, or interest,

I have always believed that as long as I am interested in something, it will not be too difficult to solve it,

Therefore, this blog post is purely based on the mentality of cultivating interest.

 

October 27, 2010

Related Article

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.