# Acquiring images \> 8-bit using CVBpy

**URL:** <https://forum.commonvisionblox.com/t/acquiring-images-8-bit-using-cvbpy/1761>\
**Category:** Python API\
**Created:** [July 26, 2022, 10:20am UTC](https://forum.commonvisionblox.com/t/acquiring-images-8-bit-using-cvbpy/1761 "2022-07-26T10:20:52Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![JRo85](https://avatars.discourse-cdn.com/v4/letter/j/7993a0/32.png) [@JRo85](https://forum.commonvisionblox.com/u/JRo85)\
**Post date:** [July 26, 2022, 10:20am UTC](https://forum.commonvisionblox.com/t/acquiring-images-8-bit-using-cvbpy/1761/1 "2022-07-26T10:20:52Z")

</div>

Hello,

I want to acquire images in 10 or 12 bit from a jai Fusion camera. I can acquire 8 bit images without a problem but as soon as I change the PixelFormat.values to “BayerRG10” or “BayerRG12” for the color sensor or “Mono10 or 12” for the monochromatic sensors the images show a plain image of zeros if I plot the MultiPartImage using matplotlib.pyplot.imshow(). If I use cvb.as\_array on the MultiPartImage I receive the following error message:

```auto
File "...\cvb\ __init__.py", line 368, in as_array
    planeCount = len(image.planes)
UnboundLocalError: local variable 'image' referenced before assignment

```

I’m aware that the camera only allows 12bit images being captured when VideoBypassMode is enabled, but enabling doesn’t change anything in Python. I can however display data in \>8bit when I use the GeniCamBrowser.

I’m using cvb v13.04.005, python 3.8.3 and a jai FS-3200-10GE-NNC on a Windows 10 mashine.

Thank you for your help!

---

<div class="post-metadata">

**Author:** ![s-woe](https://yyz1.discourse-cdn.com/flex027/user_avatar/forum.commonvisionblox.com/s-woe/32/572_2.png) [@s-woe](https://forum.commonvisionblox.com/u/s-woe)\
**Post date:** [July 26, 2022, 11:23am UTC](https://forum.commonvisionblox.com/t/acquiring-images-8-bit-using-cvbpy/1761/2 "2022-07-26T11:23:13Z")

</div>

Hi @JRo85 !  
Please provide a minimum working example of the task you want to accomplish.  
The error slightly indicates faulty use of Python and/or CVBpy.

---

<div class="post-metadata">

**Author:** ![JRo85](https://avatars.discourse-cdn.com/v4/letter/j/7993a0/32.png) [@JRo85](https://forum.commonvisionblox.com/u/JRo85)\
**Post date:** [July 26, 2022, 11:42am UTC](https://forum.commonvisionblox.com/t/acquiring-images-8-bit-using-cvbpy/1761/3 "2022-07-26T11:42:19Z")

</div>

Hi,

sure! Here is a bit of code that should ilustrate the issue:

```auto
import matplotlib.pyplot as plt
import cvb

discover = cvb.DeviceFactory.discover_from_root(flags=cvb.DiscoverFlags.IgnoreVins,)
device_info = discover[0]
device_info.set_parameter("PacketSize", "8192")
dev = cvb.DeviceFactory.open(device_info.access_token, cvb.AcquisitionStack.GenTL)
dev_node_map = dev.node_maps["Device"]

stream = []
PixelFormatlist = ["BayerRG8","Mono8","Mono8"]
for n in range(3):
    stream.append(dev.stream(cvb.ImageStream, n))
    dev_node_map["SourceSelector"].value = "Source%s"%n
    dev_node_map["PixelFormat"].value = PixelFormatlist[n]
    dev_node_map["GevSCPSPacketSize"].value = 8192

img = [[],[],[]]
for n in range(3):
    stream[n].start()
for n in range(3):
    img[n],_,_ = stream[n].wait()

fig, ax = plt.subplots(1,3, figsize=(18,4))
for n in range(3):
    ax[n].imshow(img[n])
plt.show()

np_img = cvb.as_array(img[0])

```

When changing the elements in PixelFormatlist to [“BayerRG10”, “Mono10”, “Mono10”], matplotlib shows a plain image of zeros and the beforementioned error in cvb.as\_array occurs.

---

<div class="post-metadata">

**Author:** ![s-woe](https://yyz1.discourse-cdn.com/flex027/user_avatar/forum.commonvisionblox.com/s-woe/32/572_2.png) [@s-woe](https://forum.commonvisionblox.com/u/s-woe)\
**Post date:** [July 26, 2022, 12:23pm UTC](https://forum.commonvisionblox.com/t/acquiring-images-8-bit-using-cvbpy/1761/4 "2022-07-26T12:23:25Z")

</div>

First: I would recommend to put your acquisition into a context in order to guarantee the correct lifetime of acquisition related objects:

```auto
with cvb.DeviceFactory.open(device_info.access_token, cvb.AcquisitionStack.GenTL) as dev:
  dev_node_map = dev.node_maps["Device"]
  [...]

```

Check whether the set of the pixel format is successful (by debugging). Also let the driver react to the change of pixel format, which obviously changes the data size:

```auto
[...]
for n in range(3):
    stream.append(dev.stream(cvb.ImageStream, n))
    dev_node_map["SourceSelector"].value = "Source%s"%n
    dev_node_map["PixelFormat"].value = PixelFormatlist[n]
    stream[n].deregister_flow_set_pool() # this helps to update the flowsetpool accordingly
[...]

```

You may have a look at the DeviceConfiguration sample program (included starting from CVB 14.00.000) in `%CVB%\Tutorial\Image Manager\CvbPy\DeviceConfiguration`

---

<div class="post-metadata">

**Author:** ![JRo85](https://avatars.discourse-cdn.com/v4/letter/j/7993a0/32.png) [@JRo85](https://forum.commonvisionblox.com/u/JRo85)\
**Post date:** [July 26, 2022, 1:01pm UTC](https://forum.commonvisionblox.com/t/acquiring-images-8-bit-using-cvbpy/1761/5 "2022-07-26T13:01:55Z")

</div>

> [@s-woe](#):
>
> Tutorial\Image Manager\CvbPy\DeviceConfiguration

Thank you! I changed my code according to your suggestions with no effect on the outcome. Setting the PixelFormat seems to be successfull as the img[n].planes[0].data\_type.bits\_per\_pixel returns the value 10 and max\_val 1023.0 for all three MultiPartImages in img, when I use “BayerRG10” and so on and bits\_per\_pixel = 12 and max\_val 4095 if using “BayerRG12” and so on.  
The waitStatus after img[n] return the Value 0. So this should be fine as well.  
The data are still missing… is it possible that this is a bug of my cvb version?  
On your homepage my version of cvb 13.04.005 seems to be the most recent one.

---

<div class="post-metadata">

**Author:** ![s-woe](https://yyz1.discourse-cdn.com/flex027/user_avatar/forum.commonvisionblox.com/s-woe/32/572_2.png) [@s-woe](https://forum.commonvisionblox.com/u/s-woe)\
**Post date:** [July 26, 2022, 1:39pm UTC](https://forum.commonvisionblox.com/t/acquiring-images-8-bit-using-cvbpy/1761/6 "2022-07-26T13:39:35Z")

</div>

The most recent version is 14.00.000 ([here](https://forum.commonvisionblox.com/t/common-vision-blox-for-linux/1575/last) for Linux, [here](https://forum.commonvisionblox.com/t/common-vision-blox-for-windows/1574/last) for Windows).

Just to be clear, the changes should be:

```auto
import matplotlib.pyplot as plt
import cvb

discover = cvb.DeviceFactory.discover_from_root(flags=cvb.DiscoverFlags.IgnoreVins,)
device_info = discover[0]
device_info.set_parameter("PacketSize", "8192")
with cvb.DeviceFactory.open(device_info.access_token, cvb.AcquisitionStack.GenTL) as dev:
    dev_node_map = dev.node_maps["Device"]
    
    stream = []
    PixelFormatlist = ["BayerRG8","Mono8","Mono8"]
    for n in range(3):
        stream.append(dev.stream(cvb.ImageStream, n))
        dev_node_map["SourceSelector"].value = "Source%s"%n
        dev_node_map["PixelFormat"].value = PixelFormatlist[n]
        dev_node_map["GevSCPSPacketSize"].value = 8192
        stream[n].deregister_flow_set_pool() # this helps to update the flowsetpool accordingly
    
    img = [[],[],[]]
    for n in range(3):
        stream[n].start()
    for n in range(3):
          img[n],_,_ = stream[n].wait()
      
    fig, ax = plt.subplots(1,3, figsize=(18,4))
    for n in range(3):
        ax[n].imshow(img[n])
    plt.show()
  
    np_img = cvb.as_array(img[0])

```

As a last suggestion, you could try to copy the image out of the array:  
Change the last line to:

```auto
np_img = cvb.as_array(img[0].copy())

```

If that doesn’t help, you may get some more detailed support at [our support](mailto:de.support@stemmer-imaging.com), by mentionning this thread here.

---

<div class="post-metadata">

**Author:** ![JRo85](https://avatars.discourse-cdn.com/v4/letter/j/7993a0/32.png) [@JRo85](https://forum.commonvisionblox.com/u/JRo85)\
**Post date:** [July 26, 2022, 2:28pm UTC](https://forum.commonvisionblox.com/t/acquiring-images-8-bit-using-cvbpy/1761/7 "2022-07-26T14:28:30Z")

</div>

Hi s-woe,

it seems indeed to be a bug of version 13.04.005. After updating to version 14.00.000 it sort of works. However, it works only for stream[1] and stream[2]. Weirdly stream[0] still shows the plain image filled with 0s and isn’t able to return an array via cvb.as\_array(). Shall I go to your support with this?

---

<div class="post-metadata">

**Author:** ![s-woe](https://yyz1.discourse-cdn.com/flex027/user_avatar/forum.commonvisionblox.com/s-woe/32/572_2.png) [@s-woe](https://forum.commonvisionblox.com/u/s-woe)\
**Post date:** [July 27, 2022, 7:03am UTC](https://forum.commonvisionblox.com/t/acquiring-images-8-bit-using-cvbpy/1761/8 "2022-07-27T07:03:38Z")

</div>

Okay, great. Which camera do you use and what are your remaining setup details (OS, arch, python version)?

---

<div class="post-metadata">

**Author:** ![JRo85](https://avatars.discourse-cdn.com/v4/letter/j/7993a0/32.png) [@JRo85](https://forum.commonvisionblox.com/u/JRo85)\
**Post date:** [July 27, 2022, 7:28am UTC](https://forum.commonvisionblox.com/t/acquiring-images-8-bit-using-cvbpy/1761/9 "2022-07-27T07:28:07Z")

</div>

I’m using a jai FS-3200T-10GE-NNC on a Windows 10 (10.0.19044) and Python 3.8.3. Let me know if you need anything else.

---

<div class="post-metadata">

**Author:** ![s-woe](https://yyz1.discourse-cdn.com/flex027/user_avatar/forum.commonvisionblox.com/s-woe/32/572_2.png) [@s-woe](https://forum.commonvisionblox.com/u/s-woe)\
**Post date:** [July 27, 2022, 1:08pm UTC](https://forum.commonvisionblox.com/t/acquiring-images-8-bit-using-cvbpy/1761/10 "2022-07-27T13:08:19Z")

</div>

Hi @JRo85 ,

your code should work when cloning the image directly after the wait. The cvb.as\_array works as a view on buffers which may not exist anymore lateron. I would suggest to write something like:

```auto
(...)
    stream = []
    PixelFormatlist = ["BayerRG8","Mono8","Mono8"]
    for n in range(3):
        stream.append(dev.stream(cvb.ImageStream, n))
        dev_node_map["SourceSelector"].value = "Source%s"%n
        dev_node_map["PixelFormat"].value = PixelFormatlist[n]
        stream[n].deregister_flow_set_pool() # this helps to update the flowsetpool accordingly
        
    img = []
    for n in range(3):
        stream[n].start()
        image,state,nodemaps = stream[n].wait()
        np_image = cvb.as_array(image.clone(), copy=True)
        img.append(np_image)

    fig, ax = plt.subplots(1,3, figsize=(18,4))
    for n in range(3):
        print(img[n].dtype)
        ax[n].imshow(img[n])
    plt.show()

```

---

<div class="post-metadata">

**Author:** ![JRo85](https://avatars.discourse-cdn.com/v4/letter/j/7993a0/32.png) [@JRo85](https://forum.commonvisionblox.com/u/JRo85)\
**Post date:** [July 27, 2022, 1:48pm UTC](https://forum.commonvisionblox.com/t/acquiring-images-8-bit-using-cvbpy/1761/11 "2022-07-27T13:48:28Z")

</div>

Hi s-woe,

thanks for your answer. Indeed, I get images from all three sensors using this method. But something still goes wrong with Sensor0 in case of bitdepth \>8bit. While sensor1(middle) and sensor2(right) show the expected image, I get some weird response from sensor0(left).

 ![image](https://canada1.discourse-cdn.com/flex027/uploads/commonvisionblox/original/2X/2/2ae7f620f76b21d10d37686f015eba0aa0a7ce7a.jpeg)  
In the 8bit case everything looks normal.  
Also, in case of \>8bit a simple cvb.as\_array(image) does the trick to go from MultiPartImage to numpy for sensors 1 and 2, while doing the same for sensor0 triggers the error message:

```auto
  File "...\cvb\ __init__.py", line 386, in as_array
    plane_count = len(image.planes)
UnboundLocalError: local variable 'image' referenced before assignment

```

---

<div class="post-metadata">

**Author:** ![s-woe](https://yyz1.discourse-cdn.com/flex027/user_avatar/forum.commonvisionblox.com/s-woe/32/572_2.png) [@s-woe](https://forum.commonvisionblox.com/u/s-woe)\
**Post date:** [July 27, 2022, 3:38pm UTC](https://forum.commonvisionblox.com/t/acquiring-images-8-bit-using-cvbpy/1761/12 "2022-07-27T15:38:11Z")

</div>

At least for your second problem

> [@s-woe](#):
>
> `np_image = cvb.as_array(image.clone(), copy=True)`

should be the trick…?

---

<div class="post-metadata">

**Author:** ![JRo85](https://avatars.discourse-cdn.com/v4/letter/j/7993a0/32.png) [@JRo85](https://forum.commonvisionblox.com/u/JRo85)\
**Post date:** [July 28, 2022, 6:51am UTC](https://forum.commonvisionblox.com/t/acquiring-images-8-bit-using-cvbpy/1761/13 "2022-07-28T06:51:42Z")

</div>

> [@s-woe](#):
>
> `np_image = cvb.as_array(image.clone(), copy=True)`

This is how the picture in my last post was generated. I used cvb.as\_array(image.clone(), copy=True) to extract the images directly after stream[n].wait(), but the image from sensor0 (the left one of the 3 images) shows this weird tiled image. I guess the tiles have something to do with the Bayer-Pattern of the sensor, but still only the upper two show the actual scene, the lower tiles are just vertical lines. If I use 8-bit for all 3 channels (using cvb.as\_array(img),not cvb.as\_array(image.clone(), copy=True), the images look as expected:

 ![image](https://canada1.discourse-cdn.com/flex027/uploads/commonvisionblox/original/2X/a/af59ce7de2b20337e2c80689b24cb899fed0171a.jpeg)  
To make things more complicated, if I set sensor0 to 8bit and sensors1 and 2 to 12 bit. I get the result using cvb.as\_array(image.clone(), copy=True):  
 ![image](https://canada1.discourse-cdn.com/flex027/uploads/commonvisionblox/original/2X/3/30408bf4c51fdcfe2d9a80576982e8285ffdc3c3.jpeg)

---

<div class="post-metadata">

**Author:** ![s-woe](https://yyz1.discourse-cdn.com/flex027/user_avatar/forum.commonvisionblox.com/s-woe/32/572_2.png) [@s-woe](https://forum.commonvisionblox.com/u/s-woe)\
**Post date:** [July 28, 2022, 11:57am UTC](https://forum.commonvisionblox.com/t/acquiring-images-8-bit-using-cvbpy/1761/14 "2022-07-28T11:57:04Z")

</div>

Okay, I guess, we might have to dive a little deeper into that. As soon as we found the problem, we will return to you.  
Cheers.

---

<div class="post-metadata">

**Author:** ![JRo85](https://avatars.discourse-cdn.com/v4/letter/j/7993a0/32.png) [@JRo85](https://forum.commonvisionblox.com/u/JRo85)\
**Post date:** [July 28, 2022, 12:41pm UTC](https://forum.commonvisionblox.com/t/acquiring-images-8-bit-using-cvbpy/1761/15 "2022-07-28T12:41:48Z")

</div>

Ok! Thank you! Let me know if I can do something to support you.

---

<div class="post-metadata">

**Author:** ![s-woe](https://yyz1.discourse-cdn.com/flex027/user_avatar/forum.commonvisionblox.com/s-woe/32/572_2.png) [@s-woe](https://forum.commonvisionblox.com/u/s-woe)\
**Post date:** [August 2, 2022, 7:12am UTC](https://forum.commonvisionblox.com/t/acquiring-images-8-bit-using-cvbpy/1761/16 "2022-08-02T07:12:35Z")

</div>

Hi @JRo85 , you spotted a bug, which will be fixed in the next service release. Thanks for that!

---

<div class="post-metadata">

**Author:** ![katesmith1304](https://avatars.discourse-cdn.com/v4/letter/k/46a35a/32.png) [@katesmith1304](https://forum.commonvisionblox.com/u/katesmith1304)\
**Post date:** [June 13, 2023, 8:56am UTC](https://forum.commonvisionblox.com/t/acquiring-images-8-bit-using-cvbpy/1761/17 "2023-06-13T08:56:48Z")

</div>

Regarding the 2nd method, how can I get the cvtColor method to work with a numpy array? I do some processing on my image using numpy, which returns an np array, which I then need to save as an 8-bit image. Here’s the error I get when calling cvtColor:

---

<div class="post-metadata">

**Author:** ![katesmith1304](https://avatars.discourse-cdn.com/v4/letter/k/46a35a/32.png) [@katesmith1304](https://forum.commonvisionblox.com/u/katesmith1304)\
**Post date:** [June 14, 2023, 6:01pm UTC](https://forum.commonvisionblox.com/t/acquiring-images-8-bit-using-cvbpy/1761/18 "2023-06-14T18:01:49Z")

</div>

no reply[.](https://vidmateapp.win/)…? is this forum active.[.](https://momix.uno/).?

---

<div class="post-metadata">

**Author:** ![JRo85](https://avatars.discourse-cdn.com/v4/letter/j/7993a0/32.png) [@JRo85](https://forum.commonvisionblox.com/u/JRo85)\
**Post date:** [June 15, 2023, 6:52am UTC](https://forum.commonvisionblox.com/t/acquiring-images-8-bit-using-cvbpy/1761/19 "2023-06-15T06:52:34Z")

</div>

Dear Kate,

At least I cannot see the error message that the cvtColor-function is producing in your first message. Also a bit of code would probably help to understand the problem.

Best

Jan

---

<div class="post-metadata">

**Author:** ![s-woe](https://yyz1.discourse-cdn.com/flex027/user_avatar/forum.commonvisionblox.com/s-woe/32/572_2.png) [@s-woe](https://forum.commonvisionblox.com/u/s-woe)\
**Post date:** [June 15, 2023, 6:56am UTC](https://forum.commonvisionblox.com/t/acquiring-images-8-bit-using-cvbpy/1761/20 "2023-06-15T06:56:30Z")

</div>

Hi @katesmith1304 ,  
would you mind reformulating your question a little bit? I’m having trouble getting the essence of it.

- What do you mean by the “2nd method”?
- You didn’t provide the error.
- cvtColor is an opencv function. Keep in mind that this is a CVB user forum.
