feat!: 增加图像匹配算法 RGBCount, HSVCount

feat: 使用 F1-score 作为返回结果而不是 Recall

perf: 简化 F1-score 计算
This commit is contained in:
zzyyyl
2024-07-22 01:38:20 +08:00
parent 49fb5bcf2b
commit 98e24a1093
2 changed files with 28 additions and 1 deletions

View File

@@ -96,7 +96,7 @@ std::vector<Matcher::RawResult> Matcher::preproc_and_match(const cv::Mat& image,
cv::Mat matched;
cv::Mat image_for_match;
cv::Mat templ_for_match;
if (method == MatchMethod::CcoeffHSV) {
if (method == MatchMethod::CcoeffHSV || method == MatchMethod::HSVCount) {
cv::cvtColor(image, image_for_match, cv::COLOR_BGR2HSV);
cv::cvtColor(templ, templ_for_match, cv::COLOR_BGR2HSV);
}
@@ -152,6 +152,28 @@ std::vector<Matcher::RawResult> Matcher::preproc_and_match(const cv::Mat& image,
cv::matchTemplate(image_for_match, templ_for_match, matched, match_algorithm, mask_opt.value());
}
}
else if (method == MatchMethod::RGBCount || method == MatchMethod::HSVCount) {
// 待匹配图像与模板中指定颜色的像素数量比值越接近1越相似
auto templ_active_opt = calc_mask(templ_for_match, false);
auto image_active_opt = calc_mask(image_for_match, false);
if (!image_active_opt || !templ_active_opt) [[unlikely]] {
return {};
}
const auto& templ_active = templ_active_opt.value();
const auto& image_active = image_active_opt.value();
cv::Mat zero = cv::Mat::zeros(templ_active.size(), CV_8U);
cv::threshold(image_active, image_active, 1, 1, cv::THRESH_BINARY);
// 把 SQDIFF 当 count 用,计算 image_active 在 templ_active 形状内的像素数量
cv::Mat tp, fp;
int tp_fn = cv::countNonZero(templ_active);
cv::matchTemplate(image_active, zero, tp, cv::TM_SQDIFF, templ_active);
tp.convertTo(tp, CV_32S);
cv::Mat templ_inactive;
cv::bitwise_not(templ_active, templ_inactive);
cv::matchTemplate(image_active, zero, fp, cv::TM_SQDIFF, templ_inactive);
fp.convertTo(fp, CV_32S);
cv::divide(2 * tp, tp + fp + tp_fn, matched, 1, CV_32F); // matched = f1 score
}
results.emplace_back(RawResult { .matched = matched, .templ = templ, .templ_name = templ_name });
}
return results;