Git Product home page Git Product logo

Comments (9)

pthom avatar pthom commented on June 10, 2024 1

I agree, your approach is simpler and will not require the developer to create variables in order to hold these values.

from cvui.

Dovyski avatar Dovyski commented on June 10, 2024

Hi! I like the idea of a mouse API, but I am not sure I would implement it the way you did. Everything in cvui is designed to avoid the idea of asynchronous calls, which is not the case here unfortunately. In my head, a mouse API would be something like:

// true while the mouse button is down
if(cvui::mouse(cvui::IS_PRESSED)) { 
}

// same behavior as cvui::button(): returns true once
// (when the mouse button was clicked), then returns
// false for all subsequent calls. 
if(cvui::mouse(cvui::JUST_PRESSED)) { 
}

In that way there is room for future addition, e.g. cvui::LEFT_CLICK, cvui::MIDDLE_CLICK, etc.

Regarding cvui::imgshow() and the inclusion of cv::namedWindow() into cvui::init(), I think it moves the lib towards being an abstraction of OpenCV, which is not the idea. cvui was designed to live on top of OpenCV to allow maximum customization and freedom, so developers don't have to learn the "cvui way" of doing things. They can do as they have been doing, plus some cvui calls :)

from cvui.

pthom avatar pthom commented on June 10, 2024

Hi !
Thanks for your answer.

Concerning the mouse, your idea of having method like cvui::mouse(cvui::IS_PRESSED) is good !
However it is not incompatible with having cvui::setMouseCallback : both can coexist.

Below is an example of the kind of program I very commonly write when doing research with opencv. If your intent if to give maximum customization and freedom to developers, then it should be easy to add cvui to this kind of program.

Typical program 1 : using mouse interactions

This becomes impossible with cvui .

void myMouseHandler( int event, int x, int y, int flags, void * data ) {
    // Do stuff with the mouse : it is possible to handle 
    // - mouseButtonDown / mouseButtonDown for left/right/middle buttons
    // - mouseMove (if the mouse button is down *or not*)
    // - flags : Alt key pressed, Shift Key Pressed, Ctrl Key pressed, or a combination
}

void TypicalProgram1() {
    cv::Mat image;
    cv::namedWindow("image");
    cv::setMouseCallback("image", myMouseHandler, 0);
    while(true)
    {
        cv::imshow("image", image);
        if (cv::waitKey(50) == 'q')
            break;
    }
}

Many programs are written like this, and it should be possible to port them to cvui effortlessly, don't you think ? This is why I proposed cvui::setMouseCallback. It has almost exactly the same API as opencv, with an additional improvement over the opencv api : it accept raw function pointers + lambda and std::function.

For the moment, this kind of program is incompatible with cvui, because cvui will register the mouse callback (and opencv accepts only one mouse callback...)

Typical program 2 : using only keyboard interactions

In this case, I ony use keyboard interactions. I do not need to call cv::namedWindow (which becomes mandatory with cvui).
But I understand your concern, and it is not very important after all.

void TypicalProgram2() {
    cv::Mat image;
    // No cv::namedWindowd is required
    while(true)
    {
        cv::imshow("image", image);
        char c = cv::waitKey(50);
        if (c == 'q') break;
        if (c == 's') saveStuff();
        // etc
    }
}

from cvui.

Dovyski avatar Dovyski commented on June 10, 2024

Ok, I see your point. The addition of mouse callbacks is definitely an easy way for developers to use cvui. I am still reluctant though. In your example, despite the fact that cvui uses the only OpenCV mouse callback available, you can still work that around:

void myMouseHandler( int event, int x, int y, int flags, void * data ) {
    cvui::handleMouse(event, x, y, flags, data);
   // your usual code here
}

void TypicalProgram1() {
    cv::Mat image;
    cv::namedWindow("image");
    cvui::init("image");
    cv::setMouseCallback("image", myMouseHandler, 0);
    while(true)
    {
        cvui::update();
        cv::imshow("image", image);
        if (cv::waitKey(50) == 'q')
            break;
    }
}

from cvui.

pthom avatar pthom commented on June 10, 2024

Hi !

As I actually need to be able to use mouse interactions, I tried to write a draft of an API that follows your idea.

Does the API below reflect your point of view?

namespace cvui
{
    enum MousePressedStatus
    {
        JustPressed,
        IsPressed,
        JustReleased
    };

    enum MouseButton
    {
        LeftButton,
        MiddleButton,
        RightButton
    };

    // cvui::mouse
    // if pt is not null, it will be filled with the mouse location
    // if flag is not null, it will be filled with the mouse callback flags
    // if data is not null, it will be filled with the mouse callback *data content
    // Note : mouseLocation, flag and data will always be filled (if not null)
    //        even if the mouse status is not the one that was queried.
    bool mouse(MousePressedStatus status,
               cv::Point * mouseLocation = nullptr,
               MouseButton button = MouseButton::LeftButton,
               int * flags = nullptr,
               void **data = nullptr);
    // conveniency methods, if someone need to quickly access right or middle mouse button status
    bool mouseLeft(MousePressedStatus status, 
                   cv::Point * pt = nullptr, 
                   int * flags = nullptr, 
                   void **data = nullptr);
    bool mouseRight(MousePressedStatus status, 
                    cv::Point * pt = nullptr, 
                    int * flags = nullptr, 
                    void **data = nullptr);
    bool mouseMiddle(MousePressedStatus status, 
                     cv::Point * pt = nullptr, 
                     int * flags = nullptr, 
                     void **data = nullptr);

    // returns true if mouse was wheeled (mouse-wheel motion delta, `
    // from cv::getMouseWheelDelta
    // Note : I never used this, and the opencv docs mentions that it works only under windows. So, it might also be ignored
    bool mouseWheel(int *mouseWheelDelta = nullptr, 
                    cv::Point *pt = nullptr, 
                    int * flags = nullptr, 
                    void **data = nullptr);
    // returns non zero value if mouse was wheeled horizontally
    bool mouseHWheel(int *mouseWheelDelta = nullptr, 
                     cv::Point *pt = nullptr, 
                     int * flags = nullptr, 
                     void **data = nullptr);
}

from cvui.

Dovyski avatar Dovyski commented on June 10, 2024

It looks good, thanks for the contribution. I am a bit confused about some parameters though. Why are the cv::Point *pt = nullptr, int * flags = nullptr, void **data = nullptr parameters needed? I think such information should be handled internally by cvui leaving developers with simpler API.

The mouse position, for instance, could be provided as a property within the cvui namespace:

if(cvui::mouse(cvui::JUST_PRESSED)) {
   std::cout << cvui::mouse.x << cvui::mouse.y << std::endl;
}

What do you think?

from cvui.

pthom avatar pthom commented on June 10, 2024

Hi Fernando,

I looked at your recent modifications, and it is very interesting. Good work :-)
I will post a merge request that adds the possibility to query the mouse location.
Look at it and tell me your thoughts !

Cheers

from cvui.

Dovyski avatar Dovyski commented on June 10, 2024

Hey Pascal! Thanks! :) I saw your PR, it looks good. I am a bit overwhelmed by work the next days, so it might take a bit for me to comment and merge your contributions.

from cvui.

Dovyski avatar Dovyski commented on June 10, 2024

I added several of the ideas discussed in this issue into cvui, including the improvements in cvui::init() and the addition of cvui::imshow(). Thank you Pascal for all the insights and contributions!

I am closing this issue, but feel free to continue using it to give feedback regarding the newly added features. You can also open new issues if you think something is not working as intended with the new features.

from cvui.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.