如何使用OpenCV HighGui只用一个按钮创建一个简单的窗口?

时间:2022-09-11 18:47:01

I am working on a game-project using OpenCV. Now I have to make a simple GUI: a window with one button, using HighGui only.

我正在使用OpenCV开发一个游戏项目。现在我必须创建一个简单的GUI:一个带有一个按钮的窗口,仅使用HighGui。

I'm not sure but I think I'm supposed to use something like this:

我不确定,但我认为我应该使用这样的东西:

cvNamedWindow( "NameWindow" , CV_WINDOW_AUTOSIZE);

Any help is much appreciated.

任何帮助深表感谢。

3 个解决方案

#1


12  

OpenCV does not provide a button, but you can easily use a colored rectangle, and check if the clicked point on the image is inside this rectangle.

OpenCV不提供按钮,但您可以轻松使用彩色矩形,并检查图像上的单击点是否在此矩形内。

Remember that OpenCV HighGui is very simple and is meant only for debugging purposes. You may want to use a full featured graphic library as Qt, or similar.

请记住,OpenCV HighGui非常简单,仅用于调试目的。您可能希望使用全功能图形库作为Qt或类似的。

However, this is a small example that shows a (green) image, and a button on top:

但是,这是一个显示(绿色)图像和顶部按钮的小示例:

如何使用OpenCV HighGui只用一个按钮创建一个简单的窗口?

Clicking the button will print "Clicked" on stdout:

单击该按钮将在stdout上打印“Clicked”:

如何使用OpenCV HighGui只用一个按钮创建一个简单的窗口?

Code:

#include <opencv2\opencv.hpp>#include <iostream>using namespace cv;using namespace std;Mat3b canvas;string buttonText("Click me!");string winName = "My cool GUI v0.1";Rect button;void callBackFunc(int event, int x, int y, int flags, void* userdata){    if (event == EVENT_LBUTTONDOWN)    {        if (button.contains(Point(x, y)))        {            cout << "Clicked!" << endl;            rectangle(canvas(button), button, Scalar(0,0,255), 2);        }    }    if (event == EVENT_LBUTTONUP)    {        rectangle(canvas, button, Scalar(200, 200, 200), 2);    }    imshow(winName, canvas);    waitKey(1);}int main() {    // An image    Mat3b img(300, 300, Vec3b(0, 255, 0));    // Your button    button = Rect(0,0,img.cols, 50);    // The canvas    canvas = Mat3b(img.rows + button.height, img.cols, Vec3b(0,0,0));    // Draw the button    canvas(button) = Vec3b(200,200,200);    putText(canvas(button), buttonText, Point(button.width*0.35, button.height*0.7), FONT_HERSHEY_PLAIN, 1, Scalar(0,0,0));    // Draw the image    img.copyTo(canvas(Rect(0, button.height, img.cols, img.rows)));    // Setup callback function    namedWindow(winName);    setMouseCallback(winName, callBackFunc);    imshow(winName, canvas);    waitKey();    return 0;}

#2


0  

you are aware that openCV is not a GUI library, but an image processing lib?

你知道openCV不是一个GUI库,而是一个图像处理库吗?

it ships with highgui: http://docs.opencv.org/2.4/modules/highgui/doc/highgui.html

它附带highgui:http://docs.opencv.org/2.4/modules/highgui/doc/highgui.html

for those cases where you really have no other options, but need to create a window for displaying stuff.

对于那些你真的没有其他选择,但需要创建一个显示内容的窗口的情况。

While OpenCV was designed for use in full-scale applications and can be used within functionally rich UI frameworks (such as Qt*, WinForms*, or Cocoa*) or without any UI at all, sometimes there it is required to try functionality quickly and visualize the results. This is what the HighGUI module has been designed for.

虽然OpenCV设计用于全面的应用程序,并且可以在功能丰富的UI框架(例如Qt *,WinForms *或Cocoa *)中使用,或者根本没有任何UI,但有时需要快速尝试功能。可视化结果。这就是HighGUI模块的设计目标。

