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.
Cigarette/CigaretteSingle/CigaretteSingle.cpp

218 lines
6.9 KiB
C++

#include "CigaretteSingle.h"
#include <QFile>
#include <QMessageBox>
#include <common.h>
SyncQueue<std::pair<std::string, cv::Mat> >* g_save_queue;
SyncQueue<cv::Mat>* g_image_queue;
ASyncQueue<bool>* g_result_queue;
CigaretteSingle::CigaretteSingle(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::CigaretteSingleClass())
{
ui->setupUi(this);
ui->centralWidget->setStyleSheet("background-color : #1F2026;");
QFile qrcfile;
//<2F><><EFBFBD>õ<EFBFBD>¼<EFBFBD><C2BC><EFBFBD>ں<EFBFBD><DABA><EFBFBD><EFBFBD>Ӵ<EFBFBD><D3B4>ڵ<EFBFBD><DAB5><EFBFBD>ʽ
qrcfile.setFileName(":/CigaretteSingle/darkblack.css");
qrcfile.open(QIODevice::ReadOnly);
QString strQss = qrcfile.readAll();
this->setStyleSheet(strQss);
g_save_queue = new SyncQueue<std::pair<std::string, cv::Mat> >(Queue_Size);
g_save_queue->name = "save queue";
g_image_queue = new SyncQueue<cv::Mat>(Queue_Size);
g_image_queue->name = format("image");
g_result_queue = new ASyncQueue<bool>(Queue_Size);
g_result_queue->name = format("result");
saveThread.init(g_save_queue);
saveThread.start_work();
g_ts_start = QDateTime::currentDateTime(); //ϵͳ<CFB5><CDB3><EFBFBD><EFBFBD>ʱ<EFBFBD><CAB1>
//ÿ<><C3BF><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD><CAB1>
m_pTimer = new QTimer(this);
connect(m_pTimer, SIGNAL(timeout()), this, SLOT(event_per_second()));
m_pTimer->start(1000);
connect(ui->textBrowser, SIGNAL(cursorPositionChanged()), this, SLOT(autoScroll()));
m_alg_ptr = new Alg;
int hik_cnt = 0;
hik_cnt = HIKCamera::Enumerate();
if (hik_cnt)
{
HIKCamera::HikController = HIKCamera::InitHIKController(0);
HikControllerPtr = HIKCamera::HikController;
HikControllerPtr->p_image_queue = g_image_queue;
HikControllerPtr->p_result_queue = g_result_queue;
HikControllerPtr->OpenCamera();
}
work_thread.p_image_queue = g_image_queue;
work_thread.p_result_queue = g_result_queue;
work_thread.p_save_queue = g_save_queue;
work_thread.m_alg_ptr = m_alg_ptr;
work_thread.init();
connect(&work_thread, SIGNAL(display_image(cv::Mat)), this, SLOT(DisplayImage(cv::Mat)));
work_thread.start_work();
}
CigaretteSingle::~CigaretteSingle()
{
delete g_save_queue;
delete g_image_queue;
delete g_result_queue;
delete ui;
}
void CigaretteSingle::DisplayImage(cv::Mat m)
{
// //<2F><>ͼ<EFBFBD><CDBC><EFBFBD><EFBFBD>С<EFBFBD><D0A1><EFBFBD><EFBFBD>Ϊ<EFBFBD><CEAA>ʾ<EFBFBD><CABE><EFBFBD>ڴ<EFBFBD>С
cv::Size display_size = cv::Size(ui->image_label->width(), ui->image_label->height());
cv::resize(m, m, display_size);
QImage tempImage = cvMatToQImage(m);
ui->image_label->setPixmap(QPixmap::fromImage(tempImage));
}
//<2F><>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ÿ<EFBFBD><C3BF><EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><D2BB>
void CigaretteSingle::event_per_second()
{
static int weight_per_minute_mat[10] = {0};
static INT16 last_weight = 0;
QDateTime ts_start = QDateTime::currentDateTime();
QDateTime local(ts_start);
QString localTime = local.toString("yyyy-MM-dd hh:mm:ss");
//ui->label_cur_time_val->setText(localTime);
qint64 intervalTime = g_ts_start.secsTo(ts_start);
int days = (intervalTime) / (24 * 3600);
int hours = (intervalTime) % (24 * 3600) / 3600;
int minutes = (intervalTime) % 3600 / 60;
int second = (intervalTime) % 60;
QString run_stat = SecondToQStrTime(intervalTime);
ui->label_run_time_val->setText(run_stat);
}
void CigaretteSingle::on_pushButton_mtest_released()
{
QString srcDirPath = QFileDialog::getExistingDirectory(this, "choose src Directory", "./");
if (srcDirPath.isEmpty())
{
return;
}
vector<string> files;
qDebug() << "srcDirPath=" << srcDirPath;
srcDirPath += "/";
//<2F><>ȡ<EFBFBD><C8A1>·<EFBFBD><C2B7><EFBFBD>µ<EFBFBD><C2B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ļ<EFBFBD>
getFiles(srcDirPath.toStdString() + "*.jpg", files);
for (int i = 0; i < files.size(); i++)
{
//<2F><><EFBFBD><EFBFBD>ͼ<EFBFBD><CDBC>
std::cout << "open file------ " << files[i] << std::endl;
cv::Mat imagein = cv::imread(srcDirPath.toStdString() + files[i], cv::IMREAD_COLOR);
if (imagein.data)
{
cv::cvtColor(imagein, imagein, cv::COLOR_BGR2GRAY);
g_image_queue->put(imagein.clone());
Sleep(500);
}
}
}
//cv::Mat image;
//cv::Mat imageCopy; //<2F><><EFBFBD>ƾ<EFBFBD><C6BE>ο<EFBFBD>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ԭͼ<D4AD><CDBC>ͼ<EFBFBD><CDBC>
bool leftButtonDownFlag = false; //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ƶ<EFBFBD><C6B5>ͣ<EFBFBD><CDA3><EFBFBD>ŵı<C5B5>־λ
cv::Point originalPoint; //<2F><><EFBFBD>ο<EFBFBD><CEBF><EFBFBD><EFBFBD><EFBFBD>
cv::Point processPoint; //<2F><><EFBFBD>ο<EFBFBD><CEBF>յ<EFBFBD>
std::vector<cv::Rect> RectVet;
void onMouse(int event, int x, int y, int flags, void* ustc); //<2F><><EFBFBD><EFBFBD><EFBFBD>ص<EFBFBD><D8B5><EFBFBD><EFBFBD><EFBFBD>
void CigaretteSingle::on_pushButton_selectregion_released()
{
QString srcDirPath = QString::fromStdString(SelectDirIFD());
Alg m_alg;
vector<string> files;
qDebug() << "srcDirPath=" << srcDirPath;
srcDirPath += "/";
//<2F><>ȡ<EFBFBD><C8A1>·<EFBFBD><C2B7><EFBFBD>µ<EFBFBD><C2B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ļ<EFBFBD>
getFiles(srcDirPath.toStdString() + "*.jpg", files);
if (files.size() == 0)
{
QMessageBox::StandardButton result = QMessageBox::information(NULL, QStringLiteral("<EFBFBD><EFBFBD>ʾ"), QStringLiteral("û<EFBFBD><EFBFBD><EFBFBD>ҵ<EFBFBD>ͼƬ"));
return;
}
cv::namedWindow("Image");
cv::setMouseCallback("Image", onMouse);
for (int index = 0; index < files.size(); index++)
{
//<2F><><EFBFBD><EFBFBD>ͼ<EFBFBD><CDBC>
//std::cout << "open file------ " << files[index] << std::endl;
cv::Mat imagein = cv::imread(srcDirPath.toStdString() + files[index], cv::IMREAD_COLOR);
cv::cvtColor(imagein, imagein, cv::COLOR_BGR2GRAY);
if (!imagein.data)
{
break;
}
//1. <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>յ<EFBFBD>
//2. <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>δ̧<CEB4><CCA7>
if ((originalPoint != processPoint) && (leftButtonDownFlag == true))
{
rectangle(imagein, originalPoint, processPoint, cv::Scalar(255, 0, 0), 2);
}
if (RectVet.size())
{
for (int i = 0; i < RectVet.size(); i++)
{
cv::Mat input = imagein(RectVet[i]).clone();
cv::Mat output;
m_alg.process_bool(input, output);
cv::putText(imagein, to_string (i), RectVet[i].tl(),CV_FONT_HERSHEY_DUPLEX, 1, cv::Scalar(0, 0, 255));
rectangle(imagein, RectVet[i], cv::Scalar(0, 0, 255), 2);
}
}
imshow("Image", imagein);
//cv::cvtColor(imagein, imagein, cv::COLOR_BGR2GRAY);
//g_image_queue->put(imagein.clone());
//Sleep(500);
int k = cv::waitKeyEx(1);
if (k == 27)break;//ESC<53><43>
else if ((k == 3014656) && (RectVet.size() != 0))RectVet.pop_back();//DELETE<54><45>
if (index == files.size() - 1)index = 0;
QCoreApplication::processEvents();
}
cv::imshow("Image", cv::Mat::zeros(cv::Size(1, 1), CV_8UC1));
cv::destroyWindow("Image");
}
//<2F><><EFBFBD><EFBFBD><EFBFBD>ص<EFBFBD><D8B5><EFBFBD><EFBFBD><EFBFBD>
void onMouse(int event, int x, int y, int flags, void* ustc)
{
if (event == CV_EVENT_LBUTTONDOWN)
{
leftButtonDownFlag = true; //<2F><>־λ
originalPoint = cv::Point(x, y); //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>µ<EFBFBD><C2B5>ľ<EFBFBD><C4BE><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
processPoint = originalPoint;
}
if (event == CV_EVENT_MOUSEMOVE && leftButtonDownFlag)
{
processPoint = cv::Point(x, y);
}
if (event == CV_EVENT_LBUTTONUP)
{
leftButtonDownFlag = false;
RectVet.push_back(cv::Rect(originalPoint, processPoint));
//cv::Mat rectImage = image(cv::Rect(originalPoint, processPoint)); //<2F><>ͼ<EFBFBD><CDBC><EFBFBD><EFBFBD>ʾ
//cv::imshow("Sub Image", rectImage);
}
}