Install opencv-contrib

Everything OAD-related that won't fit in the other categories: share general aspects of macro programming and discuss the OAD environment
Matthew Cottrell
Posts: 10
Joined: Sat Jul 24, 2021 4:10 pm

Install opencv-contrib

Post by Matthew Cottrell »

I would like to install opencv-contrib and use those functions in Zen macros.

Normally, I would install it using pip, but that does not seem to be the way it's done with the python installed with Zen blue.

Is it possible to install OpenCV-contrib using anaconda? There appears to be at least some anaconda stuff installed with Zen blue, but I can't figure out how to access it.

There is _anaconda.exe in the env directory under Py20037.2, but that doesn't seem to be the way to go. Or is it?

Anyway, how can I install Python packages that will work with the Python that came with Zen blue?
CarlZeissMicroscopy3
Posts: 180
Joined: Wed May 20, 2020 10:10 am

Re: Install opencv-contrib

Post by CarlZeissMicroscopy3 »

Hello Matthew Cottrell,

first of all I would like to clarify some very foundamental ideas:

'Standard' Python is based on 'unmaged code' (like C/C++ generates). Whereas 'Iron'Python is based on 'managed code' (which is generated by all .Net based languages like C#, VB.Net etc.).

It is not possible to integrate OpenCV ‘directly’ into IronPython, as OpenCV is written in unmanaged code and IronPython uses managed code.
There has to be an interface that manages this gap.

As many people use a .Net based languages OpenCV has written such an interface.

Please have a look at:
https://docs.opencv.org/3.4/db/d30/clas ... _1Net.html
or
https://github.com/shimat/opencvsharp
etc.

Every .Net based dll can be used by IronPython via importing the dll and its namespaces.

A very simple example:

Code: Select all

from System.IO import File, Directory
path = r'C:\Temp'
imageType = '*.CZI'
files = Directory.GetFiles(path,imageType)

This imports the functionality of the Directory class of .Net into an IronPython macro.

In a similar way this approach should work with the .Net libraries for OpenCV!
Matthew Cottrell
Posts: 10
Joined: Sat Jul 24, 2021 4:10 pm

Re: Install opencv-contrib

Post by Matthew Cottrell »

I'm making good progress with the OpenCV libraries for IronPython, but I need a little help with one of the libraries.

The NumpyDotNet.dll and OpenCvSharp.dll appear to load successfully.

But I get an error about OpenCvShaprExtern.dll saying:
The module was expected to contain an assembly manifest.

when the debugger breaks on the line:
"clr.AddReferenceToFileAndPath(r"C:\Users\Cottrell\Library\OpenCvSharpExtern.dll")"

Code: Select all

import clr
import sys

print sys.path

clr.AddReferenceToFileAndPath(r"C:\Users\Cottrell\Library\NumpyDotNet.dll")
import NumpyDotNet as np

clr.AddReferenceToFileAndPath(r"C:\Users\Cottrell\Library\OpenCvSharp.dll")
import OpenCvSharp as cv

clr.AddReferenceToFileAndPath(r"C:\Users\Cottrell\Library\OpenCvSharpExtern.dll")
import OpenCvSharpExtern

img = cv.Cv2.ImRead("/Volumes/GoogleDrive/My Drive/Laboratory/Documents/Projects/Zeiss/Images/Yeast5.tif")
If I don't include the OpenCvSharpExtern.dll I get the error:
The type initializer for OpenCvSharp.Internal.NativeMethods threw an exception
Unable to load the DLL OpenCvSharpExtern: The specified module could not be found.


when the debugger breaks at the line:
"img = cv.Cv2.ImRead("/Volumes/GoogleDrive/My Drive/Laboratory/Documents/Projects/Zeiss/Images/Yeast5.tif")"

I suspect that the error message is not especially informative. References that I've read in places like StackExchange have not been very helpful.

But maybe someone here will have a suggestion.
CarlZeissMicroscopy3
Posts: 180
Joined: Wed May 20, 2020 10:10 am

Re: Install opencv-contrib

Post by CarlZeissMicroscopy3 »

Hello Matthew Cottrell,

just some ideas ...

Maybe OpenCvSharp.dll, included with

Code: Select all

clr.AddReferenceToFileAndPath(r"C:\Users\Cottrell\Library\OpenCvSharp.dll")
import OpenCvSharp as cv
expects OpenCvSharpExtern.dll in a certain directory together with a proper installation of OpenCv.

Therefore including OpenCvSharpExtern.dll might bee too much ...

I just can recommed that you post your question in a forum for OpenCvSharp because to me it seems that you have reached a point that is OpenCvSharp specific and has most probably little to do with 'OAD-Macro-Programming'.
Fredrik Olsson
Posts: 16
Joined: Mon May 25, 2020 9:53 am

Re: Install opencv-contrib

Post by Fredrik Olsson »

Hi,
@Matthew Cottrell: If you happened to found a solution on how o incorporate a functioning OpenCvSharp with the OAD environment it would be nice if you could publish it here.

Kind regards
Fredrik Olsson
Matthew Cottrell
Posts: 10
Joined: Sat Jul 24, 2021 4:10 pm

Re: Install opencv-contrib

Post by Matthew Cottrell »

I found that the best way for me to satisfy my need to use OpenCV together with Zen Blue was to use the Xojo (xojo.com) rapid application development tool to build the user interface that communicates with python scripts that call Zen Blue macro functions and OpenCV functions. Xojo has the ability to run shell scripts which makes calling the python scripts very straight forward.

A button coded in Xojo to start a live camera image looks like this:

Code: Select all

Var sh As New Shell
sh.TimeOut = -1

// Setup the camera and hardware
If gblHardwareAndCameraSet = False Then
  sh.Execute("py.exe C:\Users\Cottrell\Documents\MicrobeCounter\Zen_Camera_Hardware_Settings.py")
  gblHardwareAndCameraSet = True
End If

If Me.Caption = "Live" Then
  // Start live preview
  sh.Execute("py.exe C:\Users\Cottrell\Documents\MicrobeCounter\Zen_Start_Live.py")
  Me.Caption = "Stop"
  // Setup Camera and Hardware
  sh.Execute("py.exe C:\Users\Cottrell\Documents\MicrobeCounter\Zen_Camera_Hardware_Settings.py")
ElseIf Me.Caption = "Stop" Then
  // Stopt live preview
  sh.Execute("py.exe C:\Users\Cottrell\Documents\MicrobeCounter\Zen_Stop_Live.py")
  Me.Caption = "Live"
End If
And the Zen_Start_Live.py python script looks like this:

Code: Select all

#! python
import win32com.client
Zen = win32com.client.GetActiveObject("Zeiss.Micro.Scripting.ZenWrapperLM")

# Set the camera settings
camerasetting = Zen.Acquisition.CameraSettings.GetByName("Live Dead MV Yeast Hemocytometer.czcs")
Zen.Acquisition.ActiveCamera.ApplyCameraSetting(camerasetting)

Zen.Acquisition.StartLive()
Here is a glimpse of the OpenCV python code that does my image processing:

Code: Select all

from __future__ import print_function

import cv2 as cv
import sys
import numpy as np
import random as rng
import time
import win32gui
import os

toolbar_width = 80

thepath = str(sys.argv[1])
theFileName = str(sys.argv[2])

ddepth = cv.CV_8U

src = cv.imread(thepath + "\\" + theFileName)

resize = cv.resize(src,(924, 771))
cv.imshow("Original Image", resize)
cv.moveWindow("Original Image",0,0)
#cv.waitKey(1000)

# Convert to grey scale
img = cv.cvtColor(src,  cv.COLOR_RGBA2GRAY, 0)

# output image with contours
img_copy = img.copy()

img_out = np.zeros((2056, 2464, 3), dtype = "uint8")

# Blur the image for better edge detection
img_blur = cv.GaussianBlur(img, (3,3), cv.BORDER_DEFAULT)
.
.
.
Hope that helps

Cheers,
Matt
CarlZeissMicroscopy3
Posts: 180
Joined: Wed May 20, 2020 10:10 am

Re: Install opencv-contrib

Post by CarlZeissMicroscopy3 »

Hello Matthew Cottrell,

thank you very much for your contribution!

This is apparently one way to go.

By using

Code: Select all

import win32com.client
Zen = win32com.client.GetActiveObject("Zeiss.Micro.Scripting.ZenWrapperLM")
it must be clear that this is just for Zen blue and cannot be used in case of Zen Core as COM is not supported!

We will try to supply a solution that is called directly from a Zen macro in the future.
The nuget packages OpenCvSharp4 and OpenCvSharp4.runtime.win are accessible via .Net languages.
IronPython, the macro language of OAD, is also .Net based.
Therefore, it should be possible to accsess OpenCvSharp from the macro directly.

As this has no high priority it may take a while …

We already tried to integrate OpenCvSharp in a c# program in Visual Studio as demonstrated in detail here:
https://www.lostindetails.com/articles/ ... ith-csharp
This worked and showed us, that integrating OpenCvSharp directly into an OAD macro should be possible.

If someone wants to try it in advance the steps I would take are:
* Downloading the two nuget packages (OpenCvSharp4 and OpenCvSharp4.runtime.win)
* Rename the extensions .nupkg by .zip. This enables you to take the dlls from the zip-file to the folder of the Zen.exe.
* Finally import OpenCvSharp in your macro.

Just an idea but maybe it helps someone ...
CarlZeissMicroscopy3
Posts: 180
Joined: Wed May 20, 2020 10:10 am

Re: Install opencv-contrib

Post by CarlZeissMicroscopy3 »

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
DLLs.png (17.65 KiB) Viewed 5212 times

Now the macro from above should also run ;-)
CarlZeissMicroscopy3
Posts: 180
Joined: Wed May 20, 2020 10:10 am

Re: Install opencv-contrib

Post by CarlZeissMicroscopy3 »

Maybe the following link is also interesting for you:

viewtopic.php?p=3355#p3355
Finch Alex

Re: Install opencv-contrib

Post by Finch Alex »

I would like to install opencv-contrib and use those functions in Zen macros.

Normally, I would install it using pip, but that does not seem to be the way it's done with the python installed with Zen blue.

Is it possible to install OpenCV-contrib using anaconda.

There appears to be at least some anaconda stuff installed with Zen blue, but I can't figure out how to access it.

There is anaconda.exe in the env directory under Py20037.2, but that doesn't seem to be the way to go.

Anyway, how can I install Python packages that will work with the Python that came with Zen blue.
Last edited by Finch Alex on Tue Mar 22, 2022 5:06 pm, edited 1 time in total.
Post Reply