简体   繁体   中英

Setting up OpenCvSharp with Visual Studio 2019

I am trying to set up OpenCvSharp, but thus far I haven't been able to successfuly compile even the basic code examples, despite following the documentation. I followed the instruction from theOpenCvSharp wiki Windows tutorial to the tee.

I got the latest OpenCV binaries from SourceForge and added the folder to my environment variables and Path:

OPENCV_DIR 环境变量和路径扩展

Next, I downloaded the OpenCvSharp DLL files and added an OpenCvSharp.dll reference to my sample project:

参考列表中的 OpenCvSharp.dll

as well as adding OpenCvSharpExtern.dll to the EXE output directory, though that doesn't help because I can't compile the executable in the first place.

I just copied the sample Program.cs code from the wiki,

using System;
using System.Runtime.InteropServices;
using OpenCvSharp;

namespace WindowsFormsApplication1
{
    static class Program
    {
        static void Main()
        {
            using (IplImage image = new IplImage(128, 128, BitDepth.U8, 1)) 
            {
                image.Zero();
                for (int x = 0; x < image.Width; x++) 
                {
                    for (int y = 0; y < image.Height; y++) 
                    {
                        int offset = y * image.WidthStep + x;
                        byte value = (byte)(x + y);
                        Marshal.WriteByte(image.ImageData, offset, value);
                    }
                }
                using (CvWindow window = new CvWindow(image))
                {
                    CvWindow.WaitKey();
                }
            }
        }
    }
}

but when I try to compile it I get errors for the OpenCvSharp functions and types:

  CS0246  The type or namespace "IplImage" was not found
  CS0246  The type or namespace "IplImage" was not found
  CS0103  The name "BitDepth" is not defined in current context
  CS0246  The type or namespace "CvWindow" was not found
  CS0246  The type or namespace "CvWindow" was not found
  CS0103  The name "CvWindow" is not defined in current context

在此处输入图像描述

The reference is in place and the package is inluded with using , so Why does Visual Studio not recognize the definitions?

IplImage is the primitive format for storing image data from the old OpenCV1.x interface. Mat is the newer format from the OpenCV2.x versions and on. Also, CvWindow should be changed to. Since you are working with the latest version, need to update the code or NuGet packages. Updated code is like:

using System.Runtime.InteropServices;
using OpenCvSharp;

namespace WindowsFormsApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            using (var image  = new Mat(new Size(128, 128), MatType.CV_8U, Scalar.All(255)))
            {
                for (int x = 0; x < image.Width; x++) 
                {
                    for (int y = 0; y < image.Height; y++) 
                    {
                        int offset = y * image.Width + x;
                        byte value = (byte)(x + y);
                        Marshal.WriteByte(image.Data, offset, value);
                    }
                }

                using (var window = new Window("window", image: image, flags: WindowMode.AutoSize))
                {
                    Cv2.WaitKey();
                }
            }
        }
    }
}

Got it working thanks to the advice given by Ceopee. Reinstalled and tested a sample C++ project with OpenCV according to this guide to make sure it works, then compiled & launched it with this code:

using System.Runtime.InteropServices;
using OpenCvSharp;

namespace WindowsFormsApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            using (var image = new Mat(128, 128, MatType.CV_8U, Scalar.All(255)))
            {
                int w = image.Width; int h = image.Height;
                int WidthStep = w / image.Cols;
                for (int x = 0; x < w; x++)
                {
                    for (int y = 0; y < h; y++)
                    {
                        int offset = y * w + x;
                        byte value = (byte)(x + y);
                        Marshal.WriteByte(image.Data, offset, value);
                    }
                }

                using (var window = new Window("window", image: image, flags: WindowMode.AutoSize))
                {
                    Cv2.WaitKey();
                }
            }
        }
    }
}

Moral of story is to use the correct format (Read the docs,) and make sure to create a new CPU configuration and set the platform to x64 instead of "Any CPU," just like one would with a C++ OpenCV project

创建 x64 配置

Otherwise you get a BadImageFormat exception. Works like a charm now.

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM