Image Processing and Pixel Access
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.
Here is how it works…
- Load an image
image = cvb.Image(cvb.install_path() + "/tutorial/Clara.bmp")
- Convert it to a numpy array
np_array = cvb.as_array(image, copy=False)
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.
wrapped_image = cvb.WrappedImage.from_buffer(np_array, cvb.BufferLayout.HeightWidthPlane)
You may use the optional plane parameter to change the meaning of the array dimensions.