see OpenCV and creating GUIs

请参阅OpenCV并创建GUI

edit: "this doesn't answer the question": -> more help..

编辑:“这不回答问题”: - >更多帮助..

you can't.

or that is, if you know your underlying window manager, you can.i.e. if you'r on windows, you could get the window handle, and dynamically add more controls..if not, you need to know what platform you'r on, and how to do it in that.

或者,如果你知道你的底层窗口管理器,你可以.i.e。如果你在Windows上,你可以获得窗口句柄,并动态添加更多控件..如果不是,你需要知道你在哪个平台,以及如何做到这一点。

I wouldn't dare to try and put this in a simple answer

我不敢尝试把它放在一个简单的答案中

#3


0  

@Miki, why I cannot use my buttons alternately? How to fix it? I mean I want to use them at the same time.

@Miki,为什么我不能交替使用我的按钮?怎么解决?我的意思是我想同时使用它们。

EDIT: I fixed it myself. No need of help. :)

编辑:我自己修好了。不需要帮助。 :)

#include <opencv2\opencv.hpp>#include <iostream>using namespace cv;using namespace std;Mat3b canvas;string buttonText("Nacisnij guzik!");string buttonText2("Nacisnij guzik NR2!");string winName = "PokerGui";int a = 0;//mozna pozniej usunac, potrzebne tylko czy button reaguje jak nalezyRect button, button2;void callBackFunc(int event, int x, int y, int flags, void* userdata){    if (event == EVENT_LBUTTONDOWN)    {        if (button.contains(Point(x, y)))//ponizej to co ma sie wykonac po nacisnieciu klawisza        {            a = a + 7;            cout << "Nacisnales guzik!\n" << endl;            printf("liczba = %i\n", a);            rectangle(canvas(button), button, Scalar(0, 0, 255), 2);        }        else if (button2.contains(Point(x, y)))//ponizej to co ma sie wykonac po nacisnieciu klawisza        {            //a = a + 7;            cout << "Nacisnales guzik NR2!\n" << endl;            //printf("liczba = %i\n", a);            rectangle(canvas(button2), button, Scalar(0, 0, 255), 2);        }    }    //if (event == EVENT_LBUTTONUP)    //{    //rectangle(canvas, button, Scalar(200, 200, 200), 2);    //}    imshow(winName, canvas);    waitKey(1);}void callBackFunc2(int event, int x, int y, int flags, void* userdata){    if (event == EVENT_LBUTTONDOWN)    {        if (button2.contains(Point(x, y)))//ponizej to co ma sie wykonac po nacisnieciu klawisza        {            //a = a + 7;            cout << "Nacisnales guzik NR2!\n" << endl;            //printf("liczba = %i\n", a);            rectangle(canvas(button2), button, Scalar(0, 0, 255), 2);        }    }    //if (event == EVENT_LBUTTONUP)    //{    //rectangle(canvas, button, Scalar(200, 200, 200), 2);    //}    imshow(winName, canvas);    waitKey(1);}int main(){    // An image    Mat3b img(300, 300, Vec3b(0, 255, 0));    // Your button    button = Rect(0, 0, img.cols, 50);    button2 = Rect(0, 60, img.cols, 50);    // The canvas    canvas = Mat3b(img.rows + button.height, img.cols, Vec3b(0, 0, 0));    // Draw the button    canvas(button) = Vec3b(200, 200, 200);    canvas(button2) = Vec3b(200, 200, 200);    putText(canvas(button), buttonText, Point(button.width*0.35, button.height*0.7), FONT_HERSHEY_PLAIN, 1, Scalar(0, 0, 0));    putText(canvas(button2), buttonText2, Point(button.width*0.25, button.height*0.7), FONT_HERSHEY_PLAIN, 1, Scalar(0, 0, 0));    // Draw the image    //img.copyTo(canvas(Rect(0, button.height, img.cols, img.rows)));    // Setup callback function    namedWindow(winName);    setMouseCallback(winName, callBackFunc);    //setMouseCallback(winName, callBackFunc2);    imshow(winName, canvas);    waitKey();    return 0;}

