-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChessSampling.cpp
More file actions
70 lines (55 loc) · 2.16 KB
/
ChessSampling.cpp
File metadata and controls
70 lines (55 loc) · 2.16 KB
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
//
// Created by xushuyang on 2018/7/31.
//
#include "ChessSampling.h"
#include "applicationutils.h"
#include <QDebug>
ChessSampling::ChessSampling() : chessRect(QRect(0, 0, 0, 0)){}
QList<cv::Mat> ChessSampling::grabSample(cv::Mat &screen) {
if (chessRect.width() == 0) {
chessRect = QRect(Chess::detect_chess_board(screen));
qDebug() << "detect chess board(" << chessRect.left() << "," << chessRect.top() << "," << chessRect.width()
<< "," << chessRect.height() << ")";
}
QList<cv::Mat> result;
if (chessRect.width() == 0) {
return result;
}
QList<QRect> circles = filter(Chess::hough_detection_circle(screen));
QSize size(48, 48);
for (QRect circle : circles) {
cv::Mat out;
cv::Mat roi = screen(cv::Rect(circle.left() - size.width() / 2, circle.top() - size.width() / 2, size.width(),
size.width()));
cv::Mat mask = cv::Mat::zeros(size.width(), size.width(), screen.type());
cv::circle(mask, cv::Point(24, 24), circle.width() - 4, CV_RGB(255, 255, 255), -1);
roi.copyTo(out, mask);
cv::inRange(out, cv::Scalar(0,0,56), cv::Scalar(132,130,193), out);
cv::threshold(out, out, 0, 255.0, CV_THRESH_BINARY_INV);
out.convertTo(out, CV_32F);
result.push_back(out);
}
return result;
}
void ChessSampling::test(cv::Mat &screen) {
QList<cv::Mat> list = grabSample(screen);
QString path = Hub::current_dir();
path.append("/resources/train/");
for(cv::Mat mat : list) {
QString name(path);
name.append(QString::number(qrand())).append(".jpg");
cv::imwrite(name.toStdString(), mat);
}
}
QList<QRect> ChessSampling::filter(QList<QRect> list) {
QRect extandRect = QRect(chessRect.left() - 10, chessRect.top() - 10, chessRect.width() + 10,
chessRect.height() + 10);
QList<QRect> result;
for (QRect rect : list) {
if (extandRect.contains(rect.topLeft())) {
result.push_back(rect);
}
}
qDebug() << "input cicle size:" << list.size() << " filter:" << list.size() - result.size();
return result;
}