5#include <opencv2/core.hpp>
6#include <opencv2/highgui.hpp>
7#include <opencv2/imgcodecs.hpp>
8#include <opencv2/imgproc.hpp>
12#define AC_DISPLAY_IMAGE_MATCH 0
16 ImageEvent(
const cv::Mat &templatePic,
const cv::Mat &maskPic,
17 const int matchMethod,
const double matchThreshold,
19 : templatePic(templatePic), maskPic(maskPic), matchMethod(matchMethod),
20 matchThreshold(matchThreshold), imageCrop(imageCrop),
21 videoFrameSink(videoFrameSink) {}
26 this->videoFrameSink->
getData(videoData);
28 cv::Mat screenshot = cv::Mat(this->videoFrameSink->
getHeight(),
29 this->videoFrameSink->getWidth(), CV_8UC3,
32 cv::Mat submat = cv::Mat(screenshot, imageCrop);
34 int result_cols = submat.cols - templatePic.cols + 1;
35 int result_rows = submat.rows - templatePic.rows + 1;
36 cv::Mat result(result_rows, result_cols, CV_32FC1);
37 if ((cv::TM_CCORR == matchMethod ||
38 matchMethod == cv::TM_CCORR_NORMED) &&
39 maskPic.cols > 0 && maskPic.rows > 0) {
40 cv::matchTemplate(submat, templatePic, result, matchMethod,
43 cv::matchTemplate(submat, templatePic, result, matchMethod);
48 double critalMatchVal;
52 minMaxLoc(result, &minVal, &maxVal, &minLoc, &maxLoc);
53 if (matchMethod == cv::TM_SQDIFF ||
54 matchMethod == cv::TM_SQDIFF_NORMED) {
56 critalMatchVal = minVal;
59 critalMatchVal = maxVal;
61#if AC_DISPLAY_IMAGE_MATCH == 1
62 rectangle(submat, matchPoint,
63 cv::Point(matchPoint.x + templatePic.cols,
64 matchPoint.y + templatePic.rows),
65 cv::Scalar::all(0), 2, 8, 0);
66 rectangle(result, matchPoint,
67 cv::Point(matchPoint.x + templatePic.cols,
68 matchPoint.y + templatePic.rows),
69 cv::Scalar::all(0), 2, 8, 0);
71 cv::namedWindow(name +
"-1", cv::WINDOW_AUTOSIZE);
72 cv::imshow(name +
"-1", submat);
74 cv::namedWindow(name +
"-2", cv::WINDOW_AUTOSIZE);
75 cv::imshow(name +
"-2", result);
79 spdlog::debug(
"image match: ({}, {}) {}", matchPoint.x, matchPoint.y,
81 if (matchMethod == cv::TM_SQDIFF ||
82 matchMethod == cv::TM_SQDIFF_NORMED) {
83 return critalMatchVal < matchThreshold;
85 return matchThreshold < critalMatchVal;
90 const cv::Mat templatePic;
91 const cv::Mat maskPic;
92 const int matchMethod;
93 const double matchThreshold;
94 const cv::Rect imageCrop;
97 mutable std::vector<uint8_t> videoData;
void waitForInit()
Definition: FFmpegFrameSink.h:48
long long getData(std::vector< uint8_t > &data)
Definition: FFmpegFrameSink.h:62
Definition: ImageEvent.h:14
ImageEvent(const cv::Mat &templatePic, const cv::Mat &maskPic, const int matchMethod, const double matchThreshold, const cv::Rect imageCrop, VideoFrameSink *videoFrameSink)
Definition: ImageEvent.h:16
uint8_t value() const
Definition: ImageEvent.h:23
Definition: VideoFrameSink.h:17
int getHeight() const
Definition: VideoFrameSink.h:100