opencv的一个小例子

2023-11-10 09:03:20 浏览数 (3)

打开图片或者摄像头:

直接上源码:

代码语言:javascript复制
#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 statusdadao@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) ...

0 人点赞