////myopencv.pro文件
QT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
CONFIG += c++11
# The following define makes your compiler emit warnings if you use
# any Qt feature that has been marked deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS
# You can also make your code fail to compile if it uses deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
SOURCES += \
main.cpp \
myopencv.cpp
HEADERS += \
myopencv.h
# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target
INCLUDEPATH +=D:\Professional\OpenCV\OpenCVMinGW\OpenCV-MinGW-Build-OpenCV-4.5.2-x64\include
//动态链接库位置
LIBS +=D:\Professional\OpenCV\OpenCVMinGW\OpenCV-MinGW-Build-OpenCV-4.5.2-x64\x64\mingw\bin\libopencv_core452.dll
LIBS +=D:\Professional\OpenCV\OpenCVMinGW\OpenCV-MinGW-Build-OpenCV-4.5.2-x64\x64\mingw\bin\libopencv_highgui452.dll
LIBS +=D:\Professional\OpenCV\OpenCVMinGW\OpenCV-MinGW-Build-OpenCV-4.5.2-x64\x64\mingw\bin\libopencv_imgproc452.dll
LIBS +=D:\Professional\OpenCV\OpenCVMinGW\OpenCV-MinGW-Build-OpenCV-4.5.2-x64\x64\mingw\bin\libopencv_calib3d452.dll
LIBS +=D:\Professional\OpenCV\OpenCVMinGW\OpenCV-MinGW-Build-OpenCV-4.5.2-x64\x64\mingw\bin\libopencv_imgcodecs452.dll
LIBS +=D:\Professional\OpenCV\OpenCVMinGW\OpenCV-MinGW-Build-OpenCV-4.5.2-x64\x64\mingw\bin\libopencv_videoio452.dll
////myopencv.h文件:
#ifndef MYOPENCV_H
#define MYOPENCV_H
#include <QWidget>
////核心模块,OpenCV库的基本构建块
#include "opencv2/core/core.hpp"
////图像编码模块,提供read、write等方法
#include "opencv2/imgcodecs/imgcodecs.hpp"
////图像处理模块
#include "opencv2/imgproc/imgproc.hpp"//image process
////包含创建/释放窗口、载入/保存图像、显示图像等方法
#include "opencv2/highgui/highgui.hpp"
class myOpenCV : public QWidget
{
Q_OBJECT
public:
myOpenCV(QWidget *parent = nullptr);
~myOpenCV();
public:
////将OpenCV的矩阵数据转换为QPixmap的函数
QPixmap matricsToPixmap(const cv::Mat& myMat);
};
#endif // MYOPENCV_H
////myopencv.cpp文件:
#include "myopencv.h"
#include <QLabel>
#include <QDebug>
#include <iostream>
using namespace cv;
myOpenCV::myOpenCV(QWidget *parent)
: QWidget(parent)
{
QLabel* myLabel = new QLabel(this);
myLabel->setGeometry(0, 0, 960, 540);
//读取一张图片
Mat myMat = imread("C:/Users/12615/Pictures/Saved Pictures/ZJN.jpeg",1);
////OpenCV显示图片的方法
//imshow("myMat", myMat);
/*
注意:
1、Mat对象的数据结构几乎只包括矩阵头Header和指向数据的指针两部分构成。
2、为了节省资源和处理速度,处理Mat对象的复制运算符(包括拷贝构造、矩阵赋值等)
仅将矩阵头和指向矩阵数据的指针复制,而不会复制矩阵数据本身。因此,这样创建出来
的Mat对象都指向同一片矩阵数据,对其中一个修改就会导致其它的复制品的值发生变化。
3、基于上述,矩阵数据本身会属于多个Mat对象。矩阵数据本身引入了计数器机制,即
增加一个引用矩阵数据本身的Mat对象时,就会增加一个矩阵头,清理对于Mat对象时,
矩阵头就会减少。当矩阵头计数器减少为0时,释放矩阵数据本身。
4、clone()和copyTo()方法与上述不同,是真正的克隆了矩阵数据,对克隆体进行修改不会影响原数据。
*/
////矩阵克隆
Mat myMatA = myMat.clone();
////输出矩阵的数据值
//std::cout<<myMatA<<std::endl<<std::endl;
////截取图像的某一区域
Mat myMatB = Mat(myMat, Rect(20, 20, 400, 400));
////Scalar()方法用于将Mat对象所有数据初始化为自定义的值,例如此处为58。
////CV_[每个项目的位数][有符号或无符号][类型前缀]C(通道号)
Mat myMatC = Mat(400, 400, CV_8UC(1), Scalar(58));
////CV_[每个项目的位数][有符号或无符号][类型前缀]C[通道数]
Mat myMatD(2, 2, CV_8UC3, Scalar(168, 140, 0));
std::cout<<myMatD<<std::endl<<std::endl;
/*
打印可得:[168, 140, 0, 168, 140, 0;
168, 140, 0, 168, 140, 0]
*/
////create可以为Mat重新分配尺寸,但不可用于构造初始化!!!
myMatD.create(1, 1, CV_8UC3);
std::cout<<myMatD<<std::endl<<std::endl;
/*
打印可得:[ 12, 0, 0]
*/
////randu()可为矩阵元素用随机值初始化,需要指定随机数上下限。
randu(myMatD, Scalar::all(0), Scalar::all(255));
//将myMatE的三个通道(R、G、B)的所有元素数据值都赋为0~255中的任意一个数。
Mat myMatE(960, 540, CV_8UC3);
randu(myMatE, Scalar::all(0), Scalar::all(255));
//输出结果为像素色彩为随机的图片
myLabel->setPixmap(matricsToPixmap(myMatE));
}
QPixmap myOpenCV::matricsToPixmap(const cv::Mat& myMat)
{
cv::Mat myMatA;
//检测矩阵是否为空
if(!(myMat.empty()))
{
////色彩空间转换。OpenCV的默认读取方式为BGR
cv::cvtColor(myMat, myMatA, COLOR_BGR2RGB);
////将OpenCV的矩阵数据转换为QImage
QImage myQImageA = QImage((const unsigned char*)(myMatA.data), myMatA.cols, myMatA.rows, myMatA.step, QImage::Format_RGB888);
return QPixmap::fromImage(myQImageA);
}
else
{
qDebug()<<"Failed to convert the matrics !!!"<<endl;
myMatA = myMat.clone();
QImage myQImageA = QImage((const unsigned char*)(myMatA.data), myMatA.cols, myMatA.rows, myMatA.step, QImage::Format_RGB888);
return QPixmap::fromImage(myQImageA);
}
}
myOpenCV::~myOpenCV()
{
}