您的位置:首页 > 理论基础

作为计算机视觉的一个初学者,应该如何配置OpenCV以及TensorFlow

2019-03-13 20:00 141 查看

本文是一个菜鸟编写的,所以各个地方我都尽可能去找最简单的方法去解决问题。接下来开始讲解安装配置过程。

1.各个软件的安装顺序

因为我的笔记本显卡还行,所以可以使用cuda版本的TensorFlow,也顺便在vs中配置了cuda。

在最简化安装的过程中,vs要先于cuda安装,cuda要先于TensorFlow安装,所以首先安装vs。

2.安装vs

因为我要用到siftgpu的关系,一开始装了vs2017,结果发现无法配置siftgpu,后来又装了低版本的vs2013,结果还是配置不了,耽误了我大量的时间。但是先装vs的顺序不能变。旧版本的vs可以免费下载了,vs2017部分版本也免费,这里贴了旧版本的链接,下载请点击。

https://visualstudio.microsoft.com/zh-hans/vs/older-downloads/

我之前用的vs2013,这里也下载了该版本

vs2013版本的安装比较简单,直接选择安装目录后,一路下一步直到完成即可。

vs到这里就安装完成了,也可以试试编写一个helloworld小程序验证一下。

3.配置opencv

我下载使用的版本为3.4.1,当然,你也可以选择其他版本,以3.4.1为例,本文讲解OpenCV的配置过程

首先去OpenCV的官网下载3.4.1,链接在这里

但是你可以发现,下载文件的后缀为vc14_vc15,代表它是适用于这两个版本的,vc14代表vs2015,vc15代表的是vs2017,如果你恰巧使用的是这两个版本的vs,那么你可以直接配置,不需要经过cmake过程。但是如果你也需要contrib的模块,contrib部分也需要使用cmake进行编译

这里贴一个比较好的配置链接

https://www.geek-share.com/detail/2750142093.html

该链接最大的问题是附加依赖项的版本不对,我编写了一个提取附加依赖项名称的教程,在这里:

https://mp.csdn.net/postedit/88375969

按教程配置完后,可以来一个小程序试一下自己的opencv是否配置成功

以下代码为一个跟踪代码,在第一帧图像中按住鼠标左键选中目标后,右键点击确认,即可实现目标跟踪的功能。

[code]#include <opencv2/opencv.hpp>
#include <opencv2/video.hpp>
#include <opencv2/tracking.hpp>
#include <opencv2/tracking/tracker.hpp>
using namespace cv;
void draw_rectangle(int event, int x, int y, int flags, void*);
Mat firstFrame;
Point previousPoint, currentPoint;
Rect2d bbox;
int main(int argc, char *argv[])
{
//启用摄像头进行跟踪
VideoCapture capture(0);
Mat frame;
capture >> frame;
//使用事先录好的视频进行检验
//VideoCapture capture;
//frame = capture.open("E:\\imagelib\\1.avi");
if (!capture.isOpened())
{
printf("can not open ...\n");
return -1;
} //获取视频的第一帧,并框选目标
capture.read(firstFrame);
if (!firstFrame.empty())
{
namedWindow("output", WINDOW_AUTOSIZE);
imshow("output", firstFrame);
setMouseCallback("output", draw_rectangle, 0);
waitKey();
}

//使用不同跟踪算法进行跟踪
//Ptr<TrackerMIL> tracker= TrackerMIL::create();
//Ptr<TrackerTLD> tracker= TrackerTLD::create();
Ptr<TrackerKCF> tracker = TrackerKCF::create();
//Ptr<TrackerMedianFlow> tracker = TrackerMedianFlow::create();
//Ptr<TrackerBoosting> tracker= TrackerBoosting::create();

capture.read(frame);
tracker->init(frame, bbox);
namedWindow("output", WINDOW_AUTOSIZE);
while (capture.read(frame))
{
tracker->update(frame, bbox);
rectangle(frame, bbox, Scalar(255, 0, 0), 2, 1);
imshow("output", frame);
if (waitKey(20) == 'q') return 0;
}
capture.release();
destroyWindow("output"); return 0;
} //框选目标
void draw_rectangle(int event, int x, int y, int flags, void*)
{
if (event == EVENT_LBUTTONDOWN) { previousPoint = Point(x, y); }
else if (event == EVENT_MOUSEMOVE && (flags&EVENT_FLAG_LBUTTON))
{
Mat tmp; firstFrame.copyTo(tmp);
currentPoint = Point(x, y);
rectangle(tmp, previousPoint, currentPoint, Scalar(0, 255, 0, 0), 1, 8, 0);
imshow("output", tmp);
}
else if (event == EVENT_LBUTTONUP)
{
bbox.x = previousPoint.x;
bbox.y = previousPoint.y;
bbox.width = abs(previousPoint.x - currentPoint.x);
bbox.height = abs(previousPoint.y - currentPoint.y);
}
else if (event == EVENT_RBUTTONUP) {
destroyWindow("output");
}
}

 

4.anaconda与tensorflow的安装

anaconda以及tensorflow的配置,这里有链接,讲解的也比较详细。

https://www.geek-share.com/detail/2721576526.html

需要注意的是,anaconda与python版本有对应关系;tensorflow对cuda和python版本也有对应关系。我的安装为

anaconda5.2+python3.6+cuda9.7+cudnn7.0+tensorflow1.10.0

按照链接安装完成后,可能会出现一些问题,报错如下:

问题描述,报错如下:/home/zhongjia/anaconda3/lib/python3.6/site-packages/h5py/__init__.py:36: FutureWarning: Conversion of the second argument of issubdtype from `float` to `np.floating` is deprecated. In future, it will be treated as `np.float64 == np.dtype(float).type`.from ._conv import register_converters as _register_converters

问题来源于h5py版本的问题,重装新版本即可

解决方法为

pip uninstall h5py

pip install hypy=2.8.0rc1

参考链接:https://blog.csdn.net/zhuiyuanzhongjia/article/details/80170412

5.python版本opencv的配置

这部分参考以下链接

https://www.geek-share.com/detail/2724149842.html

安装完成后可能会出现numpy的版本冲突,opencv与tensorflow的numpy版本可能不一致

这时卸载numpy,安装同时适合两个软件的numpy版本即可

至此TensorFlow与OpenCV就配置完了,可以开心地开始学习了

 

 

 

 

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