opencv的一个小例子
打开图片或者摄像头:
直接上源码:
#include <string.h> #include "opencv2/core.hpp" #include "opencv2/videoio.hpp" #include "opencv2/opencv.hpp" void testMat() { cv::Mat img =cv::imread("000111.png");//声明Mat变量,并读取图片,imread中需要传入图片的绝对路径 cv::imshow("image",img);//在窗口中显示载入的图片 cv::waitKey(5000);//等待按键 std::cout << "testMat() ......... \n"; } void testVideo() { std::cout << "testVideo() ......... \n"; std::string ipc_addr("rtsp://xxxxxxxxx"); // 也可以直接为0 表示打开摄像头 cv::VideoCapture cap(ipc_addr); if (!cap.isOpened()) { std::cout << "通用摄像头打开失败('" << ipc_addr.c_str() << "'), 请检查..." << std::endl; return; } cv::namedWindow("Video", 1); while (1) { cv::Mat frame; cap >> frame; // get a new frame from camera cv::imshow("Video", frame); if (cv::waitKey(30) >= 0) break; } } int main() { std::cout << "main() +++++++++ \n"; testMat(); testVideo(); std::cout << "main() --------- \n"; return 0; }
编译: build.sh
若是opencv4以下的版本:
g++ main.cpp -o test `pkg-config opencv --cflags --libs`
opencv4以上:
g++ main.cpp -o test `pkg-config opencv4 --cflags --libs`
用opencv4时,用到了cv::VideoCapture就会出错。编译遇到了下列问题:
系统:Ubuntu 20.04
Opencv版本:4.2.0
CPU架构:ARM64 3588
dadao@ubuntu:~/Workspace/youngtest/opencvtest$ ./build.sh
/usr/bin/ld: /lib/aarch64-linux-gnu/libavutil.so.56: undefined reference to `clReleaseMemObject@OPENCL_1.0'
/usr/bin/ld: /lib/aarch64-linux-gnu/libavutil.so.56: undefined reference to `clGetPlatformInfo@OPENCL_1.0'
/usr/bin/ld: /lib/aarch64-linux-gnu/libavutil.so.56: undefined reference to `clCreateCommandQueue@OPENCL_1.0'
/usr/bin/ld: /lib/aarch64-linux-gnu/libavutil.so.56: undefined reference to `clWaitForEvents@OPENCL_1.0'
/usr/bin/ld: /lib/aarch64-linux-gnu/libavutil.so.56: undefined reference to `clEnqueueReadImage@OPENCL_1.0'
/usr/bin/ld: /lib/aarch64-linux-gnu/libavutil.so.56: undefined reference to `clGetSupportedImageFormats@OPENCL_1.0'
/usr/bin/ld: /lib/aarch64-linux-gnu/libavutil.so.56: undefined reference to `clGetDeviceInfo@OPENCL_1.0'
/usr/bin/ld: /lib/aarch64-linux-gnu/libavutil.so.56: undefined reference to `clReleaseContext@OPENCL_1.0'
/usr/bin/ld: /lib/aarch64-linux-gnu/libavutil.so.56: undefined reference to `clGetPlatformIDs@OPENCL_1.0'
/usr/bin/ld: /lib/aarch64-linux-gnu/libavutil.so.56: undefined reference to `clReleaseCommandQueue@OPENCL_1.0'
/usr/bin/ld: /lib/aarch64-linux-gnu/libavutil.so.56: undefined reference to `clCreateContext@OPENCL_1.0'
/usr/bin/ld: /lib/aarch64-linux-gnu/libavutil.so.56: undefined reference to `clEnqueueUnmapMemObject@OPENCL_1.0'
/usr/bin/ld: /lib/aarch64-linux-gnu/libavutil.so.56: undefined reference to `clCreateImage@OPENCL_1.2'
/usr/bin/ld: /lib/aarch64-linux-gnu/libavutil.so.56: undefined reference to `clEnqueueWriteImage@OPENCL_1.0'
/usr/bin/ld: /lib/aarch64-linux-gnu/libavutil.so.56: undefined reference to `clEnqueueMapImage@OPENCL_1.0'
/usr/bin/ld: /lib/aarch64-linux-gnu/libavutil.so.56: undefined reference to `clReleaseEvent@OPENCL_1.0'
/usr/bin/ld: /lib/aarch64-linux-gnu/libavutil.so.56: undefined reference to `clGetContextInfo@OPENCL_1.0'
/usr/bin/ld: /lib/aarch64-linux-gnu/libavutil.so.56: undefined reference to `clGetExtensionFunctionAddressForPlatform@OPENCL_1.2'
/usr/bin/ld: /lib/aarch64-linux-gnu/libavutil.so.56: undefined reference to `clGetDeviceIDs@OPENCL_1.0'
/usr/bin/ld: /lib/aarch64-linux-gnu/libavutil.so.56: undefined reference to `clRetainCommandQueue@OPENCL_1.0'
collect2: error: ld returned 1 exit status
dadao@ubuntu:~/Workspace/youngtest/opencvtest$
解决方法:安装opencl
dadao@ubuntu:~/Workspace/youngtest/opencvtest$ sudo apt-get install opencl-headers ocl-icd-opencl-dev
Reading package lists... Done
Building dependency tree
Reading state information... Done
Some packages could not be installed. This may mean that you have
requested an impossible situation or if you are using the unstable
distribution that some required packages have not yet been created
or been moved out of Incoming.
The following information may help to resolve the situation:The following packages have unmet dependencies:
ocl-icd-opencl-dev : Depends: ocl-icd-libopencl1 (= 2.2.11-1ubuntu1) but 2.2.11-2ubuntu1 is to be installed
E: Unable to correct problems, you have held broken packages.
dadao@ubuntu:~/Workspace/youngtest/opencvtest$ sudo apt install ocl-icd-libopencl1=2.2.11-1ubuntu1
Reading package lists... Done
Building dependency tree
Reading state information... Done
Suggested packages:
opencl-icd
The following packages will be DOWNGRADED:
ocl-icd-libopencl1
0 upgraded, 0 newly installed, 1 downgraded, 0 to remove and 8 not upgraded.
Need to get 29.2 kB of archives.
After this operation, 0 B of additional disk space will be used.
Do you want to continue? [Y/n] y
Get:1 https://mirrors.tuna.tsinghua.edu.cn/ubuntu-ports focal/main arm64 ocl-icd-libopencl1 arm64 2.2.11-1ubuntu1 [29.2 kB]
Fetched 29.2 kB in 0s (84.2 kB/s)
debconf: delaying package configuration, since apt-utils is not installed
dpkg: warning: downgrading ocl-icd-libopencl1:arm64 from 2.2.11-2ubuntu1 to 2.2.11-1ubuntu1
(Reading database ... 148571 files and directories currently installed.)
Preparing to unpack .../ocl-icd-libopencl1_2.2.11-1ubuntu1_arm64.deb ...
Unpacking ocl-icd-libopencl1:arm64 (2.2.11-1ubuntu1) over (2.2.11-2ubuntu1) ...
Setting up ocl-icd-libopencl1:arm64 (2.2.11-1ubuntu1) ...
Processing triggers for man-db (2.9.1-1) ...
Processing triggers for libc-bin (2.31-0ubuntu9.12) ...
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
2017-11-09 《Linux命令行与shell脚本编程大全》 第四章
2015-11-09 《程序员的自我修养》第二章学习笔记