前言:本文是接着上一篇(https://blog.csdn.net/Smile_h_ahaha/article/details/102769455)继续说的,在这里简单说明一下。
上一篇的主要内容是:第0部分(教你自行搭配各软件的安装版本),第一部分(小白级别的VS+OpenCV运行示例,与第二部分无关),第二部分(新手级别的VS+OpenCV+Cmake生成自建库+小红球图像检测示例,与第三部分相关)。
本文的主要内容是:第三部分(入门级VS+OpenCV+QT,不带QT GUI),第四部分(入门级VS+OpenCV+QT,带QT GUI)
下一篇的主要内容是:第五部分(指尖识别示例与解析,就是数你有多少个手指头,这是做指纹识别或者动态捕捉方向的入门级程序)。
进一步说明:(手快党请看完下文介绍,再安装)
1、上一篇的第二部分非常重要,如果你想在第三部分中成功,那么在第二部分中的操作,你必须保证都完成。如果你没照着我的做,请回去看一下,对比检查。不要上来就跟着本文做。如果你确定第二部分的操作都完成了,那么请看下去。
2、第三部分和第四部分都是在第二部分的基础上的延伸,这两个部分是可选的(二选一安装)。网上大多数的教程并不给你详细说明这俩的区别。实际上区别就是有没有QT GUI。
比如说你平时只需要分析静态图像,那么不带GUI的就够用,你可以选择跟着第三部分一起做。
比如说你要分析动态图像捕捉,要用摄像头,要检测实时物体(上一篇的红球检测)等等复杂操作,那么你就需要带QT GUI的。
小伙伴们此时的内心仿佛在说:“我还差你那点流量和储存空间?没说的,上QT GUI,妥妥的”!
哈哈,好吧,但是我还是把不带GUI的安装方法也放在这里,给大家一种很全面很细致的感觉(我是暖男,不用谢XD)。
3、我也推荐你选择带QT GUI的,你可以看看第三部分的解说,或者直接跳到第四部分。
第三部分:配置QT (不带QT GUI的安装教程)
第一步,打开QT creater选择新建一个项目:
然后:
然后:文件夹的名字就随便起一个,我就想运行我那红球检测的程序,于是就叫RedBall了。保存位置自己设置(路径别带中文)
然后build system,选择qmake
然后点击详情(details)
然后,取消选中选项,直到仅剩一个为止,即使用MSVC 201X进行32位编译的调试版本
因为我们不带GUI了,大多数都可以不勾选,仅剩下Debug的那个,同时还要把它的路径改成你刚才命名的那个。
比如,看往上数第三张图片所示的“创建路径”(C:\Users\Administrator\Desktop\QTtext),你把debug的默认路径改到与“创建路径”相同的文件夹中。
必须这样做,不然系统会混淆,找不到目录。
虽然此时此刻QT的RedBall(我们刚命名的文件夹名字)还没生成,但你还是要把debug的路径设置成:
C:\Users\Administrator\Desktop\QTtext\RedBall
下一个界面选None,然后"完成"。
第二步:
转到“ main.cpp”,复制/粘贴代码
// 保存名为RedBall.cpp的文件
//没有QT GUI的只能做静态图片分析
#include <opencv2/opencv.hpp> //我们的opencv/Include
using namespace cv;
int main()
{
// 读入一张图片,写你自己的保存路径
//要把图片保存在你的项目的统一文件夹中
Mat img = imread("C:\Users\Administrator\Desktop\QTtext\RedBall\1.jpg");
namedWindow("666");// 创建一个名为 "666"的窗口
imshow("图片名字", img);// 在窗口中显示图片名字
waitKey(10000);// 等待10 s后窗口自动关闭
}
由于我们还没告知Qt Creator,OpenCV的位置,此时Qt Creator还不能正常调用OpenCV库,所以你会看到include那有红色下划线
在你电脑中,导航到你的OpenCV 自建库的目录位置,例如“ D:\OpenCV\V3.0\mybuild\lib\Debug”,然后列出所有以“ d.lib”结尾的文件名(不同的OpenCV版本,有不同的文件名,例如OpenCV3.1.0 就是310了)。
接下来在Qt Creator中,转到您的.pro文件(例如“ RedBall.pro”),此时它是这样的:
在第七行那添加以下内容,并确保根据你的OpenCV版本更改lib目录中存在的库:
########################################################################
# add these to the end of your .pro file, this is so Qt knows about the location of the include and lib directories
# in Qt .pro files, begin a line with a pound character '#' to enter a comment
# note that for the double backslashes, the second is an escape character so the first is seen by Qt as a backslash
# the single backslashes at the end of each line (except for the last line) are line continuation characters
INCLUDEPATH += D:\\OpenCV\\V3.0\\opencv\\build\\include
LIBS += -LD:\\OpenCV\\V3.0\\mybuild\\lib\\Debug \
-lopencv_imgcodecs300d \
-lopencv_imgproc300d \
-lopencv_ml300d \
-lopencv_objdetect300d \
-lopencv_photo300d \
-lopencv_shape300d \
-lopencv_stitching300d \
-lopencv_superres300d \
-lopencv_ts300d \
-lopencv_video300d \
-lopencv_videoio300d \
-lopencv_videostab300d \
-lopencv_calib3d300d \
-lopencv_core300d \
-lopencv_features2d300d \
-lopencv_flann300d \
-lopencv_hal300d \
-lopencv_highgui300d
# Note: it is recommended to add a blank line at the end of your .pro file ############################
因为我这个是OpenCV3.0.0版本的,所以是300,别的版本的话,把它们复制粘贴到txt文本中自己替换300值就好了。
第三步:
然后你就可以运行了。通过单击左下角的相应图标来运行程序,可以选择“运行”(绿色箭头)或“开始调试”(绿色箭头,上面有一个瓢虫的那个)。
如果在尝试运行该程序时收到异常或不合逻辑的错误,
例如“无法打开包含文件:'opencv2 / core / core.hpp':无此类文件或目录”,
并且可以确定已正确设置了所有内容,
例如 在第一个故障排除步骤中,选择“构建”->“全部清除”,然后选择“构建”->“运行qmake”,然后尝试再次运行程序。
嗯,完成了,简单吧。
第四部分(配置QT,带QT GUI)
第一步:打开QT creator 新建一个项目
在 "New Project" 页面, 选择 "Application" 和"Qt Widgets Application", 然后 "Choose . . ."
在 "Introduction and Project Location" 界面, 随便起个文件夹名, 例如 "RedballGUI"
然后这样做:(这步和第三部分的新建的选择一样,所以我不再解释一遍了,可以往上翻看看解释)
在“类信息”屏幕上,选择“类名:”作为首选,例如设置成“ redballgui”,注意这会为你直接全部设置“头文件:”,“源文件:”和“表单文件:”的名称。 不建议把“标题文件:”,“源文件:”或“表单文件:”的名称改成不一样的名字。
确保“基类:”设置为QMainWindow
然后下一步,然后
复制粘贴如下代码到“redballgui.cpp”中
// 保存名为redballgui.cpp的文件
#include<opencv2/core/core.hpp>
#include<opencv2/highgui/highgui.hpp>
#include<opencv2/imgproc/imgproc.hpp>
#include<iostream>
///
int main() {
cv::VideoCapture capWebcam(0); // declare a VideoCapture object and associate to webcam, 0 => use 1st webcam
if (capWebcam.isOpened() == false) { // check if VideoCapture object was associated to webcam successfully
std::cout << "error: capWebcam not accessed successfully\n\n"; // if not, print error message to std out
return(0); // and exit program
}
cv::Mat imgOriginal; // input image
cv::Mat imgHSV;
cv::Mat imgThreshLow;
cv::Mat imgThreshHigh;
cv::Mat imgThresh;
std::vector<cv::Vec3f> v3fCircles; // 3 element vector of floats, this will be the pass by reference output of HoughCircles()
char charCheckForEscKey = 0;
while (charCheckForEscKey != 27 && capWebcam.isOpened()) { // until the Esc key is pressed or webcam connection is lost
bool blnFrameReadSuccessfully = capWebcam.read(imgOriginal); // get next frame
if (!blnFrameReadSuccessfully || imgOriginal.empty()) { // if frame not read successfully
std::cout << "error: frame not read from webcam\n"; // print error message to std out
break; // and jump out of while loop
}
cv::cvtColor(imgOriginal, imgHSV, CV_BGR2HSV);
cv::inRange(imgHSV, cv::Scalar(0, 155, 155), cv::Scalar(18, 255, 255), imgThreshLow);
cv::inRange(imgHSV, cv::Scalar(165, 155, 155), cv::Scalar(179, 255, 255), imgThreshHigh);
cv::add(imgThreshLow, imgThreshHigh, imgThresh);
cv::GaussianBlur(imgThresh, imgThresh, cv::Size(3, 3), 0);
cv::Mat structuringElement = cv::getStructuringElement(cv::MORPH_RECT, cv::Size(3, 3));
cv::dilate(imgThresh, imgThresh, structuringElement);
cv::erode(imgThresh, imgThresh, structuringElement);
// fill circles vector with all circles in processed image
cv::HoughCircles(imgThresh, // input image
v3fCircles, // function output (must be a standard template library vector
CV_HOUGH_GRADIENT, // two-pass algorithm for detecting circles, this is the only choice available
2, // size of image / this value = "accumulator resolution", i.e. accum res = size of image / 2
imgThresh.rows / 4, // min distance in pixels between the centers of the detected circles
100, // high threshold of Canny edge detector (called by cvHoughCircles)
50, // low threshold of Canny edge detector (set at 1/2 previous value)
10, // min circle radius (any circles with smaller radius will not be returned)
400); // max circle radius (any circles with larger radius will not be returned)
for (int i = 0; i < v3fCircles.size(); i++) { // for each circle . . .
// show ball position x, y, and radius to command line
std::cout << "ball position x = " << v3fCircles[i][0] // x position of center point of circle
<< ", y = " << v3fCircles[i][1] // y position of center point of circle
<< ", radius = " << v3fCircles[i][2] << "\n"; // radius of circle
// draw small green circle at center of detected object
cv::circle(imgOriginal, // draw on original image
cv::Point((int)v3fCircles[i][0], (int)v3fCircles[i][1]), // center point of circle
3, // radius of circle in pixels
cv::Scalar(0, 255, 0), // draw pure green (remember, its BGR, not RGB)
CV_FILLED); // thickness, fill in the circle
// draw red circle around the detected object
cv::circle(imgOriginal, // draw on original image
cv::Point((int)v3fCircles[i][0], (int)v3fCircles[i][1]), // center point of circle
(int)v3fCircles[i][2], // radius of circle in pixels
cv::Scalar(0, 0, 255), // draw pure red (remember, its BGR, not RGB)
3); // thickness of circle in pixels
} // end for
// declare windows
cv::namedWindow("imgOriginal", CV_WINDOW_AUTOSIZE); // note: you can use CV_WINDOW_NORMAL which allows resizing the window
cv::namedWindow("imgThresh", CV_WINDOW_AUTOSIZE); // or CV_WINDOW_AUTOSIZE for a fixed size window matching the resolution of the image
// CV_WINDOW_AUTOSIZE is the default
cv::imshow("imgOriginal", imgOriginal); // show windows
cv::imshow("imgThresh", imgThresh);
charCheckForEscKey = cv::waitKey(1); // delay (in ms) and get key press, if any
} // end while
return(0);
}
————————————————
版权声明:本文为CSDN博主「Smile_h_ahaha」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/Smile_h_ahaha/article/details/102769455
然后在电脑中找到你下载的OpenCV的保存位置,导航的OpenCV lib目录,
例如“ D:\OpenCV\V3.0\mybuild\lib\Debug”,然后列出所有以“ d.lib”结尾的文件名(不同的OpenCV版本,有不同的文件名,例如OpenCV3.1.0 就是310了)。
接下来在Qt Creator中,转到您的.pro文件(例如“ redballgui.pro”),此时它是这样的:
在上面所示的程序的第32行,添加(注意下文中的路径和lib版本号,要改成你自己的):
########################################################################
# add these to the end of your .pro file, this is so Qt knows about the location of the include and lib directories
# in Qt .pro files, begin a line with a pound character '#' to enter a comment
# note that for the double backslashes, the second is an escape character so the first is seen by Qt as a backslash
# the single backslashes at the end of each line (except for the last line) are line continuation characters
INCLUDEPATH += D:\\OpenCV\\V3.0\\opencv\\build\\include
LIBS += -LD:\\OpenCV\\V3.0\\mybuild\\lib\\Debug \
-lopencv_imgcodecs300d \
-lopencv_imgproc300d \
-lopencv_ml300d \
-lopencv_objdetect300d \
-lopencv_photo300d \
-lopencv_shape300d \
-lopencv_stitching300d \
-lopencv_superres300d \
-lopencv_ts300d \
-lopencv_video300d \
-lopencv_videoio300d \
-lopencv_videostab300d \
-lopencv_calib3d300d \
-lopencv_core300d \
-lopencv_features2d300d \
-lopencv_flann300d \
-lopencv_hal300d \
-lopencv_highgui300d
# Note: it is recommended to add a blank line at the end of your .pro file ############################
第二步:
双击表单文件,例如“ redballgui.ui”,这将打开表单编辑器
然后你就看见GUI编辑界面了:
如果这是你第一次使用Qt,则可以通过以下方式更改表单设计选项:
工具->选项->设计师(在左下角)
我建议将网格更改为比默认值小点的值,例如4 x 4,
到这里QT的配置都说完了。
第四部分没有说怎么设计GUI(图形操作界面)。放在下次再给大家介绍吧。在这里的说的话显着又密密麻麻了。
那些熟练了的大佬他们曾经也是萌新,先当孙子后当爷么,学习新知识总是一个痛苦的过程。我也是刚起步。
如果你也在学图像处理,想用树莓派做智能之家,做动态识别之类的,可以在下方评论留言,我们一起学习讨论。
从2020年3月到9月我会一直搞智能之家,图像处理这块的东西,欢迎大家交流讨论,我们互相学习。
苦海无涯,兴趣作舟,最后祝大家都学成大佬,牛气冲天!
哈哈,还没完呢,在图像处理的第三篇博文,我将给大家解释一个手指指尖识别的算法,附赠代码(干货!)。
YouTube安装视频(https://www.youtube.com/watch?v=akAAAvGyLn0)
WiKi百科参考链接(https://wiki.qt.io/How_to_setup_Qt_and_openCV_on_Windows)
OpenCV官方用Cmake自建库(https://docs.opencv.org/2.4/doc/tutorials/introduction/windows_install/windows_install.html)