4
votes

I use namedWindow with openCV and get an error: (Image1): Gtk-WARNING **: cannot open display:"

The code is:

Mat image1 = imread("image1.jpg");

cv::namedWindow("image1");
cv::imshow("image1",image1);

waitKey(0);

Someone says he solves it by: "export DISPLAY=:0". But I don't really understand what does this solution mean.Since I am a beginner in opencv and c++. Could someone explain it in detail. e.g. How to add the code? Where to implement?

1
What is your source for the solution you mention ? - BConic
I don't know.It's seems to be a root command, as I know from the internet. - user3384116
export sets an environmental variable and has to be issued in the same session/terminal, so that the environment is correctly altered. Calling it outside the terminal, or as root, has no effect. - ypnos
I have solved it by creating a new environment variable DISPLAY to :0.0 in Run configuration. Thanks a lot! - user3384116

1 Answers

4
votes

You seem to be lacking the context of your X server.

The easiest way to make this work is to login graphically, then open a terminal, then start your application. This means, not opening a session for another user, e.g. root.

If you want to run the program as another user or from remote, you need other solutions. Please specify what you are doing. It is a common problem (it is about authorization) with common solutions depending on your use-case! It is not a OpenCV-related problem.