Page 1 of 1

Install opencv-contrib

Posted: Mon Jul 26, 2021 10:28 pm
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?

Re: Install opencv-contrib

Posted: Tue Jul 27, 2021 11:31 am
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!

Re: Install opencv-contrib

Posted: Wed Jul 28, 2021 5:10 pm
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.

Re: Install opencv-contrib

Posted: Thu Jul 29, 2021 5:15 pm
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'.

Re: Install opencv-contrib

Posted: Wed Sep 15, 2021 11:38 am
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

Re: Install opencv-contrib

Posted: Wed Sep 29, 2021 8:41 pm
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

Re: Install opencv-contrib

Posted: Thu Sep 30, 2021 10:21 am
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 ...