如果有比《一个简单的YOLO格式图片标注工具 Qt界面实现》更无聊的,那应该就是本文了8。
该代码实现功能是YOLO格式图片标注查看,打开一个存放图片的文件夹,和一个存放YOLO格式标签的文件夹,在图片上可视化矩形框。
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include
#include
#include
#include
#include
QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = nullptr);
~MainWindow();
private:
Ui::MainWindow *ui;
QString imagePath;
QString imagesPath;
QStringList imagesList;
QString labelPath;
QString labelsPath;
QStringList labelsList;
int num = 0;
private slots:
void Open();
void Next();
void readtxt(QFile& file, cv::Mat image);
};
#endif // MAINWINDOW_H
main.cpp
#include "mainwindow.h"
#include
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
QAction *openAction = new QAction;
openAction->setShortcut(QKeySequence(tr("Ctrl+O")));
ui->toolBar->addAction(openAction);
connect(openAction, SIGNAL(triggered()), this, SLOT(Open()));
QAction *nextAction = new QAction;
nextAction->setShortcut(QKeySequence(tr("Right")));
ui->toolBar->addAction(nextAction);
connect(nextAction, SIGNAL(triggered()), this, SLOT(Next()));
}
MainWindow::~MainWindow()
{
delete ui;
}
QStringList getImageList(QString path)
{
QStringList imagesList;
QDir dir(path);
if (!dir.exists())
{
qDebug() << "path is non-existent!";
return imagesList;
}
dir.setFilter(QDir::Files | QDir::NoSymLinks);
QStringList filters;
filters << "*.bmp" << "*.jpg" << "*png";
dir.setNameFilters(filters);
imagesList = dir.entryList();
return imagesList;
}
QImage MatToImage(cv::Mat &mat) //Mat转QImage
{
switch(mat.type())
{ //QIamge 构造函数, ((const uchar *data, 宽(列),高(行), 一行共多少个(字节)通道,宽度*字节数,宏参数)
case CV_8UC1:
{
QImage img((uchar *)mat.data,mat.cols,mat.rows,mat.cols * 1,QImage::Format_Grayscale8);
return img;
}
break;
case CV_8UC3: //一个像素点由三个字节组成
{
//cvtColor(m,m,COLOR_BGR2RGB); BGR转RGB
QImage img((uchar *)mat.data,mat.cols,mat.rows,mat.cols * 3,QImage::Format_RGB888);
return img.rgbSwapped(); //opencv是BGR Qt默认是RGB 所以RGB顺序转换
}
break;
case CV_8UC4:
{
QImage img((uchar *)mat.data,mat.cols,mat.rows,mat.cols * 4,QImage::Format_RGB32);
return img;
}
break;
default:
{
QImage img; //如果遇到一个图片均不属于这三种,返回一个空的图片
return img;
}
}
}
void MainWindow::readtxt(QFile& file, cv::Mat image)
{
while(!file.atEnd())
{
QByteArray line = file.readLine();
QString str(line);
QStringList strList = str.split(" ");
float x = strList[1].toFloat();
float y = strList[2].toFloat();
float w = strList[3].toFloat();
float h = strList[4].toFloat();
int x1= (x-w/2)*image.cols;
int y1= (y-h/2)*image.rows;
int x2= (x+w/2)*image.cols;
int y2= (y+h/2)*image.rows;
cv::rectangle(image,cv::Point(x1,y1),cv::Point(x2,y2),cv::Scalar(0,0,255),2);
}
QImage qimage = MatToImage(image);
ui->label->setPixmap(QPixmap::fromImage(qimage));
}
void MainWindow::Open()
{
imagesPath = QFileDialog::getExistingDirectory(this,tr("打开"),"");
qDebug()<<imagesPath;
if(imagesPath.isEmpty())
return;
imagesList = getImageList(imagesPath);
QString image_name = imagesList[0];
qDebug()<<imagesList;
imagePath = imagesPath+"/"+image_name;
cv::Mat image = cv::imread(imagePath.toStdString());
ui->label->setPixmap(QPixmap(imagePath));
ui->statusbar->showMessage(imagePath+" "+QString::number(num+1)+"/"+QString::number(imagesList.size()));
labelsPath = QFileDialog::getExistingDirectory(this,tr("打开"),"");
if(labelsPath.isEmpty())
return;
labelPath = labelsPath+"/"+image_name.replace(".bmp", ".txt").replace(".jpg", ".txt").replace(".png", ".txt");
QFile file(labelPath);
if(!file.open(QIODevice::ReadOnly))
return;
readtxt(file, image);
}
void MainWindow::Next()
{
if(num+1>=imagesList.size())
return;
ui->label->clear();
num++;
QString image_name = imagesList[num];
imagePath = imagesPath+"/"+image_name;
ui->statusbar->showMessage(imagePath+" "+QString::number(num+1)+"/"+QString::number(imagesList.size()));
cv::Mat image = cv::imread(imagePath.toStdString());
ui->label->setPixmap(QPixmap(imagePath));
labelPath = labelsPath+"/"+image_name.replace(".bmp", ".txt").replace(".jpg", ".txt").replace(".png", ".txt");
QFile file(labelPath);
if(!file.open(QIODevice::ReadOnly))
return;
readtxt(file, image);
}