highGUI graphical user interface

1 #include <opencv2\core\core.hpp>
2 #include <opencv2\highgui\highgui.hpp>
3 using namespace cv;

Loading image display, output to a file

Image loading: imread () function

prototype:

1 Mat imread(const string& filename, int flag=1);

(1) The first argument, the path name of the picture loaded.

(2) loading the second parameter is identified, the value in the enumeration body opencv image format identifier, specify the type of a color image to be loaded. The default color image loaded three-channel.

. 1 CV_LOAD_IMAGE_UNCHANGED = - . 1 ; // new version obsolete 
2 CV_LOAD_IMAGE_GRAYSCALE = 0 ; // convert the image to grayscale and then return 
. 3 CV_LOAD_IMAGE_COLOR = . 1 ; // image is converted back to the color 
. 4 CV_LOAD_IMAGE_ANYDEPTH = 2 ; // Loading images 16-bit or 32-bit, it returns a corresponding depth image, or is converted into 8-bit image back 
. 5 CV_LOAD_IMAGE_ANYCOLOR = . 4 ; 
 . 6  
. 7 CV_LOAD_IMAGE_COLOR | CV_LOAD_IMAGE_ANYCOLOR; // . 1 | Loading three channel FIG. 4 
. 8 CV_LOAD_IMAGE_ANYDEPTH | CV_LOAD_IMAGE_ANYCOLOR; // 2 | 4 loading real source lossless image

flags is an int variable, the body no longer enumeration values, then:

  • flags> 0 returns a 3-channel color image
  • flags = 0 returns grayscale
  • flags <0 Apha return passage comprises loading image (transparent color)

The image display: imshow () function

void imshow(const string& winname, InputArray mat)

Window identifier name (1) a first parameter to be displayed

(2) The second parameter, the image to be displayed, as to the class Mat

imshow function is used to display the image in the specified window, if the window is CV_WINDOW_AUTOSIZE (default value) created, displayed images of the original size, otherwise the image will be scaled to fit the window.

Create a window: namedWindow () function

If the simple image display would not have this function, but when you need to display the name of the window before the window is used, for example, will develop the slider attached to a window, you need this function to create a window, the window name explicit provisions .

namedWindow function is designated by the name, image and create a progress bar can be used as a container window, if the window with the same name already exists, the function does nothing.

void namedWindow(const string& winname, int flags=WINDOW_AUTOSIZE);

Window Name (1) it is used as a window identifier

(2) window identifier:

. 1 WINDOW_NORMAL = 0 ; // may change the size of the window, CV_WINDOW_NORMAL 
2 WINDOW_AUTOSIZE = . 1 ; // window size will automatically adjust to the displayed image, the user can not manually change the window size, CV_WINDOW_AUTOSIZE 
. 3 WINDOW_OPENGL = 2 ; // Window Create when will support OpenGL, CV_WINDOW_OPENGL

Available through destroyWindow () or destroyAllWindow () function to close Chong district, and to cancel all the memory space associated with the window previously assigned.

Output image to a file: imwrite () function

imgwrite function is used to save the file to the specified image, the image format is based on a consistent image file extension of the extension, the extension that can be stored and can be read in imread.

bool imwrite(const string& filename, InputArray img, cost vector<int>& params=vector<int>() );

(1) the need to write the file name with a suffix;

(2) Mat types of image data

(3) const vector <int> & type params, indicated as a parameter encoding a particular storage format Default vector <int> (), generally do not fill out, if desired:

  • JPEG, this parameter indicates the picture quality (CV_IMWRITE_JPEG_QUALITY) 0 ~ 100, the default value is 95
  • PNG, this parameter indicates the level of compression (CV_IMWRITE_PNG_COMPRESSION), 0 ~ 9, the default value of 3. A higher value means a smaller size and longer compression time
  • PPM / PGM / PBM, this parameter represents a binary format flag (CV_IMWRITE_BINARY), value 0 or 1, default 1.

To create and use the slider

Create a slider: createTrackbar () function

Create a slider to adjust the value, and the slider attached to the specified window, and tend to use a callback function with up.

1 int creatTrackbar(const string& trackbarname, const string& winname, int* value, int count, TrackbarCallback onChange=0, void* userdata=0)

(1) const string & type of trackbarname, track bar's name

Name (2) cosnt string & type of winname, window

When (3) int * value type, pointer to an integer, represents the position of the slider, to create the initial position of the slider is the current value of the variable;

The maximum value of the position (4) int type COUT, the slider can reach the minimum position of the slider is always 0;

(5) TrackbarCallback type onChange, default value of 0. A pointer to the callback function, the callback function prototype must be void xxxx (int, void *), the first parameter is the position of the track bar, the second parameter is user data. If the callback is NULL pointer, indicating that no callback function is called, the only value changes.

(6) void * type serdata, default value of 0. User data passed to the callback function, processing track bar events. If the value argument is a global variable, do not control userdata.

Obtaining a current track position of the bar: getTrackbarPos () function

int getTrackbarPos(const string& trackbarname, const string& winname);

(1) const string & type of trackbarname, track bar's name

(2) const string & type winname, the name of the parent window track bar;

Mouse operation

 The message map similar to the slider way, it seems a callback function is fitted through an intermediary function. Specifies the mouse function callback function is SetMouseCallback:

1 void setMouseCallback(const string& winname, MouseCallback onMouse, void* userdata=0)

(1) const string & type winname, name of the window;

(2) MouseCallback onMouse type, when specified window of time of occurrence of each mouse, the function pointer is called.

The function prototype is void Foo (int event, int x, int y, int flags, void * param), wherein the event is the EVENT _ + (opencv2 in CV_ENENT_ +) one of the variables, x and y are the mouse pointer in the image coordinate system coordinate values ​​(not a window coordinate system) is, flags is a combination of EVENT_FLAG, param SetMouseCallbace parameter is passed to a user-defined function call.

EVENT_MOUSEMOVE the mouse movement message, EVENT_LBUTTONDOWN message press the left mouse button.

(3) void * UserData type, the transfer of user-defined parameters to the callback function, the default value of 0

 

Guess you like

Origin www.cnblogs.com/pacino12134/p/10979238.html