Switch Controller
Loading...
Searching...
No Matches
ImageEvent.h
Go to the documentation of this file.
1#pragma once
2
3#include "pch.h"
4
5#include <opencv2/core.hpp>
6#include <opencv2/highgui.hpp>
7#include <opencv2/imgcodecs.hpp>
8#include <opencv2/imgproc.hpp>
9
11
12#define AC_DISPLAY_IMAGE_MATCH 0
13
15public:
16 ImageEvent(const cv::Mat &templatePic, const cv::Mat &maskPic,
17 const int matchMethod, const double matchThreshold,
18 const cv::Rect imageCrop, VideoFrameSink *videoFrameSink)
19 : templatePic(templatePic), maskPic(maskPic), matchMethod(matchMethod),
20 matchThreshold(matchThreshold), imageCrop(imageCrop),
21 videoFrameSink(videoFrameSink) {}
22
23 uint8_t value() const {
24
25 this->videoFrameSink->waitForInit();
26 this->videoFrameSink->getData(videoData);
27
28 cv::Mat screenshot = cv::Mat(this->videoFrameSink->getHeight(),
29 this->videoFrameSink->getWidth(), CV_8UC3,
30 videoData.data());
31
32 cv::Mat submat = cv::Mat(screenshot, imageCrop);
33
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,
41 maskPic);
42 } else {
43 cv::matchTemplate(submat, templatePic, result, matchMethod);
44 }
45
46 double minVal;
47 double maxVal;
48 double critalMatchVal;
49 cv::Point minLoc;
50 cv::Point maxLoc;
51 cv::Point matchPoint;
52 minMaxLoc(result, &minVal, &maxVal, &minLoc, &maxLoc);
53 if (matchMethod == cv::TM_SQDIFF ||
54 matchMethod == cv::TM_SQDIFF_NORMED) {
55 matchPoint = minLoc;
56 critalMatchVal = minVal;
57 } else {
58 matchPoint = maxLoc;
59 critalMatchVal = maxVal;
60 }
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);
70
71 cv::namedWindow(name + "-1", cv::WINDOW_AUTOSIZE);
72 cv::imshow(name + "-1", submat);
73 cv::waitKey(1);
74 cv::namedWindow(name + "-2", cv::WINDOW_AUTOSIZE);
75 cv::imshow(name + "-2", result);
76 cv::waitKey(1);
77#endif
78
79 spdlog::debug("image match: ({}, {}) {}", matchPoint.x, matchPoint.y,
80 critalMatchVal);
81 if (matchMethod == cv::TM_SQDIFF ||
82 matchMethod == cv::TM_SQDIFF_NORMED) {
83 return critalMatchVal < matchThreshold;
84 } else {
85 return matchThreshold < critalMatchVal;
86 }
87 }
88
89private:
90 const cv::Mat templatePic;
91 const cv::Mat maskPic;
92 const int matchMethod;
93 const double matchThreshold;
94 const cv::Rect imageCrop;
95
96 VideoFrameSink *videoFrameSink;
97 mutable std::vector<uint8_t> videoData;
98};
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