• 在树莓派4B上使用.Net Core进行开发OpenCV


    本文已假设你的树莓派已经使用cmake编译并安装好OpenCV和dotnet

    OpenCV和dotnet安装(本文使用OpenCV 4.4.0):

    在树莓派上安装OpenCV教程(英文 - 建议):https://pimylifeup.com/raspberry-pi-opencv/

    在树莓派上安装OpenCV教程(中文):https://www.cnblogs.com/fjlinww/p/11956264.html

    在编译的过程中会提示你有几个文件找不到,需要去这个issue上下载这个老哥提供的缺失文件的地址https://github.com/opencv/opencv_contrib/issues/1301#issuecomment-447181426

    然后复制到~/opencv_contrib/opencv_contrib-4.4.0/modules/xfeatures2d/src/下

    在树莓派上手动安装dotnet教程(中文 - 官方):https://docs.microsoft.com/zh-cn/dotnet/core/install/linux-debian#manual-install

    记得在树莓派应该下载linux-arm32的binaries

    使用.Net Core进行OpenCV开发步骤:

    1.从github:https://github.com/shimat/opencvsharp/releases上下载Release(例如4.4.0则下载OpenCvSharp-4.4.0-20200725.zip)

    2.解压zip文件后如下:

     3.解压后在自己Windows10的电脑上创建一个.Net Core 控制台项目,然后添加以下三个dll

     4.然后再添加OpenCvSharpExtern.dll(注意,根据自己系统添加x64还是x86的,添加这个是为了方便自己在win上进行开发,部署到树莓派需要编译一个so文件复制到发布根目录,后面会讲到)

     5.在新建的OpenCv .net项目引用完四个dll后在main方法尝试粘贴并运行以下代码:

    static void Main(string[] args)
            {
                Mat mat = new Mat("lenna.png", ImreadModes.Color);
    
                for (int y = 0; y < mat.Height; y++)
                {
                    for (int x = 0; x < mat.Width; x++)
                    {
                        Vec3b color = mat.Get<Vec3b>(y, x);
                        byte temp = color.Item0;
                        color.Item0 = color.Item2; // B <- R
                        color.Item2 = temp;        // R <- B
                        mat.Set<Vec3b>(y, x, color);
                    }
                }
            }

    记得随便复制一个图片改成lenna.png放到inDebug etcoreapp3.1目录下,方便调试的时候能够读到该图片

    如果跑起来没问题说明已经可以进行下一步了。

    在树莓派上编译libOpenCvSharpExtern.so文件(均在树莓派4B上操作)

    准备:先安装Mono,请按顺序执行以下命令:

    sudo apt-get -y install apt-transport-https dirmngr gnupg ca-certificates
    sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv-keys 3FA7E0328081BFF6A14DA29AA6A19B38D3D831EF
    echo "deb https://download.mono-project.com/repo/debian stable-raspbianstretch main" | sudo tee /etc/apt/sources.list.d/mono-official-stable.list
    sudo apt-get update
    sudo apt-get install mono-complete

    1.还是原来这个github repo:https://github.com/shimat/opencvsharp/releases上下载Source Code(例如4.4.0则下载Assets对应的Source Code.zip)

    2.解压源代码执行以下命令

    cd src
    mkdir build
    cd build
    cmake -D CMAKE_INSTALL_PREFIX=${YOUR_OPENCV_INSTALL_PATH} ..
    make -j4
    sudo make install

    注意:${YOUR_OPENCV_INSTALL_PATH}要换成你的OpenCv安装路径,我的树莓派是在/usr/local/include/opencv4则填这个路径

    最后你能在/usr/local/include/opencv4的lib目录下找到编译好的libOpenCvSharpExtern.so文件。

    在树莓派上运行OpenCV + .Net Core 3.1:

    1.将刚刚在win上写的Open Cv .Net例子发布后将文件复制树莓派上;

    2.然后将刚刚编译好的libOpenCvSharpExtern.so文件复制到.Net例子发布后在树莓派上根目录下

    3.运行dotnet命令将你的例子跑起来看看有没有报错。

    常见问题:

    Unhandled exception. System.TypeInitializationException: The type initializer for 'OpenCvSharp.NativeMethods' threw an exception.
     ---> System.DllNotFoundException: Unable to load shared library 'OpenCvSharpExtern' or one of its dependencies. In order to help diagnose loading problems, consider setting the LD_DEBUG environment variable: libOpenCvSharpExtern: cannot open shared object file: No such file or directory
       at OpenCvSharp.NativeMethods.redirectError(CvErrorCallback errCallback, IntPtr userdata, IntPtr& prevUserdata)
       at OpenCvSharp.ExceptionHandler.RegisterExceptionCallback() in C:ProjectsopencvsharpsrcOpenCvSharpPInvokeExceptionHandler.cs:line 49
       at OpenCvSharp.NativeMethods.LoadLibraries(IEnumerable`1 additionalPaths) in C:ProjectsopencvsharpsrcOpenCvSharpPInvokeNativeMethodsNativeMethods.cs:line 83
       at OpenCvSharp.NativeMethods..cctor() in C:ProjectsopencvsharpsrcOpenCvSharpPInvokeNativeMethodsNativeMethods.cs:line 54

    因为在树莓派运行opencvsharp需要libOpenCvSharpExtern.so文件,请检查编译后的so文件在与你的可执行文件的同级目录

    参考链接:https://nicruo.github.io/2020/02/12/opencvsharp-on-raspberry-pi-4.html

  • 相关阅读:
    Educational Codeforces Round 6
    Codeforces Round #373 (Div. 2)
    尺取法
    Codeforces Round #542 [Alex Lopashev Thanks-Round] (Div. 2)
    逆元(数论倒数)
    最大公约数gcd,最小公倍数lcm,扩展欧几里得
    hdu 6395 Sequence (分段矩阵快速幂)
    快速幂
    hdu 6432 Cyclic
    hdu 6397 charactor encoding
  • 原文地址:https://www.cnblogs.com/myhalo/p/13526303.html
Copyright © 2020-2023  润新知