#1


12  

OpenCV does not provide a button, but you can easily use a colored rectangle, and check if the clicked point on the image is inside this rectangle.

OpenCV不提供按钮,但您可以轻松使用彩色矩形,并检查图像上的单击点是否在此矩形内。

Remember that OpenCV HighGui is very simple and is meant only for debugging purposes. You may want to use a full featured graphic library as Qt, or similar.

请记住,OpenCV HighGui非常简单,仅用于调试目的。您可能希望使用全功能图形库作为Qt或类似的。

However, this is a small example that shows a (green) image, and a button on top:

但是,这是一个显示(绿色)图像和顶部按钮的小示例:

如何使用OpenCV HighGui只用一个按钮创建一个简单的窗口?

Clicking the button will print "Clicked" on stdout:

单击该按钮将在stdout上打印“Clicked”:

如何使用OpenCV HighGui只用一个按钮创建一个简单的窗口?

Code:

#include <opencv2\opencv.hpp>#include <iostream>using namespace cv;using namespace std;Mat3b canvas;string buttonText("Click me!");string winName = "My cool GUI v0.1";Rect button;void callBackFunc(int event, int x, int y, int flags, void* userdata){    if (event == EVENT_LBUTTONDOWN)    {        if (button.contains(Point(x, y)))        {            cout << "Clicked!" << endl;            rectangle(canvas(button), button, Scalar(0,0,255), 2);        }    }    if (event == EVENT_LBUTTONUP)    {        rectangle(canvas, button, Scalar(200, 200, 200), 2);    }    imshow(winName, canvas);    waitKey(1);}int main() {    // An image    Mat3b img(300, 300, Vec3b(0, 255, 0));    // Your button    button = Rect(0,0,img.cols, 50);    // The canvas    canvas = Mat3b(img.rows + button.height, img.cols, Vec3b(0,0,0));    // Draw the button    canvas(button) = Vec3b(200,200,200);    putText(canvas(button), buttonText, Point(button.width*0.35, button.height*0.7), FONT_HERSHEY_PLAIN, 1, Scalar(0,0,0));    // Draw the image    img.copyTo(canvas(Rect(0, button.height, img.cols, img.rows)));    // Setup callback function    namedWindow(winName);    setMouseCallback(winName, callBackFunc);    imshow(winName, canvas);    waitKey();    return 0;}

#2


0  

you are aware that openCV is not a GUI library, but an image processing lib?

你知道openCV不是一个GUI库,而是一个图像处理库吗?

it ships with highgui: http://docs.opencv.org/2.4/modules/highgui/doc/highgui.html

它附带highgui:http://docs.opencv.org/2.4/modules/highgui/doc/highgui.html

for those cases where you really have no other options, but need to create a window for displaying stuff.

对于那些你真的没有其他选择,但需要创建一个显示内容的窗口的情况。

While OpenCV was designed for use in full-scale applications and can be used within functionally rich UI frameworks (such as Qt*, WinForms*, or Cocoa*) or without any UI at all, sometimes there it is required to try functionality quickly and visualize the results. This is what the HighGUI module has been designed for.

虽然OpenCV设计用于全面的应用程序,并且可以在功能丰富的UI框架(例如Qt *,WinForms *或Cocoa *)中使用,或者根本没有任何UI,但有时需要快速尝试功能。可视化结果。这就是HighGUI模块的设计目标。

see OpenCV and creating GUIs

请参阅OpenCV并创建GUI

edit: "this doesn't answer the question": -> more help..

编辑:“这不回答问题”: - >更多帮助..

you can't.

