How to make a simple window with one button only using OpenCV HighGui?

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

I'm not sure, but I think I should use something like this:

cvNamedWindow( "NameWindow" , CV_WINDOW_AUTOSIZE);

Any help is greatly appreciated.

+4
source share
3 answers

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

, OpenCV HighGui . Qt .

, () :

enter image description here

"" stdout:

enter image description here

:

#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;
}
+11

, openCV , ?

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

, , .

OpenCV ( Qt *, WinForms * Cocoa *) - , . , HighGUI.

. OpenCV

edit: " ": → ..

.

, , . .. , . , , , .

0

@Miki, why can't I use my buttons interchangeably? How to fix it? I mean, I want to use them at the same time.

EDIT: I fixed it myself. No need for 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;
}
0
source

Source: https://habr.com/ru/post/1617602/


All Articles