[Opencv]3 about Highgui

Source: Internet
Author: User

the Highgui (high-level graphical user interface) enables you to open windows, display images, read and write image files, and handle mouse, cursor, and keyboard events. The Highgui is mainly divided into "video input/Output", "image input/output" and "GUI Tools", which are implemented in cacap*, grfmt* and window* source files respectively.

<span style= "Font-family:microsoft yahei;font-size:18px;" >int Cvnamedwindow  (const char* name,    int flags=cv_window_autosize  );  </span>

<span style= "Font-family:microsoft yahei;font-size:18px;" >int Cvnamedwindow (const char* name,  int              flags=cv_window_autosize);</span>

The first entry is used to indicate the name of the new window, which is displayed at the top of the window and is used as a handle to other function call windows in Highgui. The second entry is a flag that indicates whether the window size needs to be automatically adapted to the size of the image being read in. Release can be implemented with Cvreleaseimage () and Cvdestroywindow ().

Loading images can be used with cvloadimage (), when an image is opened, the function does not parse the file extension, but determines the encoding format of the image by parsing the first few bytes of the image file. Note: When a cvloadimage () read fails, it does not produce a run-time error, but instead returns a null pointer. The function corresponding to Cvloadimage () is Cvsaveimage (), which implements the Save image function.

<span style= "Font-family:microsoft yahei;font-size:18px;" >iplimage* cvloadimage  (const char* filename,    int              iscolor=cv_load_image_color  );    int Cvsaveimage  (   const char*   filename,   const cvarr* image  );  </span>

<span style= "Font-family:microsoft yahei;font-size:18px;" >iplimage* cvloadimage (const char* filename,  int              iscolor=cv_load_image_color); int Cvsaveimage (  Const char*   filename,  const cvarr* image);</span>

The Cvarr in the code is the C language style, which functions like a base class in object-oriented languages. The same display image can be used with cvshowimage (). I prefer to read the code, so stick to a standard example here.

<span style= "Font-family:microsoft yahei;font-size:18px;" >int Main (int argc,char** argv)  {      //Create A named window with the name of the file.      Cvnamedwindow (argv[1],1]);        Load the image from the given file name.      iplimage* img=cvloadimage (argv[1]);        Show the image the user hits the "ESC" key.      while (1)      {          if (Cvwaitkey (+) ==27) break               ;       }       Clean up and Don's be piggies       Cvdestorywindow (argv[1]);       Cvreleaseimage (&img);  } </span>

 
<span style= "Font-family:microsoft yahei;font-size:18px;" >int Main (int argc,char** argv) {    //Create A named window with the name of the file.    Cvnamedwindow (argv[1],1]);    Load the image from the given file name.    iplimage* img=cvloadimage (argv[1]);    Show the image the user hits the "ESC" key.    while (1)    {        if (Cvwaitkey (+) ==27) break             ;     }     Clean up and Don's be piggies     Cvdestorywindow (argv[1]);     Cvreleaseimage (&img);} </span>

In Cvloadimage () of course you can also enter the Linux address directly.

<span style= "Font-family:microsoft yahei;font-size:18px;" >void Cvmovewindow  (const char* name,    int  x,    int  y  );  </span>

<span style= "Font-family:microsoft yahei;font-size:18px;" >void Cvmovewindow (const char* name,  int  x,  int  y);</span>

Cvmovewindow () Moves the window to the position of X, y in its upper-left corner. The following describes the functions that are important to you.

<span style= "Font-family:microsoft yahei;font-size:18px;" >while (1)  {      if (Cvwaitkey () ==27) break          ;  }  </span>

<span style= "Font-family:microsoft yahei;font-size:18px;" >while (1) {    if (Cvwaitkey () ==27) break        ;} </span>

In this program, it is to tell OpenCV to wait for the user to trigger event 100ms, if there is no user triggered in 100ms to continue the loop, if the user triggered and the key ASCII code is (ESC), exit the loop.

In mouse events, the biggest difference with the keyboard is, of course, that the mouse response event is handled in a way that returns the function. So in order to be able to respond to mouse click events, you must first create a fallback function. After you create this function, you need to register the function in OpenCV so that when a particular window is triggered, OPENCV can call the function correctly.

<span style= "Font-family:microsoft yahei;font-size:18px;" >void cvmousecallback (   int event,   int x,   int y,   int flags,   void* param);</span>
<span style= "Font-family:microsoft yahei;font-size:18px;" >void cvmousecallback  (     int event,     int x,     int y,     int flags,     void* param  );  </span>

The event must be a value in the following table. The second and third parameters are set to the X, Y coordinate value of the mouse position at the time the event occurred. Note: These coordinates represent the pixel coordinates of the image in the window and are not related to the size of the window. The fourth parameter flags, each of which specifies a different state when the event occurs. For example, the value of Cv_event_flags_shiftkey is 1 (the fifth bit of the flags), and if you want to know if shift is triggered, you can use flags with a bitmask (1<<4). The last parameter is a void pointer that can be used to pass additional parameter information in any structure.

Event name Numerical
Cv_event_mousemove 0
Cv_event_lbuttondown 1
Cv_event_rbuttondown 2
Cv_event_mbuttondown 3
Cv_event_lbuttonup 4
Cv_event_rbuttonup 5
Cv_event_mbuttonup 6
Cv_event_lbuttondblclk 7
Cv_event_rbuttondblclk 8
Cv_event_mbuttondblclk 9

The function that implements the registration is Cvsetmousecallback (), which requires 3 parameters. The first parameter specifies the window to which the function needs to be registered, the second parameter is the back function, and the last parameter is used to pass additional information to the previously mentioned void* param parameter.

<span style= "Font-family:microsoft yahei;font-size:18px;" >void cvsetmousecallback  (      const char* window_name,      cvmousecallback  on_mouse,      void* param= NULL  );  </span>

<span style= "Font-family:microsoft yahei;font-size:18px;" >void cvsetmousecallback (    const char* window_name,    cvmousecallback  on_mouse,    void* param=null) ;</span>

The following is a complete example.

<span style= "Font-family:microsoft yahei;font-size:18px;" > #include <cv.h> #include 

[Opencv]3 about Highgui

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.