[OpenCV] video reading and some simple image processing, opencv reading

Source: Internet
Author: User

[OpenCV] video reading and some simple image processing, opencv reading
1. Preface

As a computer image processing tool, openCV began to show some irreplaceable advantages due to free disclosure, copyright, and so on (it has been shown for a long time ). Well, I have been working with MATLAB in my graduate school. I always feel like debugging, verification, or MATLAB is useful! Not long ago, I started to get in touch with openCV. I always felt that although it was a tool, the water was very deep. Well, it's always a pitfall about openCV. Record your learning trajectory.
Self-built slot 1: There are some pitfalls in the basic image processing, and some of them are not completed. After a few days, start to fill in the pitfalls, not too bad (= !! Self-hypnosis)
Self-built tucao 2: but no one is really waiting to see it (= Good sang Xin !)

2. Video reading and Image Display 2.1 read images from AVI Files

For image processing, the initial experiment code basically starts by reading an image and displaying it. This is the "Hello World!" of image processing !". OpenCV reads an image and displays the following code.

IplImage* ori= cvLoadImage("..\\Data\\Fig0320(4) (bottom_left).tif",Gray_image);    cvNamedWindow("Original", CV_WINDOW_AUTOSIZE );   //new windowcvShowImage("Original",ori);        //show

In fact, it is equivalent toimread(),imshow()Andfigure()It is worth noting that if you want to expand the image in the window along with the window, you can use the CV_WINDOW_NORMAL style, that is, the following code.

cvNamedWindow("Original", CV_WINDOW_NORMAL );   //new windowcvShowImage("Original",ori);        //show

The video reading function is similar to the image reading function.
cvCreateFileCapture(argv[1])Read a video. Then use the FunctioncvQueryFrame(capture)Take one frame of the image, and then display the image (cvShowImage()). It is displayed frame by frame at a certain interval.

The basic idea is as above. Here, according to the practice in the teaching material, a slider control is added as the progress display. The code implementation is as follows (the code is basically the same as the teaching material =)

// -------------------------------------------------------- // Content: // The video "Jiangyun TV" Reads/plays the video screen // adds the slider control /// 2015.5.18 // by zhou fan // ##include "stdafx. h "# include <iostream> # include <opencv2/core. hpp> # include <opencv2/highgui. hpp> using namespace cv; using namespace std; int g_slider_position = 0; CvCapture * capture = NULL; void onTrackbarSi Lde (int pos) {cvSetCaptureProperty (capture, CV_CAP_PROP_POS_FRAMES, // parts, pos);} int main (int argc, char ** argv []) {cvNamedWindow ("ENoDenn ", CV_WINDOW_AUTOSIZE); capture = cvCreateFileCapture (".. \ Data \ IMG_2023.AVI "); // The total number of frames read from the video. int frames = (int) cvGetCaptureProperty (capture, CV_CAP_PROP_FRAME_COUNT); if (frames! = 0) // if the video is valid, add the slider control {cvCreateTrackbar ("Position", "ENoDenn", & g_slider_position, frames, onTrackbarSilde);} IplImage * frame; while (1) {frame = cvQueryFrame (capture); // read if (! Frame) break; cvSetTrackbarPos ("Position", // update the Position of the slider "ENoDenn", ++ g_slider_position); cvShowImage ("ENoDenn", frame ); char c = cvWaitKey (33); // wait for 33 ms. The number of frames is 30 if (c = 27) break per second;} cvReleaseCapture (& capture); cvDestroyWindow ("ENoDenn "); return (int) 0 ;}

Shows the execution result.

It is worth noting that the function

cvCreateTrackbar("Position",                  "ENoDenn",                 &g_slider_position,                 frames,                 onTrackbarSilde                 );

The last parameter isonTrackbarSildeThis function can be used to set (or specify) a frame. For example, if you drag the slider to the position of 115 frames, this function enables the video to start playing from 115 frames. If you change this field to NULL, you can drag the slider to 115 frames, which only updates the slider from 115 and does not play the video from the position of 115 frames.

