Hello all,
as there seems to be no progress integrating OpenCV into Zen Macro language using IronPyhton with OpenCvSharp4 a description including a macro for testing will be given here.
There are two ways to find the dlls needed for accessing OpenCV via OpenCvSharp4. One via Visual Studio, the other manually via 'files' (DLLs).
The first one is easy if you have Visual Studio (or other .Net development environments with NuGet-Package Installation) installed.
Just create a .NetFramework (>4.6.0, e.g. 4.62 in my case) Solution as a WinForm-, WPF- or Console-Application.
Then go to the NuGet Manager (e.g. references) and search for
OpenCvSharp4.Windows. There is one version that as an ‘
all-in-one package for Windows users’ so that there is no need to individually add the ‘other’ needed packages.
Integrating the following code into your solution shows how basics are working:
Code: Select all
using OpenCvSharp;
Console.WriteLine("Demo with default files");
string inputFileName = @"C:\...\example.jpg";
string outputFileName = @"C:\...\example-gray.jpg";
using (var image = new Mat(inputFileName))
using (var gray = image.CvtColor(ColorConversionCodes.BGR2GRAY))
gray.SaveImage(outputFileName);
This is a modification of the code given
here
When compiling your solution, the
bin folder will contain a
Debug or
Release folder which is filled with all the DLLs you need to run your application.
To use OpenCvSharp4 in your macro, just copy content of the (Debug or Release) folder to the clipboard, go to the folder where the Zen.exe is located, generate a new folder called '
OpenCv' (see also pathToOpenCv in the macro code) and paste the dlls etc. into this OpenCv-folder.
In the Zen Macro Editor insert
Code: Select all
from System.IO import Path
import clr
import sys
pathToOpenCv = r'C:\Program Files\Carl Zeiss\ZEN 2\ZEN 2 (blue edition)\OpenCv'
pathToImages = r'C:\Users\myusername\Documents\Images'
sys.path.append(path) # set path
clr.AddReference ("OpenCvSharp.dll") # dll
import OpenCvSharp # import namespace
inputFileName = Path.Combine(pathToImages, "example.jpg")
outputFileName = Path.Combine(pathToImages, "example-gray.jpg")
image = OpenCvSharp.Mat(inputFileName)
gray = image.CvtColor(OpenCvSharp.ColorConversionCodes.BGR2GRAY)
gray.SaveImage(outputFileName)
This macro nicely worked in my case
If you are not familiar with a development-tool like Visual Studio you have to get (download) the following NuGet Packages:
OpenCvSharp4.4.5.3.20210817
OpenCvSharp4.runtime.win.4.5.3.20210817
OpenCvSharp4.Windows.4.5.3.20210817
OpenCvSharp4.WpfExtensions.4.5.3.20210817
System.Buffers.4.5.1
System.Drawing.Common.5.0.2
System.Memory.4.5.4
System.Numerics.Vectors.4.5.0
System.Runtime.CompilerServices.Unsafe.5.0.0
System.ValueTuple.4.5.0
Rename the extensions .nupkg by .zip. so that you can ‘open’ the packages.
All these packages have a lib-folder where the different language versions like net, netcore or netstandard etc. reside. We need to get the .NetFramework versios located in the
net-folders e.g. net46 etc.
At the end the
OpenCv folder next to the Zen.exe contains the following files, see screenshot:
- DLLs.png (17.65 KiB) Viewed 5213 times
Now the macro from above should also run