or that is, if you know your underlying window manager, you can.i.e. if you'r on windows, you could get the window handle, and dynamically add more controls..if not, you need to know what platform you'r on, and how to do it in that.

或者,如果你知道你的底层窗口管理器,你可以.i.e。如果你在Windows上,你可以获得窗口句柄,并动态添加更多控件..如果不是,你需要知道你在哪个平台,以及如何做到这一点。

I wouldn't dare to try and put this in a simple answer

我不敢尝试把它放在一个简单的答案中

#3


0  

@Miki, why I cannot use my buttons alternately? How to fix it? I mean I want to use them at the same time.

@Miki,为什么我不能交替使用我的按钮?怎么解决?我的意思是我想同时使用它们。

EDIT: I fixed it myself. No need of help. :)

编辑:我自己修好了。不需要帮助。 :)

#include <opencv2\opencv.hpp>#include <iostream>using namespace cv;using namespace std;Mat3b canvas;string buttonText("Nacisnij guzik!");string buttonText2("Nacisnij guzik NR2!");string winName = "PokerGui";int a = 0;//mozna pozniej usunac, potrzebne tylko czy button reaguje jak nalezyRect button, button2;void callBackFunc(int event, int x, int y, int flags, void* userdata){    if (event == EVENT_LBUTTONDOWN)    {        if (button.contains(Point(x, y)))//ponizej to co ma sie wykonac po nacisnieciu klawisza        {            a = a + 7;            cout << "Nacisnales guzik!\n" << endl;            printf("liczba = %i\n", a);            rectangle(canvas(button), button, Scalar(0, 0, 255), 2);        }        else if (button2.contains(Point(x, y)))//ponizej to co ma sie wykonac po nacisnieciu klawisza        {            //a = a + 7;            cout << "Nacisnales guzik NR2!\n" << endl;            //printf("liczba = %i\n", a);            rectangle(canvas(button2), button, Scalar(0, 0, 255), 2);        }    }    //if (event == EVENT_LBUTTONUP)    //{    //rectangle(canvas, button, Scalar(200, 200, 200), 2);    //}    imshow(winName, canvas);    waitKey(1);}void callBackFunc2(int event, int x, int y, int flags, void* userdata){    if (event == EVENT_LBUTTONDOWN)    {        if (button2.contains(Point(x, y)))//ponizej to co ma sie wykonac po nacisnieciu klawisza        {            //a = a + 7;            cout << "Nacisnales guzik NR2!\n" << endl;            //printf("liczba = %i\n", a);            rectangle(canvas(button2), button, Scalar(0, 0, 255), 2);        }    }    //if (event == EVENT_LBUTTONUP)    //{    //rectangle(canvas, button, Scalar(200, 200, 200), 2);    //}    imshow(winName, canvas);    waitKey(1);}int main(){    // An image    Mat3b img(300, 300, Vec3b(0, 255, 0));    // Your button    button = Rect(0, 0, img.cols, 50);    button2 = Rect(0, 60, img.cols, 50);    // The canvas    canvas = Mat3b(img.rows + button.height, img.cols, Vec3b(0, 0, 0));    // Draw the button    canvas(button) = Vec3b(200, 200, 200);    canvas(button2) = Vec3b(200, 200, 200);    putText(canvas(button), buttonText, Point(button.width*0.35, button.height*0.7), FONT_HERSHEY_PLAIN, 1, Scalar(0, 0, 0));    putText(canvas(button2), buttonText2, Point(button.width*0.25, button.height*0.7), FONT_HERSHEY_PLAIN, 1, Scalar(0, 0, 0));    // Draw the image    //img.copyTo(canvas(Rect(0, button.height, img.cols, img.rows)));    // Setup callback function    namedWindow(winName);    setMouseCallback(winName, callBackFunc);    //setMouseCallback(winName, callBackFunc2);    imshow(winName, canvas);    waitKey();    return 0;}