Files
Hermes 20b79b0445
Build and Deploy / build (push) Failing after 3m22s
Build and Deploy / deploy (push) Skipped
fix(opencv): 阴影滑块按上游列扫描重写,修 findContours 三通道崩溃
上一版只加了尺寸校验,同尺寸输入暴露出第二个 bug:
  threshold 之后仍是 CV_8UC3 就交给 findContours →
  "FindContours supports only CV_8UC1 images"

现在按上游 AntiCAP solve_slider_comparison 的算法重写:
  · absdiff 逐通道,任一道 > 80 记为差异像素(对齐 image.point(lambda x: 255 if x > 80 else 0))
  · 三通道掩码用 cv::compare + bitwise_or 合成单通道 CV_8UC1
  · 逐列扫描:差异像素 >= 5 的列 → start_x = 列号+2,start_y = 首次达标行号-5(与上游一致)
  · 列扫描找不到时退回最大差异区域外接矩形(保留旧路径作为兜底)
  · 尺寸不一致返回 -2,Go 侧映射成 400
2026-12-03 08:43:24 +08:00

444 lines
14 KiB
C++
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#include <fstream>
#include <opencv2/opencv.hpp>
#include <opencv2/dnn.hpp>
#include <vector>
#include <string>
#include <cstring>
using namespace cv;
using namespace cv::dnn;
// 图像结构体定义
typedef struct {
unsigned char* data;
int width;
int height;
int channels;
} Image;
static std::string last_error;
// YOLO 检测器
class YOLODetector {
public:
Net net;
std::vector<std::string> classNames;
float confThreshold;
float nmsThreshold;
YOLODetector() : confThreshold(0.5f), nmsThreshold(0.4f) {}
};
static std::map<std::string, YOLODetector*> yolo_detectors;
extern "C" {
// 图像解码
Image* cv_imdecode(const unsigned char* buf, size_t size) {
try {
std::vector<unsigned char> data(buf, buf + size);
cv::Mat mat = cv::imdecode(data, cv::IMREAD_COLOR);
if (mat.empty()) {
last_error = "无法解码图像";
return nullptr;
}
Image* img = new Image();
img->width = mat.cols;
img->height = mat.rows;
img->channels = mat.channels();
size_t data_size = mat.total() * mat.elemSize();
img->data = (unsigned char*)malloc(data_size);
memcpy(img->data, mat.data, data_size);
return img;
} catch (const std::exception& e) {
last_error = e.what();
return nullptr;
}
}
// 释放图像
void cv_image_free(Image* img) {
if (img) {
if (img->data) {
free(img->data);
}
delete img;
}
}
// 加载 YOLO ONNX 模型
int cv_yolo_load(const char* name, const char* model_path, const char* classes_path) {
try {
YOLODetector* detector = new YOLODetector();
// 加载 ONNX 模型
detector->net = readNetFromONNX(model_path);
if (detector->net.empty()) {
last_error = "无法加载 ONNX 模型";
delete detector;
return -1;
}
// 设置后端
detector->net.setPreferableBackend(DNN_BACKEND_OPENCV);
detector->net.setPreferableTarget(DNN_TARGET_CPU);
// 加载类别名称
if (classes_path && strlen(classes_path) > 0) {
std::ifstream ifs(classes_path);
if (ifs.is_open()) {
std::string line;
while (std::getline(ifs, line)) {
if (!line.empty()) {
detector->classNames.push_back(line);
}
}
}
}
yolo_detectors[std::string(name)] = detector;
return 0;
} catch (const std::exception& e) {
last_error = e.what();
return -1;
}
}
// YOLO 检测结果
typedef struct {
float x1, y1, x2, y2;
float confidence;
int class_id;
char class_name[64];
} YOLODetection;
// YOLO 检测
int cv_yolo_detect(const char* name, const unsigned char* img_data, int width, int height, int channels,
YOLODetection** detections, int* count) {
try {
auto it = yolo_detectors.find(std::string(name));
if (it == yolo_detectors.end()) {
last_error = "YOLO 模型未加载";
return -1;
}
YOLODetector* detector = it->second;
// 创建 Mat
cv::Mat frame;
if (channels == 3) {
frame = cv::Mat(height, width, CV_8UC3, (void*)img_data);
cv::cvtColor(frame, frame, cv::COLOR_RGB2BGR);
} else if (channels == 4) {
cv::Mat tmp(height, width, CV_8UC4, (void*)img_data);
cv::cvtColor(tmp, frame, cv::COLOR_RGBA2BGR);
} else if (channels == 1) {
frame = cv::Mat(height, width, CV_8UC1, (void*)img_data);
cv::cvtColor(frame, frame, cv::COLOR_GRAY2BGR);
} else {
last_error = "不支持的通道数";
return -1;
}
// 预处理
int inpWidth = 640;
int inpHeight = 640;
cv::Mat blob;
cv::Size inputSize(inpWidth, inpHeight);
blobFromImage(frame, blob, 1/255.0, inputSize, Scalar(0,0,0), true, false);
// 推理
detector->net.setInput(blob);
std::vector<Mat> outs;
detector->net.forward(outs, detector->net.getUnconnectedOutLayersNames());
// 后处理
std::vector<int> classIds;
std::vector<float> confidences;
std::vector<Rect> boxes;
float scaleX = (float)frame.cols / inpWidth;
float scaleY = (float)frame.rows / inpHeight;
for (size_t i = 0; i < outs.size(); ++i) {
// YOLO11 输出格式: [1, 5+num_classes, 8400] (通道在前)
// 需要转置为 [8400, 5+num_classes]
Mat out = outs[i];
int num_detections = out.size[2]; // 8400
int num_features = out.size[1]; // 5 + num_classes
// 转置输出
Mat out_transposed;
if (out.size[1] < out.size[2]) {
// [1, features, detections] -> [detections, features]
Mat out_2d(out.size[1], out.size[2], CV_32F, out.data);
transpose(out_2d, out_transposed);
} else {
out_transposed = out;
}
for (int j = 0; j < num_detections; ++j) {
float* data = out_transposed.ptr<float>(j);
float obj_conf = data[4];
if (obj_conf < detector->confThreshold) continue;
// 找最大类别分数
int classId = 0;
float maxScore = 0;
for (int k = 5; k < num_features; ++k) {
if (data[k] > maxScore) {
maxScore = data[k];
classId = k - 5;
}
}
float confidence = obj_conf * maxScore;
if (confidence > detector->confThreshold) {
float cx = data[0];
float cy = data[1];
float w = data[2];
float h = data[3];
int centerX = (int)(cx * scaleX);
int centerY = (int)(cy * scaleY);
int width = (int)(w * scaleX);
int height = (int)(h * scaleY);
int left = centerX - width / 2;
int top = centerY - height / 2;
classIds.push_back(classId);
confidences.push_back(confidence);
boxes.push_back(Rect(left, top, width, height));
}
}
}
// NMS
std::vector<int> indices;
NMSBoxes(boxes, confidences, detector->confThreshold, detector->nmsThreshold, indices);
// 分配结果
*count = (int)indices.size();
if (*count > 0) {
*detections = (YOLODetection*)malloc(sizeof(YOLODetection) * (*count));
for (size_t i = 0; i < indices.size(); ++i) {
int idx = indices[i];
YOLODetection* det = &(*detections)[i];
det->x1 = (float)boxes[idx].x;
det->y1 = (float)boxes[idx].y;
det->x2 = (float)(boxes[idx].x + boxes[idx].width);
det->y2 = (float)(boxes[idx].y + boxes[idx].height);
det->confidence = confidences[idx];
det->class_id = classIds[idx];
if (det->class_id < (int)detector->classNames.size()) {
strncpy(det->class_name, detector->classNames[det->class_id].c_str(), 63);
det->class_name[63] = '\0';
} else {
sprintf(det->class_name, "class_%d", det->class_id);
}
}
}
return 0;
} catch (const std::exception& e) {
last_error = e.what();
return -1;
}
}
// 释放 YOLO 检测结果
void cv_yolo_detections_free(YOLODetection* detections) {
if (detections) {
free(detections);
}
}
// 释放 YOLO 检测器
void cv_yolo_unload(const char* name) {
auto it = yolo_detectors.find(std::string(name));
if (it != yolo_detectors.end()) {
delete it->second;
yolo_detectors.erase(it);
}
}
// 滑块缺口匹配
int cv_slider_match(const Image* target, const Image* background, int* out_x) {
try {
cv::Mat target_mat(target->height, target->width, CV_8UC3, target->data);
cv::Mat bg_mat(background->height, background->width, CV_8UC3, background->data);
cv::Mat target_gray, bg_gray;
cv::cvtColor(target_mat, target_gray, cv::COLOR_BGR2GRAY);
cv::cvtColor(bg_mat, bg_gray, cv::COLOR_BGR2GRAY);
// Canny 边缘检测
cv::Mat target_edges, bg_edges;
cv::Canny(target_gray, target_edges, 100, 200);
cv::Canny(bg_gray, bg_edges, 100, 200);
// 模板匹配
cv::Mat result;
cv::matchTemplate(bg_edges, target_edges, result, cv::TM_CCOEFF_NORMED);
double min_val, max_val;
cv::Point min_loc, max_loc;
cv::minMaxLoc(result, &min_val, &max_val, &min_loc, &max_loc);
*out_x = max_loc.x;
return 0;
} catch (const std::exception& e) {
last_error = e.what();
return -1;
}
}
// 阴影滑块匹配(对齐上游 AntiCAP 的 solve_slider_comparison)
//
// 上游语义:target 与 background 是同一张图的两份,只有滑块阴影区域不同。
// image = ImageChops.difference(background, target)
// image = image.point(lambda x: 255 if x > 80 else 0) # 逐通道阈值 80
// 逐列扫描:某列差异像素 >= 5 个 → start_x = 列号 + 2,start_y = 首次达到 5 个时的行号 - 5
//
// 旧实现有两个问题:
// 1) 不校验尺寸,target/background 尺寸不同时 absdiff 直接抛 OpenCV -209;
// 2) threshold 后仍是三通道图像就丢给 findContours,报
// "FindContours supports only CV_8UC1 images"。
int cv_slider_comparison(const Image* target, const Image* background, int* out_x, int* out_y) {
try {
if (target->width != background->width || target->height != background->height) {
last_error = "target 与 background 尺寸必须一致(阴影滑块是同一张图的两份)";
return -2;
}
cv::Mat t_mat(target->height, target->width, CV_8UC3, target->data);
cv::Mat b_mat(background->height, background->width, CV_8UC3, background->data);
cv::Mat diff;
cv::absdiff(b_mat, t_mat, diff); // BGR 各自绝对差
// 逐通道 > 80 记为差异像素,合成单通道掩码
std::vector<cv::Mat> ch(3);
cv::split(diff, ch);
cv::Mat m0, m1, m2, mask;
cv::compare(ch[0], 80, m0, cv::CMP_GT);
cv::compare(ch[1], 80, m1, cv::CMP_GT);
cv::compare(ch[2], 80, m2, cv::CMP_GT);
cv::bitwise_or(m0, m1, mask);
cv::bitwise_or(mask, m2, mask);
const int H = mask.rows, W = mask.cols;
int start_x = 0, start_y = 0;
bool found = false;
for (int x = 0; x < W && !found; ++x) {
int count = 0;
for (int y = 0; y < H; ++y) {
if (mask.at<uchar>(y, x) == 0) continue;
++count;
if (count >= 5 && start_y == 0) {
start_y = y - 5;
if (start_y < 0) start_y = 0;
}
}
if (count >= 5) {
start_x = x + 2;
if (start_x > W) start_x = W;
found = true;
}
}
if (!found) {
// 退化情况:没有满足列扫描的区域,退回最大差异区域的外接矩形
std::vector<std::vector<cv::Point>> contours;
cv::findContours(mask.clone(), contours, cv::RETR_EXTERNAL, cv::CHAIN_APPROX_SIMPLE);
int max_area = 0;
cv::Rect best;
for (const auto& c : contours) {
cv::Rect r = cv::boundingRect(c);
if (r.width * r.height > max_area) {
max_area = r.width * r.height;
best = r;
}
}
start_x = best.x;
start_y = best.y;
}
*out_x = start_x;
*out_y = start_y;
return 0;
} catch (const std::exception& e) {
last_error = e.what();
return -1;
}
}
// 检测旋转角度(简化版,使用特征点)
float cv_detect_rotation(const Image* img) {
try {
cv::Mat mat(img->height, img->width, CV_8UC3, img->data);
// 转灰度
cv::Mat gray;
cv::cvtColor(mat, gray, cv::COLOR_BGR2GRAY);
// 简化处理:返回 0 度
// 实际实现需要更复杂的特征点匹配
return 0.0f;
} catch (const std::exception& e) {
last_error = e.what();
return 0.0f;
}
}
// 图像相似度比较
float cv_compare_similarity(const Image* img1, const Image* img2) {
try {
cv::Mat mat1(img1->height, img1->width, CV_8UC3, img1->data);
cv::Mat mat2(img2->height, img2->width, CV_8UC3, img2->data);
// 确保 same size
if (mat1.size() != mat2.size()) {
cv::resize(mat2, mat2, mat1.size());
}
// 计算 histogram
cv::Mat hsv1, hsv2;
cv::cvtColor(mat1, hsv1, cv::COLOR_BGR2HSV);
cv::cvtColor(mat2, hsv2, cv::COLOR_BGR2HSV);
int h_bins = 50, s_bins = 60;
int histSize[] = {h_bins, s_bins};
float h_ranges[] = {0, 180};
float s_ranges[] = {0, 256};
const float* ranges[] = {h_ranges, s_ranges};
int channels[] = {0, 1};
cv::Mat hist1, hist2;
cv::calcHist(&hsv1, 1, channels, cv::Mat(), hist1, 2, histSize, ranges);
cv::calcHist(&hsv2, 1, channels, cv::Mat(), hist2, 2, histSize, ranges);
cv::normalize(hist1, hist1, 0, 1, cv::NORM_MINMAX);
cv::normalize(hist2, hist2, 0, 1, cv::NORM_MINMAX);
double similarity = cv::compareHist(hist1, hist2, cv::HISTCMP_CORREL);
return (float)similarity;
} catch (const std::exception& e) {
last_error = e.what();
return 0.0f;
}
}
const char* cv_get_last_error() {
return last_error.c_str();
}
} // extern "C"