| 49 | } |
| 50 | |
| 51 | double MatchPattern(QImage &img, const PatternImageData &patternData, |
| 52 | double threshold, cv::Mat &result, bool useAlphaAsMask, |
| 53 | cv::TemplateMatchModes matchMode) |
| 54 | { |
| 55 | double bestFitValue = std::numeric_limits<double>::signaling_NaN(); |
| 56 | result = cv::Mat(0, 0, CV_32F); |
| 57 | if (img.isNull() || patternData.rgbaPattern.empty()) { |
| 58 | return bestFitValue; |
| 59 | } |
| 60 | if (img.height() < patternData.rgbaPattern.rows || |
| 61 | img.width() < patternData.rgbaPattern.cols) { |
| 62 | return bestFitValue; |
| 63 | } |
| 64 | |
| 65 | auto input = QImageToMat(img); |
| 66 | |
| 67 | if (useAlphaAsMask) { |
| 68 | // Remove alpha channel of input image as the alpha channel |
| 69 | // information is used as a stencil for the pattern instead and |
| 70 | // thus should not be used while matching the pattern as well |
| 71 | // |
| 72 | // Input format is Format_RGBA8888 so discard the 4th channel |
| 73 | std::vector<cv::Mat1b> inputChannels; |
| 74 | cv::split(input, inputChannels); |
| 75 | std::vector<cv::Mat1b> rgbChanlesImage( |
| 76 | inputChannels.begin(), inputChannels.begin() + 3); |
| 77 | cv::Mat3b rgbInput; |
| 78 | cv::merge(rgbChanlesImage, rgbInput); |
| 79 | |
| 80 | cv::matchTemplate(rgbInput, patternData.rgbPattern, result, |
| 81 | matchMode, patternData.mask); |
| 82 | } else { |
| 83 | cv::matchTemplate(input, patternData.rgbaPattern, result, |
| 84 | matchMode); |
| 85 | } |
| 86 | |
| 87 | // A perfect match is represented as "0" for TM_SQDIFF_NORMED |
| 88 | // |
| 89 | // For TM_CCOEFF_NORMED and TM_CCORR_NORMED a perfect match is |
| 90 | // represented as "1" |
| 91 | // |
| 92 | // -> Invert TM_SQDIFF_NORMED in the preprocess step |
| 93 | preprocessPatternMatchResult(result, matchMode == cv::TM_SQDIFF_NORMED); |
| 94 | |
| 95 | cv::minMaxLoc(result, nullptr, &bestFitValue); |
| 96 | cv::threshold(result, result, threshold, 0.0, cv::THRESH_TOZERO); |
| 97 | return bestFitValue; |
| 98 | } |
| 99 | |
| 100 | int CountPatternMatches(const cv::Mat &result, const cv::Size &patternSize) |
| 101 | { |
no test coverage detected