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

sudo apt-get install opencl-headers ocl-icd-opencl-dev
若提示出错:(如下)

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.

则需要降级:
sudo apt install ocl-icd-libopencl1=2.2.11-1ubuntu1

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) ...

 

posted @   xcywt  阅读(109)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
历史上的今天:
2017-11-09 《Linux命令行与shell脚本编程大全》 第四章
2015-11-09 《程序员的自我修养》第二章学习笔记
作者:xcywt
出处:https://www.cnblogs.com/xcywt//
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
如果文中有什么错误,欢迎指出。以免更多的人被误导。
点击右上角即可分享
微信分享提示