Getting screen size on OpenCV

14,374

Solution 1

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);
}

Solution 2

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/

Solution 3

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.

Solution 4

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.

Share:
14,374

Related videos on Youtube

Lucas C. Feijo
Author by

Lucas C. Feijo

Updated on September 15, 2022

Comments

  • Lucas C. Feijo
    Lucas C. Feijo over 1 year

    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.

  • Lucas C. Feijo
    Lucas C. Feijo almost 12 years
    It returns "Xlib: extension "RANDR" missing on display "/tmp/launch-toASus/org.x:0". RandR extension missing", any ideas? :(
  • Tal
    Tal over 3 years
    It not opencv solution
  • Derzu
    Derzu over 3 years
    @Tal, you are right, and I said it in the first line of the answer.
  • Tal
    Tal over 3 years
    I like the Octo answer because it open cv solution and it working.