Use FunctionscvSetTrackbarPos()You can set the slider position. Use FunctionscvGetTrackbarPos()You can Get to the current position of the slider.

Self-built tucao 3: the video was shot last month at the signal light in front of the warehouse college, that is, the signal light of the slam dunk OP.

2.1 read images from Camera

Reading a video file from a camera is actually quite simple. Function is requiredCvCapture* cvCreateCameraCapture(int index)Specify the Camera. Generally, CV_CAP_ANY is used to read the image from any location. If you have multiple cameras, you can change the index to specify the desired camera. If the camera is not picked or an Error occurs, a NULL pointer is returned.
The image reading and display are the same as the above Code.cvQueryFrame(capture)Extract one frame of one frame, and then display the image.cvShowImage()And display it frame by frame at a certain interval.

int main(int argc, char** argv[]){    cvNamedWindow("Camera" , CV_WINDOW_AUTOSIZE );    CvCapture* capture = cvCreateCameraCapture(CV_CAP_ANY);    assert(capture != NULL);    IplImage *frame = 0;    frame = cvQueryFrame(capture);    while(1)    {        frame = cvQueryFrame(capture);        if(!frame) break;        cvShowImage("Camera",frame);        char c = cvWaitKey(33);        if(c == 27)  break;    }       return (int)0;}

The running result will not be pasted, and the camera will be turned on.

3. Basic operations on some images

After the image and video can be read, you can start some basic image processing. For example, image smoothing (low-pass filtering), image downsampling and downsampling (downsampling and upsampling), and image edge detection.

3.1 create an Arbitrary Image

When using MATKLAB to process images, a large image, such as an object image, is usually created. Initialize it to the same format, and assign all values to 0. In openCV, use
IplImage* cvCreateImage( CvSize size, int depth, int channels )
Create an image. This function has three parameters: Size, format, and number of channels. For example, if you create a large image with an object image (grayscale image) and assign it to 0, you can perform the following operations.

IplImage* image_edge = cvCreateImage(cvGetSize(image_input),                                     IPL_DEPTH_8U,                                     1);cvZero(image_edge);                 

Used herecvGetSize()To obtain the image size. If you want to set a specific size, you can handle it as follows.

IplImage* image_edge = cvCreateImage(cvSize(200,200),                                     IPL_DEPTH_8U,                                     1);cvZero(image_edge);                 

If the above processing is done, we can get a X Full 0 image.

The second parameter IPL_DEPTH_8U is the image format. The format is as follows.

IPL_DEPTH_8U-Unsigned 8-bit integer
IPL_DEPTH_8S-signed 8-bit integer
IPL_DEPTH_16U-Unsigned 16bit integer
IPL_DEPTH_16S-Signed 16bit integer
IPL_DEPTH_32S-Signed 32bit integer
IPL_DEPTH_32F-single precision floating point type
IPL_DEPTH_64F-Dual-precision floating point

The third parameter is the number of channels. For example, if you use an RGB image, you can use three channels to store the image.

3.2 Image Smoothing

Image Smoothing is equivalent to low-pass filtering, which is quite clear in my previous post on Image Processing BASICS (self-perception ).
[Digital Image Processing] Spatial Filtering
[Digital Image Processing] frequency domain filtering (1)-basis and low-pass filter
In openCV, you can usecvSmooth()Smooth an image. For example, the following code.

IplImage* Image_smooth(IplImage* image_input){    IplImage* image_output = cvCreateImage(cvGetSize(image_input),                                           IPL_DEPTH_8U,                                           1);    cvSmooth(image_input,image_output,CV_GAUSSIAN,13,13);    return(image_output);}

Here, the 13X13 Gaussian low-pass filter is used by default. The running result is as follows.

We can see that the image on the right is more blurred than that on the left. In fact, the essence is that the high-frequency component is filtered out. Well, it is really necessary to paste the Fourier spectrum of two images here, but this is a blog post, and we will talk about the computation and reality of Fourier spectrum later. (Please try again !!!)
[OpenCV] Fourier spectrum of images

