Hi, it has been a while since the last post. However meanwhile many features were added to CVBpy.
That is right, but sometimes setting the python path is not enough. In such a case you may manually copy the contents of %CVB%/Lib/Python to your installation’s Lib/site-packages. Plase be aware that this is not an actual installation.
Before manually installing CVBpy check if your interpreter actually sees this environment variable. E.g in VS Code have to add the PYTHONPATH through the settings.
Update 18. 10. 2018: PYTHONPATH is not required anymore, see above!
Finally some advanced stuff.
CVBpy Module Layout
CVBpy contains one main module which is called "cvb" which contains everthing that is refered to as Image Manager in the C-Style API. In addition there will be sub modules for each available CVB Tool.
Currently "cvb.movie" and "cvb.foundation" (not yet complete) are available.
CVBpy Versioning
CVBpy is build on top of CVB++ which is a header only C++ wrapper for the C-Style API. This makes quite challenging as CVBpy's version also depends on the CVB++ version used to build it. You may get this information by calling cvb.version().
A possible output could be:
CVBpy-v1.0.0.build-79.rev-2793+ Release vc14 @ x64 & CVB++-v1.0.0.build-64.rev-2789+ & CVB-v13.00.004
If you report a bug or just have a question it would be great to provide CVBpy version string, as it is currently in beta state and we provide nightly builds.
However, usually you will deal with live images from stream. A stream can either be a streming device like a camera, or a video (form disk) or a emulated stream composed from single images you.
Here is how you load a EMU file, that can be used to simulate your application with a defined set of images.
Wait for 10 images and print some information about them.
try:
for i in range(0 ,10):
image, status = stream.wait_for(1000)
if status == cvb.WaitStatus.Ok:
print("Acquired image " + str(i) + " into buffer " +
str(image.buffer_index) + ".")
else:
raise RuntimeError("timeout during wait"
if status == cvb.WaitStatus.Timeout else
"acquisition aborted")
except:
pass
finally:
stream.try_abort()
Please note that, although errors are unlikely in this simple example and I just pass them it is usually a good idea to handle them, and at least always stop the stream so that stay in a defined state.
The output given you have three (default) buffers allocated would be:
Usually you will have to configure some things in a camera before the image fits your needs. Sometimes you might even have a lot of cameras which all need the same settings applied automatically. Here comes how you can script it.
Open your device as described above.
Get the device node map, which groups all settings for your camera (device).
device_node_map = vin_device.node_maps["Device"]
Now you can access the camera features. Let’s start reading some stuff.
The value property is also used to write (if the node is writeabel) features to the camera. In the debugger you can see a lot of additional information that will help you to pick the right values. E.g:
A very nice python feature is that it can be run in a REPL Console. Which means you can interactively enter instruction and see them evaluated (ReadEvalPrintLoop).
Ipython is one way to lunch such a console. Usually it also provides code completion and easy access to the build in documentation:
Image acquisition is fine, but rather useless on it’s own, so how to do image processing with CVBpy?
First I recommend to have a look at the foundation package which provides many standard processing algorithms.
import cvb.foundation
However there are valid use cases to implement a custom algorithm and access the pixel data directly.
Unfortunately python does not provide an efficient way to do such low level stuff. Usually this is handled through widely used python module called numpy. CVBpy seamlessly works together with numpy so you can directly access the pixel data from your recently acquired image.
This is very efficient as data is not copied, by default. You may choose otherwise by setting copy=True .
Please note, that the copy parameter is only a request. Depedeing on the image’s memory layout a copy might be unavoidable.
Check if you got a copy or a view into the actual image buffer.
print(np_array.flags["OWNDATA"])
The flag OWNDATA=False means that the numpy array does not own the actual buffer, but the image does.
Access the image data (quite similar to Matlab)
np_array[83 : 108, 48 : 157] = 0
In this case just set a view pixels to black.
Save the image.
image.save("ClaraUnknown.png")
The other way around
It is just as easy to get a image from a numpy array.
When testing a image processing algorithms we usually want to see the result rendered in some sort of UI as verifying pixel one by one is cumbersome work.We are lucky that this can be done easily through numpy using pyplot.
import matplotlib.pyplot as plt
...
plt.imshow(np_image, cmap="gray")
This provides a simple method to visualize your image data.
But what if the plotted image pop-up is not enough, as an actual interactive custom UI is required.
E.g you may also want to render some custom stuff on top of the image to visualize your results
or some controls to receive user input.
UI libraries usually define their own image/buffer formats optimized for on screen rendering. This format usually does not match the image format provided by a streaming device. So at least one low level copy of the image data is required.
How does it look like in a real example? E.g using PyQt5 as UI framework.
The following steps can be used to copy a mono image into an ARGB QImage optimized for rendering. Similar steps are required to copy into an OpenGl texture for instance.
If you are using the numpy interface, as described above, it is especially important to keep track of the buffer ownership.
Of course lifetime and ownership is always important for reliable safe software. Unfortunately especially script languages tend to hide such things from the user and do some magic in the background. You probably know that python has a garbage collector, so you don’t need to care?
I really wish this was the case, but unfortunately this so not true!
Of course python has a garbage collector, but when dealing with hardware like cameras.
Huge images might even remind you that RAM is limited .
So how is it done in CVBpy? Consider the following:
device = cvb.DeviceFactory.open("GenICam.vin")
Who owns the device, and what is it’s lifetime? When is it closed again?
You as the user own the device, and it will live for the entire runtime of the script.
So the device will be open and locked for all other users until the python process has finished.
This might not be what you want, so you can shorten the lifetime of the device manually to allow others to access it.
del device
This is dangerous as python objects implement shared ownership. Therefore if between creation and destruction someone does this:
device2 = device
Suddenly there are two owners, device and device2, as a consequence calling del once does nothing except reducing the reference count. The device won’t close until there is no owner left.
Typically it is not an easy task to keep track of all the owners, as sharing happens much more subtitle in real world applications. In addition you would have to make sure that every creation or share matches a del call. This is definitely not an easy task if exceptions can alter your execution path in unexpected ways!
Python would not be python if there wasn’t a nice and clean way around it - the with statement.
So instead of simply opening a device you can write:
with cvb.DeviceFactory.open("GenICam.vin") as device:
pass
This way you put the device into a scope, which ensures that the device will be closed once the interpreter leaves the scope. In addition with guarantees this even if an exception is thrown from within the scope but caught outside the scope.
In CVBpy all objects that hold significant resources support the with statement. Use it for your own benefit!
In this case if I want to connect to cameras on ports > 0 I need to use dsicovery and connect to them using their access_teken, right?
With this approach how I can set Number of buffers and CVB color format?
Did not find these settings in node_maps
@Fatemeh
The Discovery Interface does not use or have ports. You open the camera you want with the access token which is available after your discover.
To set the CVB color format (“PixelFormat”) or number of buffers (“NumBuffer”) you need to set the parameters in the token before opening the device: