程式語言 - OpenCV - Window Utils - Mouse Event



參考資訊:
https://docs.opencv.org/master/d7/d9f/tutorial_linux_install.html

main.cpp

#include <iostream>
#include <opencv2/highgui.hpp>
#include <opencv2/imgcodecs.hpp>
#include <opencv2/imgproc.hpp>

using namespace std;
using namespace cv;

void mouseHandler(int event, int x, int y, int, void *)
{
    char buf[255] = {0};

    switch (event)
    {
    case EVENT_LBUTTONDOWN:
        strcpy(buf, "EVENT_LBUTTONDOWN");
        break;
    case EVENT_LBUTTONUP:
        strcpy(buf, "EVENT_LBUTTONUP");
        break;
    case EVENT_RBUTTONDOWN:
        strcpy(buf, "EVENT_RBUTTONDOWN");
        break;
    case EVENT_RBUTTONUP:
        strcpy(buf, "EVENT_RBUTTONUP");
        break;
    case EVENT_MBUTTONDOWN:
        strcpy(buf, "EVENT_MBUTTONDOWN");
        break;
    case EVENT_MBUTTONUP:
        strcpy(buf, "EVENT_MBUTTONUP");
        break;
    case EVENT_MOUSEMOVE:
        strcpy(buf, "EVENT_MOUSEMOVE");
        break;
    }
    Mat img(100, 400, CV_8UC3, Scalar(128, 255, 255));
    putText(img, buf, Point(50, 50), FONT_HERSHEY_SIMPLEX, 1, (0, 0, 0), 1, LINE_AA);
    imshow("mouse", img);
}

int main(int argc, char **argv)
{
    namedWindow("mouse", WINDOW_AUTOSIZE);
    setMouseCallback("mouse", mouseHandler, NULL);
    waitKey();
    return 0;
}

CMakeLists.txt

cmake_minimum_required(VERSION 2.8)
project(main)
find_package(OpenCV REQUIRED)
include_directories(${OpenCV_INCLUDE_DIRS})
add_executable(main main.cpp)
target_link_libraries(main ${OpenCV_LIBS})

編譯並且執行

$ cmake .
$ make
$ ./main

完成