Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

How can I check if the monitor is connected in Windows / Linux?

How can I check if the monitor is connected in Windows / Linux?

The following program behavior is required, when I run my application:

  • if monitor is connected, then results will be showed in the window
  • if monitor isn't connected, or if I run application by SSH, then results will be saved to the image.jpg

Currently, if monitor isn't connected or if I run my application by using SSH, I get an error, that I can't catch as exception:

QXcbConnection: Could not connect to display

#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <iostream>
using namespace cv;
using namespace std;

int main( int argc, char** argv )
{
    if( argc != 2) return -1;
    Mat image = imread(argv[1], CV_LOAD_IMAGE_COLOR); 
    if(! image.data )  return -1;
    try {
        namedWindow( "Display window", WINDOW_AUTOSIZE );
        imshow( "Display window", image );
        waitKey(0);
    } catch(...) {
       imwrite( "output.jpg", image );
    }
    return 0;
}

Currently OpenCV application requires addition flag -monitor to enable/disable output to the window, that isn't very usable.

There is not required a full-fledged API, which would replace the OS API. But it would be very useful if there was a function that returns whether the monitor is connected or not.