3.2 reduction of images

For the principle of image shrinking, refer to my previous blog post, which also describes the problems and solutions of the zoom-in and zoom-in operations.
[Digital Image Processing] digital image integer doubling (digital image interpolation)
[Digital Image Processing] digital image is reduced by an integer multiple.
[Digital Image Processing] Digital Image Scaling (zoom in and zoom in)
In openCV, you can usecvPyrDown()AndcvPyrUp()To zoom out or zoom in a pair of images.

IplImage * doPyrDown (IplImage * image_input) {assert (image_input-> width % 2 = 0 & image_input-> height % 2 = 0 ); // error judge IplImage * image_output = cvCreateImage (cvSize (image_input-> width/2, image_input-> height/2), image_input-> depth, image_input-> nChannels ); cvPyrDown (image_input, image_output); return (image_output );}

The running result is as follows.

The length and width of the image are reduced by half, and the area is changed to 0.25 times. Well,cvPyrUp()Similarly, it is to insert 0 between adjacent signals, and then use the low-pass filter in the full graph. What is the result,cvPyrDown()AndcvPyrUp()Only two multiples can be expanded and reduced. In fact, I want to say that if we can achieve expansion and reduction of other multiples, we can achieve M/N times scaling by expanding M times and then narrowing down N times. For more information, see my previous blog posts ).

3.3 image edge detection

This means that you are familiar with the well-intentioned edge detection operator and paste a piece of code. In the principle section, find a time to fill up the blog and discuss it again (dig another trap, + _ +, it seems that you cannot fill it out ).

IplImage* doCanny(IplImage* image_input,                  double lowThresh,                  double highThresh,                  double aperture){    if(image_input->nChannels != 1)        return (0);    IplImage* image_output = cvCreateImage(cvGetSize(image_input),                                           image_input->depth,                                           image_input->nChannels);    cvCanny(image_input,image_output,lowThresh,highThresh,aperture);    return(image_output);}

The running result is as follows.

3.4 Region Of Interest

That is to say, you can set a rectangular area of the image as the ROI Area and then operate on the configured area. Set the function
void cvSetImageROI(IplImage* image, CvRect rect)
CvRect is the constructor of the rectangular area in an image. It has four members: int x, y, width, and height. It represents the coordinates of the starting point (x, y) and its dimensions (length and width), width and height. Of course, you can also set the ROI area to another shape.

You can use the following function to remove the preset ROI region.
void cvResetImageROI(IplImage* image)
These two functions are meaningful in practice and can be used for local operations on images to speed up the operation.

As a contact for an ROI region, paste a code.

IplImage * RIODomainAdd (IplImage * image_input, CvRect RIO_Domain) {if (image_input-> nChannels! = 1) return (0); IplImage * image_edge = cvCreateImage (cvGetSize (image_input), IPL_DEPTH_8U, 1); image_edge = Dokan (image_inputs, 10,100, 3); cvSetImageROI (image_input, RIO_Domain); transform (image_edge, RIO_Domain); // cvAddS (image_input, cvScalar (100), image_input); cvAddWeighted (image_input, 0.8, image_edge, 0.8, 0, image_input ); // image_input = image_input * 0.2 + image_edge * 0.8; (ROI region verification) cvResetImageROI (image_input); return (image_input );}

The running result is as follows.

4. Statement

This is the first article of openCV. Although there is a relationship between wood, I still want to say that Markdown is so useful !!! Next, I 'd like to try using Latex to write a mathematical formula. (En? Is it true that all previous blog posts are updated ).
Next, we will update a histogram processing program with Fourier spectrum. It is expected that the event will be completed within this week.

Published in blog: http://blog.csdn.net/thnh169/

============== Update log ===================================

2015-5-19 The first version is complete
2015-5-21 added the code for reading videos from the camera

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.