OpenCV do airplane Shooting games (i)

Source: Internet
Author: User

configuration of 1.OpenCV:
Requirements:

Win7 Win8 Win10 System.

The installation path for OPENCV is the default.

The computer is 64 bits.

1. Install Opencv1.0 and vc6.0++:

The installation path of the vc6.0++ can be changed, but the OPENCV installation path is the most default, do not change its installation path, otherwise it will be very troublesome.


2. Add Environment variables:

Right-click My Computer, properties, advanced system settings, environment variables, user variables, path add C:\Program Files (x86) \opencv\bin;


3. Enter vc6.0++ to configure:

Catalog, tools, Options

Select Include Files


Add the following:
C:\PROGRAM FILES (X86) \opencv\cxcore\include
C:\PROGRAM FILES (X86) \opencv\cvaux\include
C:\PROGRAM FILES (X86) \opencv\ml\include
C:\PROGRAM FILES (X86) \opencv\otherlibs\highgui
C:\PROGRAM FILES (X86) \opencv\otherlibs\cvcam\include
C:\PROGRAM FILES (X86) \opencv\cv\include

Select library files
Add the following:
C:\PROGRAM FILES (X86) \opencv\lib

Select source Files
Add the following:
C:\PROGRAM FILES (X86) \opencv\cv\src
C:\PROGRAM FILES (X86) \opencv\cvaux\src
C:\PROGRAM FILES (X86) \opencv\cxcore\src
C:\PROGRAM FILES (X86) \opencv\otherlibs\highgui
C:\PROGRAM FILES (X86) \opencv\otherlibs\cvcam\src\windows

after completion of the establishment of the project:

FILE--New project->win32 Console application then give the project a name, location.

Then select "One hello,world!" in the pop-up window. Program This creates a success.


to add a link library to a project:
Object/library module, connection, Project--

Copy and paste it in

Cxcore.lib cv.lib ml.lib cvaux.lib highgui.lib cvcam.lib

(Pay attention to the space, otherwise it will cause the letter to connect, otherwise it will be problematic).


Code test (can be copied directly):
#include "stdafx.h" #include <cv.h> #include 
Note: "Picture path to change."

(If the installation configuration succeeds, a window will pop up).

content:
1. Function Explanation:(1) Cvnamedwindow: Create Window

Function Prototypes:

int Cvnamedwindow (const char* name, int flags=cv_window_autosize);



Name:
The name of the window, which is used to distinguish between different windows and is displayed as a window caption.


Flags:
The Window property flags. The only flag currently supported is cv_window_autosize. When this flag is set, the user cannot manually change the window size and the window size is automatically adjusted to fit the displayed image (refer to cvshowimage).
The function Cvnamedwindow creates a window where the image can be placed. The windows that are created can be referenced by their names.
If the name already exists in the window, this function will not do anything.


(2) Cvloadimage: Reading an image from a file

Function Prototypes:

iplimage* cvloadimage (const char* filename, filename, int flags=cv_load_image_color)

The file name of the file to be read into.

Flags:
Specifies the color and depth of the read-in Image:);

FileName:
The file name of the file to be read into.

Flags:
Specifies the color and depth of the read-in image: The default is 1, which is the size of the picture by itself. int Flags=cv_load_image_color This sentence does not need to delve into, just remember the default parameter is 1, the image size itself can be.


(3) Cvshowimage: Display the image in the specified window

Function Prototypes:

void Cvshowimage (const char* name, const cvarr* image);

Name:

The name of the window.

Image
The image to be displayed.

The Cvshowimage function displays the image in the specified window. If the window is created with the flag cv_window_autosize, the image will be displayed in its original size, otherwise the image will be scaled to fit the window size.


(4) Cvwaitkey: Wait for key event

Function Prototypes:

int cvwaitkey (int delay=0);

Delay:
The number of milliseconds to delay.
The function Cvwaitkey an unrestricted wait-key event (delay<=0), or a delay of "delay" milliseconds. The return value is the value being pressed and returns 1 if the specified time is exceeded.

(5) Cvdestroywindow: destroying a window

Function Prototypes:

void Cvdestroywindow (const char* name);

Name:
The name of the window to be destroyed.
The function Cvdestroywindow destroys the window of the specified name.

(6): Get Pixel data:


