OpenCV中的图像处理 —— 图像阈值+图像平滑+形态转换(opencv进行图像处理)
688
2022-05-29
简介
系统环境
System:Win10_64
IDE版本
QtCreator:4.8.2
Qt:5.12.2
CMake:3.17.1
OpenCV:4.4.0
下载
OpenCv
OpenCv:OpenCv下载列表
OpenCv_4.4.0:OpenCv4.4.0
来自于某一个博主:
OpenCv:国内索引列表
OpenCv:OpenCv4.4.0
官网链接就不贴了。
OpenCv_Xontrib_4.4.0
OpenCv_contrib:OpenCv_contrib
OpenCv_contrib_4.4.0:OpenCv_Contrib_4.4.0
Qt5.12.2
Qt:Qt官网-
Qt5.12.2:Qt5.12.2.exe
CMake
CMake:CMakeDownload
CMake3.17.1:GitHubCmake3.17.1
CMake3.17.1.msi:cmake-3.17.1-win64-x64.msi
安装
Opencv
双击打开 opencv-4.4.0-vc14_vc15.exe,选择自己的安装路径。笔者:E:\E盘根目录
安装完为: E:\opencv
OpenCv-Contrib-4.4.0
解压opencv_contrib-4.4.0.zip,解压至 E:\opencv目录下为:
目录: E:\opencv
Mode LastWriteTime Length Name
---- ------------- ------ ----
d----- 2020/9/27 9:30 build
d----- 2020/9/27 9:39 opencv_contrib-4.4.0
d----- 2020/9/27 9:34 sources
-a---- 2020/7/18 7:23 2336 LICENSE.txt
-a---- 2020/7/18 7:23 27913 LICENSE_FFMPEG.txt
-a---- 2020/7/18 7:23 731 README.md.txt
Qt5.12.2
正常的双击安装。注意选择MinGw。安装目录为 E:\Qt
目录: E:\Qt
Mode LastWriteTime Length Name
---- ------------- ------ ----
d----- 2020/8/15 17:08 Qt5.12.2
d----- 2020/8/15 12:03 Qt5.9.2
注意:如果没有Qt帐号需要断网安装,就可以点击skip 跳过登录界面。或者自己注册帐号登录也可以。根据自己需要配置一些依赖库。
CMake
双击安装。
编译
MinGw_64
当前目录结构
目录: E:\opencv
Mode LastWriteTime Length Name
---- ------------- ------ ----
d----- 2020/9/27 9:30 build
d----- 2020/9/27 9:39 opencv_contrib-4.4.0
d----- 2020/9/27 9:34 sources
-a---- 2020/7/18 7:23 2336 LICENSE.txt
-a---- 2020/7/18 7:23 27913 LICENSE_FFMPEG.txt
-a---- 2020/7/18 7:23 731 README.md.txt
创建编译目录,安装目录
PS E:\opencv> mkdir MinGw_64
目录: E:\opencv
Mode LastWriteTime Length Name
---- ------------- ------ ----
d----- 2020/9/27 12:42 MinGw_64
PS E:\opencv> mkdir MinGw_64_build
目录: E:\opencv
Mode LastWriteTime Length Name
---- ------------- ------ ----
d----- 2020/9/27 12:42 MinGw_64_build
配置 source code 和 build the binaries
点击Configure
工程类型配置
工程类型选则: MinGw Makeifile
编译器选择:QtTools 下的 MinGw/bingcc and g++
等待配置完成 显示 Configuring done
配置安装路径
CMAKE_INSTALL_PREFIX E:/opencv/MinGw_64
配置modules
OPENCV_EXTRA_MODULES_PATH E:/opencv/opencv_contrib-4.4.0/modules
在CMakeGui Search栏 搜索 CMAKE_INSTALL_PREFIX 和 OPENCV_EXTRA_MODULES_PATH,即可找到对应的配置项。
配置支持Qt
WITH_QT TRUE
在CMakeGui Search栏 搜索 WITH_QT 即可找到对应的配置项。
配置OPENGL
WITH_OPENGL TRUE
取消Test编译
BUILD_PERE_TESTS FALSE
BUILD_TESTS FALSE
BUILD_opencv_python_tests FLASE
OPENCV_ENABLE_ALLOCATOR_STATS FALSE
OPENCV_GENERATE_SETUPVARS FALSE
在CMakeGui Search栏 搜索 test 即可找到对应的配置项,全部取消
配置完成后继续点击Configure
QT5_DIR配置
在点击Configure 之后 会有如下错误提示:
CMake Error at cmake/OpenCVFindLibsGUI.cmake:18 (find_package):
Could not find a package configuration file provided by "Qt5" with any of
the following names:
Qt5Config.cmake
qt5-config.cmake
Qt5Config.cmake
Add the installation prefix of "Qt5" to COPENCV_ENABLE_ALLOCATOR_STATSMAKE_PREFIX_PATH or set "Qt5_DIR"
to a directory containing one of the above files. If "Qt5" provides a
separate development package or SDK, be sure it has been installed.
Call Stack (most recent call first):
CMakeLists.txt:686 (include)
Configuring incomplete, errors occurred!
该错误是没有找到Qt5 的 安装路径。
在Everything 搜索 Qt5Config.cmake,即可找到对应路径。
即
目录: E:\Qt\Qt5.12.2.12.2\mingw73_64\lib\cmake\Qt5
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a---- 2019/3/12 17:49 1606 Qt5Config.cmake
-a---- 2019/3/12 17:49 288 Qt5ConfigVersion.cmake
-a---- 2019/3/12 17:49 249 Qt5ModuleLocation.cmake
Qt5_DIR E:\Qt\Qt5.12.2.12.2\mingw73_64\lib\cmake\Qt5
配置完成之后,继续点击Configure
等到 Configuring done。继续配置。
取消
BUILD_opencv_rgbd FALSE
BUILD_opencv_python3 FALSE
BUILD_opencv_python3_building_generator FALSE
BUILD_opencv_rapid FALSE
OPENCV_GENERATE_SETUPVARS FALSE
OPENCV_ENABLE_ALLOCATOR_STATS FALSE
配置完成之后,继续点击Configure
等到 Configuring done.
检测下载
目录: E:\opencv\MinGw_64_build\downloads\xfeatures2d
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a---- 2020/9/27 14:04 14852 boostdesc_bgm.i
-a---- 2020/9/27 14:04 14864 boostdesc_bgm_bi.i
-a---- 2020/9/27 14:04 7832 boostdesc_bgm_hd.i
-a---- 2020/9/27 14:04 138093 boostdesc_binboost_064.i
-a---- 2020/9/27 14:04 275322 boostdesc_binboost_128.i
-a---- 2020/9/27 14:04 549754 boostdesc_binboost_256.i
-a---- 2020/9/27 14:04 426280 boostdesc_lbgm.i
-a---- 2016/3/17 20:49 1257733 vgg_generated_120.i
-a---- 2016/3/17 20:49 773330 vgg_generated_48.i
-a---- 2016/3/17 20:49 915012 vgg_generated_64.i
-a---- 2016/3/17 20:49 1012932 vgg_generated_80.i
目录: E:\opencv\MinGw_64_buildrdparty\ffmpeg
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a---- 2020/9/27 14:03 361 ffmpeg_version.cmake
-a---- 2020/9/27 13:24 21213184 opencv_videoio_ffmpeg.dll
-a---- 2020/9/27 13:31 22100992 opencv_videoio_ffmpeg_64.dll
目录: E:\opencv\MinGw_64_build\testdata\cv\face
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a---- 2020/9/27 13:40 72368157 face_landmark_model.dat
在如上目录查看是都有对应的文件,没有则打开
E:\opencv\MinGw_64_build\CMakeDownloadLog.txt
找到下载路径,下载对应的文件。
管理员权限打开 powershell ,执行如下命令:
PS E:\opencv\MinGw_64_build> E:\Qt\Qt5.12.2\Tools\mingw730_64\bin\mingw32-make.exe -j8
E:\Qt\Qt5.12.2\Tools\mingw730_64\bin\mingw32-make.exe -j8
mingw32-make.exe 路径为对应 Qt mingw 安装路径。
相关错误
E:/Qt/Qt5.12.2/Tools/mingw730_64/lib/gcc/x86_64-w64-mingw32/7.3.0/include/c++/math.h:36:1: error: 'include' does not name a type
include
gcc: error: long: No such file or directory
E:\Qt\Qt5.12.2\Tools\mingw730_64\bin\windres.exe: preprocessing failed.
CMake Warning at cmake/OpenCVGenSetupVars.cmake:54 (message):
CONFIGURATION IS NOT SUPPORTED: validate setupvars script in install
directory
Call Stack (most recent call first):
CMakeLists.txt:968 (include)
Qt工程测试Demo
随便拿了之前的一段代码,图片合并或者说全景图拼接的。
工程目录
目录: D:\8-Project\Qt-Project\opencv_hello
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a---- 2020/9/26 20:49 528828 01.png
-a---- 2020/9/26 20:49 519384 02.png
-a---- 2020/9/26 20:31 183 main.cpp
-a---- 2020/9/28 1:01 1843 mainwindow.cpp
-a---- 2020/9/26 21:02 343 mainwindow.h
-a---- 2020/9/26 21:01 1349 mainwindow.ui
-a---- 2020/9/28 0:52 1319 opencv_hello.pro
-a---- 2020/9/28 0:48 14674 opencv_hello.pro.user
libdir / includepath 配置
在Pro文件内部添加:
INCLUDEPATH += E:\opencv\MinGw_64\include\
INCLUDEPATH += E:\opencv\MinGw_64\include\opencv2\
win32:CONFIG(release, debug|release): LIBS += E:/opencv/MinGw_64/x64/mingw/lib/libopencv_*.a
因为只编译了 release 版本,所以只添加了Release
源码
bool MainWindow::OpencvStitch()
{
//载入图片
cv::Mat fImage = cv::imread("./01.png");
cv::Mat sImage = cv::imread("./02.png");
//图片列表
std::vector
imgs.push_back(fImage);
imgs.push_back(sImage);
//图片生成
cv::Mat outImage;
cv::Ptr
cv::Stitcher::Status status = stitcher->stitch(imgs,outImage);
//图片合成结果
if (status != cv::Stitcher::Status::OK)
{
QString errmsg = "";
if (status == cv::Stitcher::Status::ERR_NEED_MORE_IMGS)
errmsg = "image no overlapping area";
else if (status == cv::Stitcher::Status::ERR_HOMOGRAPHY_EST_FAIL)
errmsg = "image no match ";
else
errmsg = "image parameter processing failed ";
qDebug() << "stitch imgs fail! : " << errmsg;
return false;
}
else
{
//重定向图片
cv::Mat stitchImage = cv::Mat::zeros(fImage.rows, fImage.cols, CV_8UC3);
cv::resize(outImage, stitchImage, stitchImage.size());
//Opencv与Qt格式转换 有默认 BGR-> RGB
cv::Mat qLabelImg;
cv::cvtColor(stitchImage,qLabelImg,cv::COLOR_BGR2RGB);
//生成QImage
QImage qImg = QImage((const unsigned char*)(qLabelImg.data),qLabelImg.cols,qLabelImg.rows,qLabelImg.step,QImage::Format_RGB888);
//QLabel显示图片
ui->label->setPixmap(QPixmap::fromImage(qImg));
ui->label->resize(qImg.size());
qDebug() << ui->label->size();
ui->label->show();
return true;
}
}
合成结果
图像处理 Qt C++
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。