——————–*.pro—————————–
添加
INCLUDEPATH += /home/yue/openCV/install-arm/include\
/home/yue/openCV/install-arm/include/opencv4 \
/home/yue/openCV/install-arm/include/opencv2
LIBS += /home/yue/openCV/install-arm/lib/libopencv_highgui.so \
/home/yue/openCV/install-arm/lib/libopencv_core.so \
/home/yue/openCV/install-arm/lib/libopencv_imgproc.so \
/home/yue/openCV/install-arm/lib/libopencv_imgcodecs.so
———————mainwindow.h————————-
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include
#include
#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;
private:
QImage cvMatToQImage(const cv::Mat& mat);
};
#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.h———————-
#include “mainwindow.h”
#include “ui_mainwindow.h”
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
cv::Mat image = cv::Mat::zeros(300, 600, CV_8UC3);
// 设置文本内容、字体、大小、颜色和厚度
std::string text = “Rotated Text”;
int fontFace = cv::FONT_HERSHEY_SIMPLEX;
double fontScale = 1.0;
int thickness = 2;
cv::Scalar color(255, 255, 255); // 白色
// 获取文本大小
int baseline = 0;
cv::Size textSize = cv::getTextSize(text, fontFace, fontScale, thickness, &baseline);
// 计算文本位置(中心对齐)
cv::Point textOrg((image.cols – textSize.width) / 2,
(image.rows + textSize.height) / 2);
// 创建旋转矩阵
double angle = 45; // 旋转角度
cv::Mat rotMat = cv::getRotationMatrix2D(textOrg, angle, 1.0);
// 绘制旋转文本
cv::putText(image, text, textOrg, fontFace, fontScale, color, thickness, cv::LINE_AA);
// 应用旋转变换
cv::warpAffine(image, image, rotMat, image.size());
// 将OpenCV图像转换为QImage
QImage qImage = cvMatToQImage(image);
// 在Qt中显示图像
ui->label->setPixmap(QPixmap::fromImage(qImage));
ui->label->show();
}
MainWindow::~MainWindow()
{
delete ui;
}
QImage MainWindow:: cvMatToQImage(const cv::Mat& mat)
{
if (mat.channels() == 3) {
// Convert the image to RGB
cv::Mat rgb;
cv::cvtColor(mat, rgb, cv::COLOR_BGR2RGB);
return QImage((const unsigned char*)(rgb.data),
rgb.cols, rgb.rows,
QImage::Format_RGB888);
} else {
return QImage((const unsigned char*)(mat.data),
mat.cols, mat.rows,
QImage::Format_Grayscale8);
}
}