-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcoordinategrabber.cpp
More file actions
133 lines (87 loc) · 3.68 KB
/
coordinategrabber.cpp
File metadata and controls
133 lines (87 loc) · 3.68 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
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
#include "coordinategrabber.h"
#include <chrono>
#include <iostream>
using namespace std::chrono;
CoordinateGrabber::CoordinateGrabber()
{
setImageToCompareWith();
setCompareImagesNameFileMap();
setInstrumentCoordinatesFromImage();
}
CoordinateGrabber::~CoordinateGrabber()
{
}
void CoordinateGrabber::setInstrumentCoordinatesFromImage(){
for (auto const& [name, image] : m_compareImagesNameFileMap){
cv::Mat result, imgDisplay, templateImage;
image.copyTo(templateImage);
m_imageToCompareWith.copyTo(imgDisplay);
int result_cols = imgDisplay.cols - templateImage.cols + 1;
int result_rows = imgDisplay.rows - templateImage.rows + 1;
result.create( result_rows, result_cols, CV_32FC1 );
auto start = high_resolution_clock::now();
cv::matchTemplate(imgDisplay,templateImage, result, cv::TM_SQDIFF );
auto stop = high_resolution_clock::now();
auto duration = duration_cast<milliseconds>(stop - start);
cv::normalize(result, result, 0,1, cv::NORM_MINMAX, -1, cv::Mat());
std::cout << "Duration in miliseconds " <<duration.count() << std::endl;
double minVal, maxVal;
cv::Point minLoc, maxLoc, matchLoc;
cv::minMaxLoc(result, &minVal, &maxVal, &minLoc, &maxLoc, cv::Mat());
matchLoc = minLoc; // This is different based on matching algorithm used, refer to the docs for more information https://docs.opencv.org/3.4/de/da9/tutorial_template_matching.html
cv::Point oppositeEnd = cv::Point( matchLoc.x + templateImage.cols , matchLoc.y + templateImage.rows );
rectangle( imgDisplay, matchLoc, oppositeEnd, cv::Scalar::all(255), 2, 8, 0 );
std::vector<int> coordinates = {matchLoc.x, matchLoc.y, oppositeEnd.x, oppositeEnd.y };
m_instrumentNamesAndCoordinates.insert({name,coordinates});
// showImageInDialogUtility(imgDisplay);
}
}
void CoordinateGrabber::setCompareImagesNameFileMap()
{
QString directoryPath = ":/images_for_coordinates";
QDir directory(directoryPath);
QStringList imagesList = directory.entryList((QStringList("*.png")));
for (auto& imagePath: imagesList){
QString qrcPath = directoryPath+ "/"+imagePath;
cv::Mat img = loadFromQrc(qrcPath, 1);
QStringList list = imagePath.split('.');
QString fileName = list[0];
m_compareImagesNameFileMap.insert({fileName, img});
// showImageInDialogUtility(img);
}
}
void CoordinateGrabber::setImageToCompareWith()
{
m_imageToCompareWith = loadFromQrc(":/windows.png",1 );
// showImageInDialogUtility(m_imageToCompareWith);
}
void CoordinateGrabber::showImageInDialogUtility(cv::Mat img)
{
QImage imdisplay((uchar*)img.data, img.cols, img.rows, img.step, QImage::Format_Grayscale8);
QDialog dlg;
QHBoxLayout *l = new QHBoxLayout(&dlg);
QLabel *label = new QLabel();
l->addWidget(label);
label->setPixmap(QPixmap::fromImage(imdisplay));
dlg.setGeometry(30,30, 100,100);
dlg.exec();
}
const std::map<QString, std::vector<int> > &CoordinateGrabber::instrumentNamesAndCoordinates() const
{
return m_instrumentNamesAndCoordinates;
}
cv::Mat CoordinateGrabber::loadFromQrc(QString qrc, int flag)
{
QFile file(qrc);
cv::Mat brgImage,grayImage;
if(file.open(QIODevice::ReadOnly))
{
qint64 sz = file.size();
std::vector<uchar> buf(sz);
file.read((char*)buf.data(), sz);
brgImage = cv::imdecode(buf, flag);
cv::cvtColor(brgImage,grayImage, cv::COLOR_BGR2GRAY);
cv::normalize(grayImage, grayImage, 0., 255., cv::NORM_MINMAX, CV_8U);
}
return grayImage;
}