admin
2022-08-18 67a5e3d825fde17b7b00906ce42b0bd8cafebc4e
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#pragma once
#include <list>
#include <iostream>
#include <math.h>
#include <opencv2/opencv.hpp>
#include <opencv2/highgui.hpp>
#include <opencv2/core.hpp>
#include <opencv2/imgcodecs.hpp> 
#include <io.h>
 
using namespace std;
 
enum OperateType {
    //Âò
    OPERATE_BUY,
    //Âò³·
    OPERATE_BUY_CANCEL,
    //Âô
    OPERATE_SELL,
    //Âô³·
    OPERATE_SELL_CANCEL,
    //´íÎó
    OPERATE_OPERATE_ERROR,
};
 
class NumberData {
 
public:
    NumberData() {
    };
    cv::Mat data;
    std::string num;
 
};
 
class RecognitionUtil
{
private: 
 
#ifndef _NUMBER_DATA_DEFAINE_
#define _NUMBER_DATA_DEFAINE_
    static std::list<NumberData>  numSimples;
    static std::list<NumberData>  timeNumSimples;
#endif
 
public:
    static void init() {
        
        //½«Êý×ÖÑù±¾ÔØÈëÄÚ´æÖÐ
        numSimples.clear();
        for (int i = 0;i < 10;i++) {
            std::string sdir = "C:\\Users\\Administrator\\Desktop\\ocr\\number\\";
            if (_access(sdir.c_str(), 0)) {
                sdir = "sample\\number\\";
            }
 
            std::string path = sdir.append(std::to_string(i)).append(".jpg");
            NumberData data = NumberData();
            data.data = cv::imread(path.c_str(), cv::IMREAD_GRAYSCALE);
            data.num = std::to_string(i);
            numSimples.push_back(data);
        }
        //½«Ê±¼äÑù±¾ÔØÈëÄÚ´æÖÐ
        for (int i = 0;i < 60;i++) {
            std::string sdir = "C:\\Users\\Administrator\\Desktop\\ocr\\time\\";
            if (_access(sdir.c_str(), 0)) {
                sdir = "sample\\time\\";
            }
            std::string path = sdir;
            std::string num;
            if (i < 10) {
                num = std::to_string(0).append(std::to_string(i));
            }
            else {
                num = std::to_string(i);
            }
 
            path = path.append(num).append(".jpg");
 
 
            NumberData data = NumberData();
            data.data = cv::imread(path.c_str(), cv::IMREAD_GRAYSCALE);
            data.num = num;
            timeNumSimples.push_back(data);
        }
    }
    //»ñȡʱ¼ä
    static std::string getTime(cv::Mat img) {
        NumberData data = knn(img, timeNumSimples);
        return data.num;
    }
    //»ñÈ¡Êý×Ö
    static std::string getNumber(cv::Mat img) {
        NumberData data = knn(img, numSimples);
        return data.num;
    }
 
    static NumberData knn(cv::Mat target, std::list<NumberData> simples) {
        //imshow("¼ì²âÄ¿±ê", target);
        std::list<long> knnValue;
        std::list<NumberData>::iterator ele;
        for (ele = simples.begin(); ele != simples.end();ele++) {
            int cols = (*ele).data.cols;
            int rows = (*ele).data.rows;
            if (cols != target.cols || rows != target.rows) {
                continue;
            }
        
            int count = 0;
            for (int i = 0;i < rows;i++) {
                for (int j = 0; j < cols; j++)
                {
                    uchar svalue = (*ele).data.ptr<uchar>(i)[j];
                    uchar tvalue = target.ptr<uchar>(i)[j];
                    //50ÊÇÈ¥³ýÔëÉù
                    svalue = svalue > 50 ? 1 : 0;
                    tvalue = tvalue > 50 ? 1 : 0;
                    int result = abs(svalue - tvalue);
                    count += result;
                }
            }
 
            if (count == 0) {
                return  (*ele);
            }
            else {
                knnValue.push_back(count);
            }
        }
 
        //ÐèÒªÅÅÐò
        knnValue.sort();
        ele = simples.begin();
        return (*ele);
    }
 
};