1.环境配置
VS2019+QT5.13.2+Opencv4.4
(其中Opencv版本必须在4.4及以上,否则不支持YOLOv4)
关于环境的配置参考我之前的博客:
2.QLabel控件的使用
2.1QLabel控件的应用
①显示文字 (普通文本、html)
label->setText(“Hello, World!”);
②显示图片
QPixmap pixmap;
pixmap.load(":/Image/boat.jpg");
QLabel *label = new QLabel;
label.setPixmap(pixmap);
(PS:QLabel还可以显示gif动画或网页链接)
Qt OpenCV 在界面显示图片 通过Lable方式 和GraphicsView 方式
2.2QString 的用法
QString中间是可以包含’\0’符号的,QString也存在append()函数。
参考:
QString::number(int)
QString::fromLocal8Bit("支持中文!")
3.源代码
#include "ConsoleShowLabel.h"
#include <QtWidgets/QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
ConsoleShowLabel w;
w.show();
return a.exec();
}
ConsoleShowLabel.h
#pragma once
#include <QtWidgets/QWidget>
#include "ui_ConsoleShowLabel.h"
#include <opencv2/opencv.hpp>
#include<opencv2/dnn.hpp>
#include<fstream>
#include<iostream>
using namespace std;
using namespace cv;
using namespace cv::dnn;
class ConsoleShowLabel : public QWidget
{
Q_OBJECT
public:
ConsoleShowLabel(QWidget *parent = Q_NULLPTR);
private:
Ui::ConsoleShowLabelClass ui;
private slots:
void Startdetect();
};
ConsoleShowLabel.cpp
#include "ConsoleShowLabel.h"
ConsoleShowLabel::ConsoleShowLabel(QWidget *parent): QWidget(parent)
{
ui.setupUi(this);
}
void ConsoleShowLabel::Startdetect()
{
ifstream classNamesFile("./model/coco.names");
vector<string> classNamesVec;
if (classNamesFile.is_open())
{
string className = "";
while (std::getline(classNamesFile, className))
classNamesVec.push_back(className);
}
String cfg = "./model/yolov4.cfg";
String weight = "./model/yolov4.weights";
dnn::Net net = readNetFromDarknet(cfg, weight);
Mat frame = imread("./image/test.jpg");
Mat inputBlob = blobFromImage(frame, 1.0 / 255, Size(608, 608), Scalar());
net.setInput(inputBlob);
std::vector<String> outNames = net.getUnconnectedOutLayersNames();
std::vector<Mat> outs;
net.forward(outs, outNames);
float* data;
Mat scores;
vector<Rect> boxes;
vector<int> classIds;
vector<float> confidences;
int centerX, centerY, width, height, left, top;
float confidenceThreshold = 0.2;
double confidence;
Point classIdPoint;
for (size_t i = 0; i < outs.size(); ++i)
{
data = (float*)outs[i].data;
for (int j = 0; j < outs[i].rows; ++j, data += outs[i].cols)
{
scores = outs[i].row(j).colRange(5, outs[i].cols);
minMaxLoc(scores, 0, &confidence, 0, &classIdPoint);
if (confidence > confidenceThreshold)
{
centerX = (int)(data[0] * frame.cols);
centerY = (int)(data[1] * frame.rows);
width = (int)(data[2] * frame.cols);
height = (int)(data[3] * frame.rows);
left = centerX - width / 2;
top = centerY - height / 2;
classIds.push_back(classIdPoint.x);
confidences.push_back((float)confidence);
boxes.push_back(Rect(left, top, width, height));
}
}
}
vector<int> indices;
NMSBoxes(boxes, confidences, 0.3, 0.2, indices);
Scalar rectColor, textColor;
Rect box, textBox;
int idx;
String className;
Size labelSize;
QString show_text;
show_text = QString::fromLocal8Bit("当前图像的尺寸:");
show_text.append(QString::number(frame.size().width));
show_text.append(QString::fromLocal8Bit("×"));
show_text.append(QString::number(frame.size().height));
show_text.append("\n");
cout << "当前图像的尺寸:" << frame.size() << endl;
for (size_t i = 0; i < indices.size(); ++i)
{
idx = indices[i];
className = classNamesVec[classIds[idx]];
labelSize = getTextSize(className, FONT_HERSHEY_SIMPLEX, 0.5, 1, 0);
box = boxes[idx];
textBox = Rect(Point(box.x - 1, box.y),
Point(box.x + labelSize.width, box.y - labelSize.height));
rectColor = Scalar(idx * 11 % 256, idx * 22 % 256, idx * 33 % 256);
textColor = Scalar(255 - idx * 11 % 256, 255 - idx * 22 % 256, 255 - idx * 33 % 256);
rectangle(frame, box, rectColor, 2, 8, 0);
rectangle(frame, textBox, rectColor, -1, 8, 0);
putText(frame, className.c_str(), Point(box.x, box.y - 2), FONT_HERSHEY_SIMPLEX, 0.5, textColor, 1, 8);
cout << className << ":" << "width:" << box.width << ",height:" << box.height << ",center:" << (box.tl() + box.br()) / 2 << endl;
show_text.append(QString::fromLocal8Bit(className.c_str()));
show_text.append(": width:");
show_text.append(QString::number(box.width));
show_text.append(", height:");
show_text.append(QString::number(box.height));
show_text.append(", center:");
int center_x = (box.tl().x + box.br().x) / 2;
show_text.append(QString::number(center_x));
show_text.append(",");
int center_y = (box.tl().y + box.br().y) / 2;
show_text.append(QString::number(center_y));
show_text.append("\n");
}
ui.labelshow->setText(show_text);
Mat show_detect_img;
cvtColor(frame, show_detect_img, COLOR_BGR2RGB);
QImage disImage = QImage((const unsigned char*)(show_detect_img.data), show_detect_img.cols, show_detect_img.rows, QImage::Format_RGB888);
ui.label_detect_display->setPixmap(QPixmap::fromImage(disImage.scaled(ui.label_detect_display->size(), Qt::KeepAspectRatio)));
}
效果:
上述工程源文件:
链接:
提取码:vkrd