qt 使用OpenCV
使用MinGW编译OpenCV源码
下载OpenCV源码
、CMake编译工具
编译安装完:
Qt工程配置
INCLUDEPATH += D:/qt_work/opencv/build/install/include/
LIBS += -LD:/qt_work/opencv/build/install/x86/mingw/bin \
-lopencv_core411 \
-lopencv_imgcodecs411 \
-lopencv_highgui411
举例一
#include "opencv2/opencv.hpp"
using namespace cv;
Widget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget)
{
ui->setupUi(this);
Mat img = imread("D:/4.jpg");
if (img.empty())
{
std::cout << "Error: load image failed." << std::endl;
return;
}
imshow("myImage", img);
waitKey(0);
}
举例二
Qt工程文件增加库文件引用
LIBS += -lopencv_imgproc411
Widget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget)
{
ui->setupUi(this);
Mat img = imread("D:/4.jpg");
if (img.empty())
{
std::cout << "Error: load image failed." << std::endl;
return;
}
if(img.channels()==3)
{
cvtColor(img,img,COLOR_BGR2RGB);
}
QImage qImag = QImage(img.data, img.cols, img.rows, QImage::Format_RGB888);
ui->label->setPixmap(QPixmap::fromImage(qImag.scaled(ui->label->size())));
}