88 lines
3.1 KiB
C++
88 lines
3.1 KiB
C++
#pragma once
|
|
|
|
#include "Types.h"
|
|
#include "PaperConfig.h"
|
|
#include <opencv2/opencv.hpp>
|
|
#include <vector>
|
|
|
|
class PaperRecognizer {
|
|
public:
|
|
PaperRecognizer();
|
|
~PaperRecognizer();
|
|
|
|
// 主识别函数
|
|
RecognitionResult recognize(const cv::Mat& image);
|
|
|
|
// 获取裁剪后的试纸图片(NAPI用)
|
|
cv::Mat getPaperImg() const;
|
|
|
|
private:
|
|
// 定位块配对结果
|
|
struct LocatorPair {
|
|
bool isValid;
|
|
LocatorInfo* big;
|
|
LocatorInfo* small;
|
|
};
|
|
// ===== 第一步:图像预处理 =====
|
|
void preprocessImage(const cv::Mat& image);
|
|
|
|
// ===== 第二步:定位块检测 =====
|
|
bool detectLocators();
|
|
void findCandidateContours(std::vector<std::vector<cv::Point>>& contours);
|
|
bool filterLocatorCandidates(const std::vector<std::vector<cv::Point>>& contours);
|
|
bool validateLocatorPair();
|
|
// 多条试纸处理
|
|
std::vector<std::vector<LocatorInfo*>> groupCandidatesByY(std::vector<LocatorInfo>& candidates);
|
|
LocatorPair findValidLocatorPairInGroup(std::vector<LocatorInfo*>& group);
|
|
|
|
// ===== 第三步:颜色判断 =====
|
|
LocatorColor determineLocatorColor(const cv::Rect& roi);
|
|
LocatorColor determineLocatorColorByCenter(const cv::RotatedRect& rotatedRect);
|
|
double calculateColorDistance(const cv::Scalar& c1, const cv::Scalar& c2);
|
|
cv::Scalar getAverageColor(const cv::Rect& roi);
|
|
cv::Scalar getCenterRegionColor(const cv::RotatedRect& rotatedRect);
|
|
|
|
// ===== 第四步:规格匹配 =====
|
|
bool matchPaperSpec();
|
|
|
|
// ===== 第五步:色块位置计算 =====
|
|
void calculateColorBlockCenters();
|
|
|
|
// ===== 第六步:绘制标注 =====
|
|
void drawAnnotations();
|
|
|
|
// ===== 第七步:保存试纸图片 =====
|
|
void savePaperImg();
|
|
|
|
// ===== 辅助函数 =====
|
|
double calculateAspectRatio(const cv::RotatedRect& rect);
|
|
bool isPotentialLocator(const cv::RotatedRect& rect, double area, double& aspectRatio);
|
|
void sortLocatorsByPosition();
|
|
BigLocatorType determineBigLocatorType(double areaRatio);
|
|
|
|
// ===== 清晰度评估 =====
|
|
BlurLevel assessLocatorBlur(const cv::Mat& gray, const cv::RotatedRect& locator, double& score);
|
|
|
|
private:
|
|
cv::Mat originalImage_; // 原图
|
|
cv::Mat processImage_; // 处理用图像
|
|
cv::Mat hsvImage_; // HSV图像
|
|
cv::Mat grayImage_; // 灰度图像
|
|
|
|
LocatorInfo locator1_; // 第一个定位块
|
|
LocatorInfo locator2_; // 第二个定位块
|
|
bool hasLocator1_;
|
|
bool hasLocator2_;
|
|
|
|
LocatorInfo* bigLocator_; // 大定位块指针
|
|
LocatorInfo* smallLocator_; // 小定位块指针
|
|
|
|
std::vector<cv::Point2f> centers_; // 色块中心点
|
|
const PaperConfig* config_; // 匹配的配置
|
|
|
|
RecognitionResult result_; // 结果
|
|
cv::Mat paperImg_; // 裁剪后的试纸图片(RGBA格式)
|
|
|
|
size_t candidateCount_; // 候选定位块数量(用于杂物检测)
|
|
};
|