Linear Access to the image data

I am acquiring data from a Camera and following the tutorial provided: Getting Started with CVB.Net, it is trivial.

var device = LoadCameraDriver();
device.Stream.RingBuffer.ChangeCount(NUM_DRIVER_BUFFERS, DeviceUpdateMode.UpdateDeviceImage);
Stream stream = device.Stream;
stream .RingBuffer.LockMode = RingBufferLockMode.On;

stream .Start();
using (StreamImage image = await stream.WaitAsync())
                    {
                        Process(image);
                    }
stream.stop()

However after acquiring the image, I would like to process the image and to access to the image data linearly.

In my previous implementation with the C-style AI I used the method:

cvbbool_t GetLinearAccess  ( IMG  Img,  
  cvbdim_t  Plane,  
  void **  ppBaseAddress,  
  intptr_t *  pXInc,  
  intptr_t *  pYInc  
 ) 

I read the .Net API documentation but I couldn’t find a similar function for this purpose.

This access is available on the ImagePlane, e.g. to access the first plane:

LinearAccessData imageData = image.Planes[0].GetLinearAccess();

This gives you the BasePtr, XInc, and YInc as IntPtrs. This call will throw if linear access is not possible, though. To get around that you can call:

image.Planes[0].TryGetLinearAccess(out LinearAccessData imageData);

This does not throw, but returns false if not possible. You then have to use:

image.Planes[0].Vpat

By the way: if you know the pixel datatype upfront you can also use (for byte pixels):

LinearAccessData<byte> imageData = image.Planes[0].GetLinearAccess<byte>();

Additionally to the base pointer and increments you get an indexer to access the pixel data:

imageData[x, y] = 255 - imageData[x, y];

(This is convenient, but direct pointer access is faster.)

3 Likes

I there also an equivalent Python function for GetLinearAccess?

Thanks

The GetLInearAccess functions and the objects they return rely heavily on pointer arithmetics - a concept that is not commonly used in Python and therefore not very widespread - and at the same time dangerous if not used properly as it is extremely easy to create hard-to-debug problems with it. For those reasons the linear access functions were never mapped in CVBpy.

Instead we rely on numpy arrays: The method cvb.asArray will generate a numpy view on a :cvb: image’s pixel data (without actually copying the data) and thereby provide simple access to the pixel data in an entirely pythonic way.

If you’re interested @Andreas has written a small post about that a while ago that might be worth looking at: https://forum.commonvisionblox.com/t/getting-started-with-cvbpy/241/9