Getting screen size on OpenCV

后端 未结 6 2002
独厮守ぢ
独厮守ぢ 2021-02-19 09:04

How do I get the computer screen resolution on OpenCV? I need to show two images side by side using the whole screen width, OpenCV requires the exact window size I wanna create.

相关标签:
6条回答
  • 2021-02-19 09:16

    Using opencv instead of OS functions:

    cv.namedWindow("dst", cv.WND_PROP_FULLSCREEN)
    cv.setWindowProperty("dst",cv.WND_PROP_FULLSCREEN,cv.WINDOW_FULLSCREEN)
    (a,b,screenWidth,screenHeight) = cv.getWindowImageRect('dst')
    

    So I create a window in full-screen mode and ask for its width and height

    It should work for any OS, but it worked only for Windows. Under Linux, the window wasn't full-screen.

    0 讨论(0)
  • 2021-02-19 09:20

    In new version it also works like below, which is great :-D

    printf("%f",cap.get(CV_CAP_PROP_FRAME_HEIGHT));
    

    for setting video res.

    <capture_handle>.set(property_in_caps, value_in_double);
    
    ex, cap.set(CV_CAP_PROP_FRAME_HEIGHT, 720);
    
    0 讨论(0)
  • 2021-02-19 09:25

    You can't it's an OS function - it depends on your operating system

    0 讨论(0)
  • 2021-02-19 09:28

    In Linux, try this

    #include <stdio.h>
    int main()
    {
     char *command="xrandr | grep '*'";
     FILE *fpipe = (FILE*)popen(command,"r");
     char line[256];
     while ( fgets( line, sizeof(line), fpipe))
     {
      printf("%s", line);
     }
     pclose(fpipe);
     return 0;
    }
    

    In Windows,

    http://cppkid.wordpress.com/2009/01/07/how-to-get-the-screen-resolution-in-pixels/

    0 讨论(0)
  • 2021-02-19 09:37

    You can use this solution cross-platform solution with or without opencv:

    #if WIN32
      #include <windows.h>
    #else
      #include <X11/Xlib.h>
    #endif
    
    //...
    
    void getScreenResolution(int &width, int &height) {
    #if WIN32
        width  = (int) GetSystemMetrics(SM_CXSCREEN);
        height = (int) GetSystemMetrics(SM_CYSCREEN);
    #else
        Display* disp = XOpenDisplay(NULL);
        Screen*  scrn = DefaultScreenOfDisplay(disp);
        width  = scrn->width;
        height = scrn->height;
    #endif
    }
    
    int main() {
        int width, height;
        getScreenResolution(width, height);
        printf("Screen resolution: %dx%d\n", width, height);
    }
    
    0 讨论(0)
  • 2021-02-19 09:38

    This is not an answer to how to get the screen width and height, but it will let you solve your problem of displaying two images using the full screen size.

    1) Create a window like this:

    namedWindow("hello", WINDOW_NORMAL);
    

    This will allow you to set the window size yourself, you can set easily maximize it and it will take the whole screen. It will also remember the settings the next time you run the program. You can also set flags to keep the aspect ratio if you desire.

    2) If you need to get the exact number of pixels, you can get the window size using this:

    getWindowImageRect("hello");
    

    That way, you can easily combine the two images into one and display the result on screen. I'm doing it myself.

    0 讨论(0)
提交回复
热议问题