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;
//设置登录窗口和添加窗口的样式
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(); //系统启动时间
//每秒任务定时器
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)
{
// //将图像大小缩放为显示窗口大小
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));
}
//定时任务,每秒运行一次
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 += "/";
//获取该路径下的所有文件
getFiles(srcDirPath.toStdString() + "*.jpg", files);
for (int i = 0; i < files.size(); i++)
{
//输入图像
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; //绘制矩形框时用来拷贝原图的图像
bool leftButtonDownFlag = false; //左键单击后视频暂停播放的标志位
cv::Point originalPoint; //矩形框起点
cv::Point processPoint; //矩形框终点
std::vector<cv::Rect> RectVet;
void onMouse(int event, int x, int y, int flags, void* ustc); //鼠标回调函数
void CigaretteSingle::on_pushButton_selectregion_released()
{
QString srcDirPath = QString::fromStdString(SelectDirIFD());
Alg m_alg;
vector<string> files;
qDebug() << "srcDirPath=" << srcDirPath;
srcDirPath += "/";
//获取该路径下的所有文件
getFiles(srcDirPath.toStdString() + "*.jpg", files);
if (files.size() == 0)
{
QMessageBox::StandardButton result = QMessageBox::information(NULL, QStringLiteral("提示"), QStringLiteral("没有找到图片"));
return;
}
cv::namedWindow("Image");
cv::setMouseCallback("Image", onMouse);
for (int index = 0; index < files.size(); index++)
{
//输入图像
//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. 起点等于终点
//2. 左键按下且未抬起
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键
else if ((k == 3014656) && (RectVet.size() != 0))RectVet.pop_back();//DELETE键
if (index == files.size() - 1)index = 0;
QCoreApplication::processEvents();
}
cv::imshow("Image", cv::Mat::zeros(cv::Size(1, 1), CV_8UC1));
cv::destroyWindow("Image");
}
//鼠标回调函数
void onMouse(int event, int x, int y, int flags, void* ustc)
{
if (event == CV_EVENT_LBUTTONDOWN)
{
leftButtonDownFlag = true; //标志位
originalPoint = cv::Point(x, y); //设置左键按下点的矩形起点
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)); //子图像显示
//cv::imshow("Sub Image", rectImage);
}
}