cisstStereoVision tutorial 3 - jhu-cisst/cisst GitHub Wiki

cisstStereoVision Architecture

  • [wiki:SVLTutorialFilterImageWindow Handling image window events (mouse and keyboard)]

Example 3 - Image Window Event Handling

List of files

  • CMakeLists.txt: CMake script for creating compiler dependent projects
  • CMyEventHandler.h: Image window event handler class declaration
  • CMyEventHandler.cpp: Image window event handler class definition
  • main.cpp: Application entry point (main() function)

All files can be found under cisstStereoVision/examples/tutorial2.

Source code

CMake

PROJECT(svlTutorial2)

FIND_PACKAGE(cisst REQUIRED)

IF(cisst_FOUND)

    INCLUDE(${CISST_USE_FILE})

    ADD_EXECUTABLE(${PROJECT_NAME}
                   CMyEventHandler.h
                   CMyEventHandler.cpp
                   main.cpp
                   )

    CISST_TARGET_LINK_LIBRARIES(${PROJECT_NAME} cisstCommon cisstVector cisstOSAbstraction cisstMultiTask cisstStereoVision)

ENDIF(cisst_FOUND)

Event handler class declaration

#include <cisstStereoVision/svlFilterImageWindow.h>

class CMyEventHandler : public svlWindowEventHandlerBase
{
public:
    CMyEventHandler();

    void OnUserEvent(unsigned int winid, bool ascii, unsigned int eventid);

private:
    svlRect ROI;
    bool LMouseButtonPressed;
};

Event handler class implementation

#include "CMyEventHandler.h"

CMyEventHandler::CMyEventHandler() :
    LMouseButtonPressed(false)
{
}

void CMyEventHandler::OnUserEvent(unsigned int winid, bool ascii, unsigned int eventid)
{
    if (ascii) {                                               // Event is an ASCII character
        std::cout << static_cast<char>(eventid) << std::flush; // Write out character
    }
    else {                                                     // Event is not an ASCII character:
                                                               //   mouse event or special key
        int x, y;
        GetMousePos(x, y);

        switch (eventid) {
            case winInput_LBUTTONDOWN:
                LMouseButtonPressed = true;
                ROI.left = ROI.right = x;
                ROI.top = ROI.bottom = y;
                std::cerr << ROI.left << ", "
                          << ROI.top << ", "
                          << ROI.right << ", "
                          << ROI.bottom << std::endl;
            break;

            case winInput_LBUTTONUP:
                if (LMouseButtonPressed) {
                    ROI.Normalize();
                    std::cerr << "ROI = " << ROI.left << ", "
                                          << ROI.top << ", "
                                          << ROI.right << ", "
                                          << ROI.bottom << std::endl;
                }
                LMouseButtonPressed = false;
            break;

            case winInput_MOUSEMOVE:
                if (LMouseButtonPressed) {
                    ROI.right = x;
                    ROI.bottom = y;
                    std::cerr << ROI.left << ", "
                              << ROI.top << ", "
                              << ROI.right << ", "
                              << ROI.bottom << std::endl;
                }
            break;

            case winInput_KEY_F1:
            case winInput_KEY_F2:
            case winInput_KEY_F3:
            case winInput_KEY_F4:
            case winInput_KEY_F5:
            case winInput_KEY_F6:
            case winInput_KEY_F7:
            case winInput_KEY_F8:
            case winInput_KEY_F9:
            case winInput_KEY_F10:
            case winInput_KEY_F11:
            case winInput_KEY_F12:
            case winInput_KEY_PAGEUP:
            case winInput_KEY_PAGEDOWN:
            case winInput_KEY_HOME:
            case winInput_KEY_END:
            case winInput_KEY_INSERT:
            case winInput_KEY_DELETE:
            case winInput_KEY_LEFT:
            case winInput_KEY_RIGHT:
            case winInput_KEY_UP:
            case winInput_KEY_DOWN:
                std::cout << "Special key down" << std::endl;
            break;
        }
    }
}

Main program

#include <cisstStereoVision.h>
#include "CMyEventHandler.h"

int main()
{
    svlInitialize();                                // Discover supported devices and codecs

    svlStreamManager stream;                        // Instantiate SVL Stream
    svlFilterSourceVideoCapture source(1);          // Instantiate video capture filter
    svlFilterImageWindow window;                    // Instantiate image window filter

    CMyEventHandler event_handler;                  // Instantiate our own window event handler
    window.SetEventHandler(&event_handler);         // Assign event handler to image window filter

    source.SetDevice(0);                            // Select first available capture device

    stream.SetSourceFilter(&source);                // Assign source filter to stream
    source.GetOutput()->Connect(window.GetInput()); // Connect source filter to window filter

    stream.Play();                                  // Initialize and Play video stream

    char ch;
    std::cin >> ch;                                 // (Wait for key-press)

    stream.Release();                               // Release stream

    return 0;
}
⚠️ **GitHub.com Fallback** ⚠️