C++ 如何仅使用 OpenCV HighGui 一键制作简单窗口?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33937800/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to make a simple window with one button using OpenCV HighGui only?
提问by Koci Ogon
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.
任何帮助深表感谢。
回答by Miki
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:
但是,这是一个显示(绿色)图像和顶部按钮的小示例:
Clicking the button will print "Clicked" on stdout:
单击该按钮将在标准输出上打印“Clicked”:
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;
}
回答by Henrik
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 模块的设计目的。
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.
或者,如果你知道你的底层窗口管理器,你就可以。即,如果您在 Windows 上,您可以获得窗口句柄,并动态添加更多控件。如果不是,您需要知道您在哪个平台上,以及如何在该平台上进行操作。
I wouldn't dare to try and put this in a simple answer
我不敢尝试把它放在一个简单的答案中
回答by Koci Ogon
@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 nalezy
Rect 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;
}