opencv3.2 with cuda3 无cuda库有什么影响

不要去欺骗别人,因为你能骗到的人,都是相信你的人。
Table of Contents
1 OpenCV2.2 在 Ubuntu 11.04 下编译安装错误及解决办法
1.1 错误1:undefined reference to `cvCreateCameraCapture_V4L(int)'
在 Ubuntu 11.04 下编译 OpenCV2.2,出现了如下错误:
Linking CXX static library ../../lib/libopencv_haartraining_engine.a
[ 80%] Built target opencv_haartraining_engine
Scanning dependencies of target opencv_createsamples
[ 80%] Building CXX object modules/haartraining/CMakeFiles/opencv_createsamples.dir/createsamples.o
Linking CXX executable ../../bin/opencv_createsamples
../../lib/libopencv_highgui.so.2.2.0: undefined reference to `cvCreateCameraCapture_V4L(int)'
collect2: ld returned 1 exit status
make[2]: *** [bin/opencv_createsamples] Error 1
make[1]: *** [modules/haartraining/CMakeFiles/opencv_createsamples.dir/all] Error 2
make: *** [all] Error 2
其中关键的错误提示在于:
undefined reference to `cvCreateCameraCapture_V4L(int)'
在 Ubuntu 11.04 下,网上的资料解释可以有两种方法解决这个问题:
(1) make ubuntu 11.04 compile with libv4l,。
(2) fixed "undefined cvCreateCameraCapture_V4L" linker errors on some linux distros (thanks to miguelinux for the patch),。
这两个解决办法里,我试验了第一种方法,的确是可以解决上述问题;不过我没测试第二种方法。
知其然还要知其所以然,根本的原因在于:
In some linux distros where V4L2 is enabled, CMake does not define the HAVE_CAMV4L macro, but the macro HAVE_CAMV4L2 is defined.
When the highgui.so is building and linked with an other program, a Link error undefined reference to cvCreateCameraCapture_V4L is detected, and the library is not finished. The compilation terminated at [ 83%] Building.
中文翻译为:
在某些 Linux 发行版本中启用了V4L2,CMake并没有定义 HAVE_CAMV4L 这个宏定义,但是定义了 HAVE_CAMV4L2 这个宏定义。
当编译 highgui.so 并需要和其他程序链接时,一个错误 undefined reference to cvCreateCameraCapture_V4L 就会产生。
详细的解释可以参看
1.2 错误2: linux/videodev.h: No such file or directory
我今天在 Ubuntu 11.04 上编译 OpenCV2.2,又碰到这样一个问题,在安装好 Utuntu-11.04-server 以及相应的依赖包后,再用 cmake, make 编译 OpenCV2.2 出错,提示如下错误:
/home/water/OpenCV/OpenCV-2.2.0/modules/highgui/src/cap_v4l.cpp:217:28: fatal error: linux/videodev.h: No such file or directory
compilation terminated.
make[2]: *** [modules/highgui/CMakeFiles/opencv_highgui.dir/src/cap_v4l.o] Error 1
make[1]: *** [modules/highgui/CMakeFiles/opencv_highgui.dir/all] Error 2
make: *** [all] Error 2
这个问题,由于 ,因此需要自己安装 libv4L 库的头文件,尤其是 lib4l 包,该包是用命令
$ sudo apt-get install libv4l-dev
来安装的,注意这里是 libv4L 而不是 libv41;
可是,现在你继续安装,依然会出现上述错误。这是由于这样安装的 libv4L 库把头文件名称改为
/usr/include/libv4l1-videodev.h
等。知道了原因,解决办法也很简单,只需要把出错这行代码的 #include 头文件改掉,即把文件
/modules/highgui/src/cap_v4l.cpp
第 217 行的
#include &linux/videodev.h&
代码,改为下面这行
#include &libv4l1-videodev.h&
这行代码。
这样,就可以顺利进行编译了。
1.3 OpenCV2.2 为 Ubuntu11.04 修订后的源代码
为了方便,可以使用针对 Ubuntu11.04 修改打包后的 OpenCV2.2.0 代码:
2 搜索本站
Please enable JavaScript to view the
Author: Water Lin
version 7.9.3f with
version 24opencv(2)
在opencv中,已经嵌入了opencl运行的方式,通过使用对象,会自动在支持的设备上使用运算,在不支持的设备仍然使用运算,这样就避免了程序运行失败,而且统一了接口。
一般正常基于CPU的读写视频一帧图像代码如下:
cv::Mat inMat, outM
vidInput && inM
cv::cvtColor(inMat, outMat, cv::COLOR_RGB2GRAY);
vidOutput && outM基于OpenCL的GPU方式读写视频一帧图像代码如下:
cv::Mat inMat, outM
vidInput && inM
cv::ocl::oclMat inOclMat(inMat);
cv::ocl::oclMat outOclM
cv::ocl::cvtColor(inOclMat, outOclMat, cv::COLOR_RGB2GRAY);
outMat = outOclM
vidOutput && outM
上述代码通过添加ocl前缀空间实现OpenCL支持设备的GPU运算能力提高。但是上述代码在不支持OpenCL的平台上还会运行失败,使用起来及其不方便。对开发者来说不是统一API和底层透明。
而如果在OpenCV3中使用UMat,则如下:
cv::UMat inMat, outM
vidInput && inM
cv::cvtColor(inMat, outMat, cv::COLOR_RGB2GRAY);
vidOutput && outM这样就无需像OpenCV2中那样通过显式声明的调用方式。很明显UMat与Mat极其类似。而且两者之间是可以相互转换的。
从UMat中获取Mat对象使用UMat的get方法UMat::getMat(int access_flags)支持的FLAG如下:
ACCESS_READACCESS_WRITEACCESS_RWACCESS_MASKACCESS_FAST&
最常用的就是读写,注意当使用这种方式的时候UMat对象将会被LOCK直到CPU使用获取Mat对象完成操作,销毁临时Mat对象之后,UMat才可以再被使用。
反过来也是一样,但是有的时候我用会出问题
所以我会直接使用cv::CopyTo(),可能这样做会减少错误,但不知道会有什么影响
cv::UMat aM
cv::Mat bM
bMat.CopyTo(aMat);
aMat.CopyTo(bMat);
UMat不能提取其中某个特定元素的值,毕竟已经存到gpu中了,提取方法是先转换成Mat,再提取,下面提供一种提取方法
UMat test_
test_umat.getMat(ACCESS_READ).at&uchar&(row, column);注意,不要过多地进行Mat和UMat之间地转化,因为内存显存之间传递信息耗时很大,这是像我这种初学者,很容易犯的错误。
之后我打算用一段程序测试OpenCL和CUDA的性能,程序如下:
#include &iostream&
#include &opencv2/opencv.hpp&
#include &opencv2/core/ocl.hpp&
#include &opencv2/core/cuda.hpp&
#include &opencv2/cudaobjdetect.hpp&
#include &opencv2/cudaimgproc.hpp&
int main(int argc, char**argv) {
std::cout && &OpenCV version=& && std::hex && CV_VERSION && std::dec && std::
cv::UMat uframe, uFrameG
cv::cuda::GpuMat image_gpu, image_gpu_
cv::VideoCapture capture(&path_to_the_video&);
bool useOpenCL = (argc == 2);
std::cout && &Use OpenCL=& && useOpenCL && std::
cv::ocl::setUseOpenCL(useOpenCL);
bool useCuda = (argc == 3);
std::cout && &Use CUDA=& && useCuda && std::
cv::Ptr&cv::CascadeClassifier& cascade = cv::makePtr&cv::CascadeClassifier&(&data/lbpcascades/lbpcascade_frontalface_at.xml&);
cv::Ptr&cv::cuda::CascadeClassifier& cascade_gpu = cv::cuda::CascadeClassifier::create(&data/lbpcascades/lbpcascade_frontalface_at.xml&);
double time = 0.0;
int nb = 0;
if(capture.isOpened()) {
capture &&
if(frame.empty() || nb &= 1000) {
std::vector&cv::Rect&
double t = 0.0;
if(!useCuda) {
t = (double) cv::getTickCount();
frame.copyTo(uframe);
cv::cvtColor(uframe, uFrameGray, CV_BGR2GRAY);
cascade-&detectMultiScale(uFrameGray, faces);
t = ((double) cv::getTickCount() - t) / cv::getTickFrequency();
t = (double) cv::getTickCount();
image_gpu.upload(frame);
cv::cuda::cvtColor(image_gpu, image_gpu_gray, CV_BGR2GRAY);
cv::cuda::GpuM
cascade_gpu-&detectMultiScale(image_gpu_gray, objbuf);
cascade_gpu-&convert(objbuf, faces);
t = ((double) cv::getTickCount() - t) / cv::getTickFrequency();
for(std::vector&cv::Rect&::const_iterator it = faces.begin(); it != faces.end(); ++it) {
cv::rectangle(frame, *it, cv::Scalar(0,0,255));
ss && &FPS=& && (nb / time);
cv::putText(frame, ss.str(), cv::Point(30, 30), cv::FONT_HERSHEY_SIMPLEX, 1.0, cv::Scalar(0,0,255));
cv::imshow(&Frame&, frame);
char c = cv::waitKey(30);
if(c == 27) {
std::cout && &Mean time=& && (time / nb) && & s& && & ; Mean FPS=& && (nb / time) && & ; nb=& && nb && std::
system(&pause&);
lbpcascade_frontalface_at.xml文件可以百度搜索就能下载到,是一个级联分类器训练好的模型,用于识别人脸正面,很常用。具体资料可以参考http://blog.csdn.net/yang_xian521/article/details/6973667
cv::VideoCapture capture(&path_to_the_video&);
这里填入你想检测的视频的地址,时长最好在十几秒,情况视你的处理器情况所定。
cv::Ptr&cv::CascadeClassifier& cascade = cv::makePtr&cv::CascadeClassifier&(&data/lbpcascades/lbpcascade_frontalface_at.xml&);
cv::Ptr&cv::cuda::CascadeClassifier& cascade_gpu = cv::cuda::CascadeClassifier::create(&data/lbpcascades/lbpcascade_frontalface_at.xml&);
两处填入下载好的xml文件的地址即可运行
运行方式为
g++ -o 文件名 文件名.cpp&`pkg-config
opencv --cflags --libs`
此处要链接上opencv的库,注意不要少了`这个符号,它在键盘数字1旁边。
如果额外输入一个参数,程序将利用opencl进行加速,如果额外输入两个,程序将利用cuda进行加速,没有额外参数,将正常运行
我的结果很意外,opencl基本没有加速效果,而cuda加速效果很明显,不知道原因在哪里
&&相关文章推荐
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:4338次
排名:千里之外
原创:18篇
转载:11篇
(1)(1)(5)(8)(9)(6)
(window.slotbydup = window.slotbydup || []).push({
id: '4740887',
container: s,
size: '250,250',
display: 'inlay-fix'[广告]投资优惠入口:
对文章打分
计算机视觉标准OpenCV支持CUDA GPU加速
(window.slotbydup=window.slotbydup || []).push({
id: '945055',
container: s,
size: '300,250',
display: 'inlay-fix'
(window.slotbydup=window.slotbydup || []).push({
id: '649316',
container: s,
size: '300,250',
display: 'inlay-fix'
阅读 (27873) 评论 (87)
阅读 (21774) 评论 (90)
阅读 (21550) 评论 (3)
阅读 (122418) 评论 (125)
阅读 (50134) 评论 (100)
阅读 (46160) 评论 (36)
Advertisment ad adsense googlesopencv3.2.0中Cuda版本的模板匹配和通道转换_天使、焦_新浪博客
opencv3.2.0中Cuda版本的模板匹配和通道转换
opencv2.0以上版本gpu模式的模板匹配和通道转换直接用gpu命名空间就可以调用;opencv3.2.0中的gpu模块有比较大的变化,原来的gpu换成了现在的cuda,通道转换可以用cuda命名空间直接使用,模板匹配则不同。代码如下,测试了cpu和gpu模式下,两个函数所用的时间:
​#include
#include "opencv2/opencv.hpp"
#define GPU&
int main()
int n = 10000;
#ifdef GPU
int num_devices = getCudaEnabledDeviceCount();
cout && "可使用的设备个数为:"
&& num_devices
setDevice(0);
Mat img = imread("G:/test3.jpg");
//Mat img = imread("RightCode.bmp",0);
Mat t = imread("G:/8.jpg", 0);
#ifdef GPU
GpuMat gpu_
GpuMat gpu_t;
gpu_img.upload(img);
gpu_t.upload(t);
uchar d = *(gpu_img.data);
Ptr alg = createTemplateMatching(CV_8U,
CV_TM_CCOEFF_NORMED);
GpuMat gpu_
tm.start();
#ifdef GPU
for (int i = 0; i & i++)
cuda::cvtColor(gpu_img, gpu_dst, CV_BGR2GRAY);
//alg-&match(gpu_img, gpu_t, gpu_dst);
for (int i = 0; i & i++)
cv::cvtColor(img, dst, CV_BGR2GRAY);
//matchTemplate(img, t, dst, CV_TM_CCOEFF_NORMED);
tm.stop();
cout && tm.getTimeMilli()/n
#ifdef GPU
gpu_dst.download(dst);
imshow("dst", dst);
waitKey();
博客等级:
博客积分:0
博客访问:1,928
关注人气:0
荣誉徽章:

我要回帖

更多关于 opencv3.2 cuda版本 的文章

 

随机推荐