Uchar R = Cv_image_elem (IMAGE, Uchar, y, nchannels * x);
Uchar G = Cv_image_elem (IMAGE, Uchar, y, nchannels * x + 1);
Ucahr B = Cv_image_elem (IMAGE, Uchar, y, nchannels * x + 2);


Cv_image_elem is a macro that gets the data at the specified position in the image. However, it is relatively silent, OPENCV does not provide a direct way to obtain RGB values, RGB three values in the memory of the location of the programmer need to calculate (the last parameter).
However, it is said that the OpenCV extension of the C + + interface provides the appropriate functionality.

Cv_image_elem is a macro,
#define Cv_image_elem (IMAGE, Elemtype, row, col) \
(((elemtype*) ((image)->imagedata + (image)->widthstep* (row))) [(COL)])

#define Cv_mat_elem (MAT, Elemtype, row, col) \
(* (elemtype*) cv_mat_elem_ptr_fast (MAT, Row, col, sizeof (elemtype)))

But to get the correct image coordinates for the pixel value of the (x, y) point, it should be written like this cv_image_elem (image, Uchar, y, x)
Note that the coordinates of the pixel point XY and the order of the row and column values are reversed (you can think carefully about why)
If the Write counter: Cv_image_elem (Image, Uchar, x, y), the result is sometimes caused by the access IMAGE out of bounds, resulting in an error.
But copy the image's data to the mat in the same way you would write it:
Cv_image_elem (Pimage, Uchar, I, j) = Cv_mat_elem (*mat, Uchar, I, J)
Don't change the order.
In short, when the macro is applied if the old error, try to change the order, or check whether the address is out of bounds.


Why multiply by 3, this involves Fourier image transformation.

What you want to know can be seen:

Http://wenku.baidu.com/link?url=hIQA_ vb0a4-jwdd7x6kt79nfqjusgnditjpexvcpi4r6essi33y8nhrdgufgcuoiq0y1wuljnozrc9warcz_7w2uogrv958bg6iqotvtic_& qq-pf-to=pcqq.c2c

2. Show another picture on one of the pictures:


#include "stdafx.h" #include <cv.h> #include 

Note: "Picture path to change."



issues to be aware of:

First, the display of another picture on the background map when the position is more than the size of the background map:

Cause the program to crash. For example: the size of the background map is 500x500, and if the non-IMG image appears on the background map as the position: 700x700, it will cause a crash. Because you're trying to find a place that you can't find at all.

Second, the size of the picture to be displayed on the background map is larger than the background image:

Cause the program to crash. Cause: Similar to the above.


3. Picture scrolling display:

Principle:

First copy the background map, then we can copy the image from bottom to top (or from top to bottom) a little bit of coverage of the original artwork if it is finished, you can do this from the beginning.

Photo Demo:












Then we can use very close to the image to blur the human eye senses, causing the image to scroll the illusion.

Scrolling code:

#include "stdafx.h" #include "cv.h" #include "highgui.h" #include "stdio.h" int main (int argc, char* argv[]) {iplimage *ba                Ckimage,*backimageclone;     The picture that will be scrolled is cloned a copy of Backimage = Cvloadimage (". \\timg.jpg");     Backimageclone = Cvcloneimage (backimage);                                int speed =-1;     The speed of the scroll int mpos = 0;     Cvnamedwindow ("123");                               while (true)//dynamic Display {MPOs + = speed;     As the program runs we calculate to refresh the displayed length if (mpos<0) {MPOs = mpos+backimageclone->height;          If the range is exceeded, modulo return} for (int i = 0;i<backimage->height;i++)//To display a portion of the clone graph in the background graph, causing the scroll illusion {          int ti = (mpos+i)%backimageclone->height;             for (int j= 0;j<backimage->width;j++) {int b = Cv_image_elem (backimageclone,uchar,ti,j*3);             int g = Cv_image_elem (backimageclone,uchar,ti,j*3+1); int r = Cv_image_elem (backimageclone,uchar,ti,j*3+2);             Cv_image_elem (backimage,uchar,i,j*3) = b;             Cv_image_elem (backimage,uchar,i,j*3+1) = g;  Cv_image_elem (backimage,uchar,i,j*3+2) = R;                             }} cvwaitkey (20); Updates display Cvshowimage ("123", backimage) every 20 milliseconds;     } printf ("Hello world!\n"); return 0;}

This will be OK.

Note: "Picture path to change."

OpenCV do airplane Shooting games (i)

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.