环境:windows 7 + qt
Opencv库版本:opencv-2.4.10
编译器:mingw-gcc-4.7.1
在这之前写过一篇在linux下编译Opencv库的文章,既然库都编译好了,现在就是怎么来使用这个库的问题了。在windows下也编译了Opencv库,这里就使用windows下的Opencv库 + qt 来写一个简单的测试程序。qt在windows下的安装及部署请参考网络上其他的文章。
1. 创建一个qt的工程
因为要使用opencv库,所在在qt的工程中要加入opencv的库所在的路径,具体添加如下
打开opencv_qt_study_001.pro在后面添加下面的内容
INCLUDEPATH +=D:\opencv-2.4.10\opencv\mingw\install\include
LIBS += D:\opencv-2.4.10\opencv\mingw\install\x86\mingw\lib\libopencv_calib3d2410d.dll.a
LIBS += D:\opencv-2.4.10\opencv\mingw\install\x86\mingw\lib\libopencv_contrib2410d.dll.a
LIBS += D:\opencv-2.4.10\opencv\mingw\install\x86\mingw\lib\libopencv_core2410d.dll.a
LIBS += D:\opencv-2.4.10\opencv\mingw\install\x86\mingw\lib\libopencv_features2d2410d.dll.a
LIBS += D:\opencv-2.4.10\opencv\mingw\install\x86\mingw\lib\libopencv_flann2410d.dll.a
LIBS += D:\opencv-2.4.10\opencv\mingw\install\x86\mingw\lib\libopencv_gpu2410d.dll.a
LIBS += D:\opencv-2.4.10\opencv\mingw\install\x86\mingw\lib\libopencv_highgui2410d.dll.a
LIBS += D:\opencv-2.4.10\opencv\mingw\install\x86\mingw\lib\libopencv_imgproc2410d.dll.a
LIBS += D:\opencv-2.4.10\opencv\mingw\install\x86\mingw\lib\libopencv_legacy2410d.dll.a
LIBS += D:\opencv-2.4.10\opencv\mingw\install\x86\mingw\lib\libopencv_ml2410d.dll.a
LIBS += D:\opencv-2.4.10\opencv\mingw\install\x86\mingw\lib\libopencv_nonfree2410d.dll.a
LIBS += D:\opencv-2.4.10\opencv\mingw\install\x86\mingw\lib\libopencv_objdetect2410d.dll.a
LIBS += D:\opencv-2.4.10\opencv\mingw\install\x86\mingw\lib\libopencv_ocl2410d.dll.a
LIBS += D:\opencv-2.4.10\opencv\mingw\install\x86\mingw\lib\libopencv_photo2410d.dll.a
LIBS += D:\opencv-2.4.10\opencv\mingw\install\x86\mingw\lib\libopencv_stitching2410d.dll.a
LIBS += D:\opencv-2.4.10\opencv\mingw\install\x86\mingw\lib\libopencv_superres2410d.dll.a
LIBS += D:\opencv-2.4.10\opencv\mingw\install\x86\mingw\lib\libopencv_ts2410d.a
LIBS += D:\opencv-2.4.10\opencv\mingw\install\x86\mingw\lib\libopencv_video2410d.dll.a
LIBS += D:\opencv-2.4.10\opencv\mingw\install\x86\mingw\lib\libopencv_videostab2410d.dll.a
到此在写打开图片程序前的准备工作已经完成,下面开始下主要的代码(完整的工程在最后面的附件中)。
void MainWindow::on_pushButton_clicked()
{
QString filename = QFileDialog::getOpenFileName(this,
tr("Open Image"),
".",
tr("Image File(*.png *.jpg *.jpeg *.bmp)")); //(1)
m_image = cv::imread(filename.toAscii().data());//(2)
cv::namedWindow("Image");//(3)
cv::imshow("Image",m_image);//(4)
}
这样就可以打开一幅图片了,
(1)选择需要打开的图片
(2)加载图片
(3)定义图像显示的窗口
(4)显示图像,(imshow有两个参数,第一个参数是窗口,即已经定义过的窗口,第二个参数是需要显示的图像)
附件:
http://download.csdn.net/detail/chengfei609/8701629