You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
105 lines
2.8 KiB
C++
105 lines
2.8 KiB
C++
|
4 years ago
|
#include "BatchTest4Alg.h"
|
||
|
|
#include "cv.h"
|
||
|
|
#include "highgui.h"
|
||
|
|
#include "Luffy.h"
|
||
|
|
#include "qstringlist.h"
|
||
|
|
#include "qfile.h"
|
||
|
|
#include "qdatetime.h"
|
||
|
|
|
||
|
|
using namespace cv;
|
||
|
|
|
||
|
|
struct Region {
|
||
|
|
cv::Scalar color;
|
||
|
|
int id;
|
||
|
|
std::vector<cv::Point2i> points;
|
||
|
|
void write(cv::FileStorage& fs) const {
|
||
|
|
fs << "{" << "id" << id << "points" << points << "}";
|
||
|
|
}
|
||
|
|
void read(const cv::FileNode& node) {
|
||
|
|
//node["color"] >> color;
|
||
|
|
node["id"] >> id;
|
||
|
|
node["points"] >> points;
|
||
|
|
}
|
||
|
|
};
|
||
|
|
|
||
|
|
inline std::vector<std::vector<cv::Point>> read_points_info(std::string path) {
|
||
|
|
cv::FileStorage fs;
|
||
|
|
fs.open(path, cv::FileStorage::READ);
|
||
|
|
int count{ -1 };
|
||
|
|
int id{ -1 };
|
||
|
|
std::vector<std::vector<std::vector<cv::Point>>> regions{};
|
||
|
|
std::vector<std::vector<cv::Point>> defect_regions{ std::vector < cv::Point > {} };
|
||
|
|
cv::FileNode node = fs.getFirstTopLevelNode();
|
||
|
|
std::vector<std::vector<cv::Point2i>> dst;
|
||
|
|
while (1) {
|
||
|
|
Region re_load;
|
||
|
|
re_load.read(fs["region" + std::to_string(++count)]);
|
||
|
|
//fs["region" + std::to_string(++count)] >> re_load;
|
||
|
|
auto region = re_load.points;
|
||
|
|
if (region.empty()) {
|
||
|
|
if (!defect_regions.empty())regions.push_back(defect_regions);
|
||
|
|
fs.release();
|
||
|
|
break;
|
||
|
|
}
|
||
|
|
if (id != re_load.id) {
|
||
|
|
if (id != -1)regions.push_back(defect_regions);
|
||
|
|
while (re_load.id > int(regions.size())) {
|
||
|
|
regions.push_back(std::vector < std::vector<cv::Point> > {std::vector < cv::Point > {}});
|
||
|
|
}
|
||
|
|
id = re_load.id;
|
||
|
|
|
||
|
|
defect_regions.clear();
|
||
|
|
}
|
||
|
|
dst.push_back(re_load.points);
|
||
|
|
//defect_regions.push_back(region);
|
||
|
|
}
|
||
|
|
return dst;
|
||
|
|
}
|
||
|
|
|
||
|
|
BatchTest4Alg::BatchTest4Alg()
|
||
|
|
{
|
||
|
|
}
|
||
|
|
|
||
|
|
|
||
|
|
BatchTest4Alg::~BatchTest4Alg()
|
||
|
|
{
|
||
|
|
}
|
||
|
|
|
||
|
|
bool BatchTest4Alg::batchTest(QString strModel, Point pt, int nError, QString strBase, QString strFile)
|
||
|
|
{
|
||
|
|
QString m = strModel.split("##").last();
|
||
|
|
QString str = strFile.replace(m, m + "-out");
|
||
|
|
str = str.left(str.length() - 3) + "xml";
|
||
|
|
Point ptCali = getCaliPosition(str);
|
||
|
|
double dis = luffy_base::luffy_math::disofPoints(ptCali, pt);
|
||
|
|
dis = sqrt(dis);
|
||
|
|
return dis < 10;
|
||
|
|
}
|
||
|
|
|
||
|
|
cv::Point BatchTest4Alg::getCaliPosition(QString strFile)
|
||
|
|
{
|
||
|
|
std::string str = strFile.toLocal8Bit().data();
|
||
|
|
FileStorage fs(strFile.toLocal8Bit().data(), FileStorage::READ);
|
||
|
|
if (!fs.isOpened()) {
|
||
|
|
return Point();
|
||
|
|
}
|
||
|
|
vector<vector<Point>> pts = read_points_info(strFile.toLocal8Bit().data());
|
||
|
|
if (pts.size() == 0) {
|
||
|
|
return Point();
|
||
|
|
}
|
||
|
|
Rect rt = cv::boundingRect(pts[0]);
|
||
|
|
return Point(rt.x + rt.width / 2, rt.y + rt.height / 2);
|
||
|
|
}
|
||
|
|
|
||
|
|
bool BatchTest4Alg::saveResult(QString strModel, int nError, QString modelFile, QString saveFile)
|
||
|
|
{
|
||
|
|
QFile qFile(saveFile);
|
||
|
|
qFile.open(QIODevice::Append | QIODevice::WriteOnly);
|
||
|
|
QString str = "time:%1, model:%2, result:%3, file:%4\r\n";
|
||
|
|
str = str.arg(QDateTime::currentDateTime().toString("yyyy-MM-dd hh-mm-ss zzz")).arg(strModel).arg(nError).arg(modelFile);
|
||
|
|
qFile.write(str.toLocal8Bit().data());
|
||
|
|
qFile.close();
|
||
|
|
|
||
|
|
return true;
|
||
|
|
}
|