# Distorted pictures on first run after camera boot - second run works fine

**URL:** <https://forum.commonvisionblox.com/t/distorted-pictures-on-first-run-after-camera-boot-second-run-works-fine/1267>\
**Category:** Python API\
**Tags:** acquisition\
**Created:** [November 12, 2020, 1:11pm UTC](https://forum.commonvisionblox.com/t/distorted-pictures-on-first-run-after-camera-boot-second-run-works-fine/1267 "2020-11-12T13:11:03Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![johang](https://avatars.discourse-cdn.com/v4/letter/j/ecc23a/32.png) [@johang](https://forum.commonvisionblox.com/u/johang)\
**Post date:** [November 12, 2020, 1:11pm UTC](https://forum.commonvisionblox.com/t/distorted-pictures-on-first-run-after-camera-boot-second-run-works-fine/1267/1 "2020-11-12T13:11:04Z")

</div>

Hi,  
I have an annoying problem in my Python/USB setup. (Python 3.8 with needed fixes)

My Python app works fine acquiring frames, except from the first run after a camera boot (USB cable unplugged/plugged). The first run always gives totally distorted frames with only diagonal lines in different colors. Stopping and restarting the application resolves the problem. No problem in GeniCam browser and my app works fine if I do a GeniCam browse before first app start.

I’m using “with cvb.DeviceFactory.open(…) as device…” and have tried all kinds of “dummy” opening/closing/start/stop/sleep in my app before starting the actual frame acquisition but nothing helps.

Seems like something is not initialized before first start but I can’t figure out what that is? I’m writing my settings to node\_map before start and that works fine.  
Any help is appreciated!

Thanks  
Johan

---

<div class="post-metadata">

**Author:** ![rtp\_derahs](https://yyz1.discourse-cdn.com/flex027/user_avatar/forum.commonvisionblox.com/rtp_derahs/32/79_2.png) [@rtp\_derahs](https://forum.commonvisionblox.com/u/rtp_derahs)\
**Post date:** [November 12, 2020, 1:54pm UTC](https://forum.commonvisionblox.com/t/distorted-pictures-on-first-run-after-camera-boot-second-run-works-fine/1267/2 "2020-11-12T13:54:25Z")

</div>

Hi @johang 🙂

Can you please post a screenshot of one of the distorted images here?  
Diagonal lines actually sounds like a testpattern…  
I assume you are loading the GenICam.vin, which you have configured via GenICam Browser before?

Which camera model are you using?

---

<div class="post-metadata">

**Author:** ![johang](https://avatars.discourse-cdn.com/v4/letter/j/ecc23a/32.png) [@johang](https://forum.commonvisionblox.com/u/johang)\
**Post date:** [November 12, 2020, 2:46pm UTC](https://forum.commonvisionblox.com/t/distorted-pictures-on-first-run-after-camera-boot-second-run-works-fine/1267/3 "2020-11-12T14:46:46Z")

</div>

![First run](https://canada1.discourse-cdn.com/flex027/uploads/commonvisionblox/original/1X/d0150aae8f1ad78f5a399871f05ce47c7f1c2304.jpeg) ![Second run](https://canada1.discourse-cdn.com/flex027/uploads/commonvisionblox/original/1X/c4f212adabc91755ff94155d9166c32a8f62fdfe.jpeg)

Ok, slightly different behaviour now. No diagonal lines on this test but a very strange picture on first run.  
The “Black and white” picture is from first run. After stopping and restarting my app I get the color picture.

Camera: Allied Vision 1800 U-240c  
Yes, I’m loading GenICam.vin

I should add that I’m making a video and this are screenshots from FFplay (FFmpeg).

---

<div class="post-metadata">

**Author:** ![johang](https://avatars.discourse-cdn.com/v4/letter/j/ecc23a/32.png) [@johang](https://forum.commonvisionblox.com/u/johang)\
**Post date:** [November 12, 2020, 2:57pm UTC](https://forum.commonvisionblox.com/t/distorted-pictures-on-first-run-after-camera-boot-second-run-works-fine/1267/4 "2020-11-12T14:57:36Z")

</div>

![Stripes](https://canada1.discourse-cdn.com/flex027/uploads/commonvisionblox/original/1X/76eed67b09180937d35125bc90f630b5526f12ac.jpeg)  
Diagonal lines example from earlier runs.

---

<div class="post-metadata">

**Author:** ![rtp\_derahs](https://yyz1.discourse-cdn.com/flex027/user_avatar/forum.commonvisionblox.com/rtp_derahs/32/79_2.png) [@rtp\_derahs](https://forum.commonvisionblox.com/u/rtp_derahs)\
**Post date:** [November 12, 2020, 4:21pm UTC](https://forum.commonvisionblox.com/t/distorted-pictures-on-first-run-after-camera-boot-second-run-works-fine/1267/5 "2020-11-12T16:21:37Z")

</div>

> [@johang](#):
>
> I’m writing my settings to node\_map before start and that works fine.

Do you set nodes like image width / height or pixel format?

Since the buffer for the images are being allocated when loading the driver, changing the image size will lead to a mismatch between buffer size (and it’s interpretation) and the actual data you receive.

To avoid a driver reload by hand, you can use the ImageRect Interface like so:

```python
new_size = cvb.Size2D(width = 640, height = 480)
device.image_rect.apply_size(new_size, cvb.DeviceUpdateMode.UpdateDeviceImage)

```

This will automatically resize the buffer for you.

Please let me know, if that fixed at least some of your problems 😁

---

<div class="post-metadata">

**Author:** ![johang](https://avatars.discourse-cdn.com/v4/letter/j/ecc23a/32.png) [@johang](https://forum.commonvisionblox.com/u/johang)\
**Post date:** [November 12, 2020, 4:40pm UTC](https://forum.commonvisionblox.com/t/distorted-pictures-on-first-run-after-camera-boot-second-run-works-fine/1267/6 "2020-11-12T16:40:33Z")

</div>

Fantastic! That was it!  
I am setting pixel size and pixel format so this was obviously the fix. That also explains the two different distortions as I changed cam pixel size between those tests.

The picture looks good now but should I do similar stuff for the pixel format change I do in my app?  
Cam boots with RGB but my app changes that to BayerRG8.

---

<div class="post-metadata">

**Author:** ![illusive](https://yyz1.discourse-cdn.com/flex027/user_avatar/forum.commonvisionblox.com/illusive/32/61_2.png) [@illusive](https://forum.commonvisionblox.com/u/illusive)\
**Post date:** [November 12, 2020, 4:47pm UTC](https://forum.commonvisionblox.com/t/distorted-pictures-on-first-run-after-camera-boot-second-run-works-fine/1267/7 "2020-11-12T16:47:58Z")

</div>

> [@johang](#):
>
> The picture looks good now but should I do similar stuff for the pixel format change I do in my app?  
> Cam boots with RGB but my app changes that to BayerRG8.

Anything that changes the arrangement/interpretation of the data inside the buffer should be followed by an update call as there is no automatism that allows :cvb: to detect these changes (there is no device/manufacturer-independent convention or mechanism that would allow a software to know that changes to the value of a node X means that the buffer layout (widht/height/stride) has been affected).

---

<div class="post-metadata">

**Author:** ![johang](https://avatars.discourse-cdn.com/v4/letter/j/ecc23a/32.png) [@johang](https://forum.commonvisionblox.com/u/johang)\
**Post date:** [November 12, 2020, 4:56pm UTC](https://forum.commonvisionblox.com/t/distorted-pictures-on-first-run-after-camera-boot-second-run-works-fine/1267/8 "2020-11-12T16:56:54Z")

</div>

Ok,  
I was more wondering about the exact update call for pixel format? I can’t find anything specific for that?  
Is it the same update call as for image size, i.e “device.image\_rect.apply\_size(…)”

---

<div class="post-metadata">

**Author:** ![rtp\_derahs](https://yyz1.discourse-cdn.com/flex027/user_avatar/forum.commonvisionblox.com/rtp_derahs/32/79_2.png) [@rtp\_derahs](https://forum.commonvisionblox.com/u/rtp_derahs)\
**Post date:** [November 12, 2020, 8:40pm UTC](https://forum.commonvisionblox.com/t/distorted-pictures-on-first-run-after-camera-boot-second-run-works-fine/1267/9 "2020-11-12T20:40:58Z")

</div>

Have a look at the :cvb: reference:

[https://help.commonvisionblox.com/NextGen/14.0/md\_scripts\_main\_\_a\_p\_i\_\_overview.html](https://help.commonvisionblox.com/NextGen/14.0/md_scripts_main __a_p_i__ overview.html)

The ImageRect interface provides the following update() function:

[https://help.commonvisionblox.com/NextGen/14.0/cvbpy/db/d10/classcvb\_1\_1\_image\_rect.html#adc39be67f28f7bbb5d429ef9717721c7](https://help.commonvisionblox.com/NextGen/14.0/cvbpy/db/d10/classcvb_1_1_image_rect.html#adc39be67f28f7bbb5d429ef9717721c7)

---

<div class="post-metadata">

**Author:** ![johang](https://avatars.discourse-cdn.com/v4/letter/j/ecc23a/32.png) [@johang](https://forum.commonvisionblox.com/u/johang)\
**Post date:** [November 12, 2020, 10:42pm UTC](https://forum.commonvisionblox.com/t/distorted-pictures-on-first-run-after-camera-boot-second-run-works-fine/1267/10 "2020-11-12T22:42:59Z")

</div>

Thanks a lot!  
It was not crystal clear to me that the class described as **Image rectangle operations on a device** would be used for updates of “non rectangle” settings as well 🙂 But now I know!  
Thanks a lot for quick response. It’s working great now!!

---

<div class="post-metadata">

**Author:** ![illusive](https://yyz1.discourse-cdn.com/flex027/user_avatar/forum.commonvisionblox.com/illusive/32/61_2.png) [@illusive](https://forum.commonvisionblox.com/u/illusive)\
**Post date:** [November 16, 2020, 7:09am UTC](https://forum.commonvisionblox.com/t/distorted-pictures-on-first-run-after-camera-boot-second-run-works-fine/1267/11 "2020-11-16T07:09:24Z")

</div>

Hi @johang

> [@johang](#):
>
> It was not crystal clear to me that the class described as **Image rectangle operations on a device** would be used for updates of “non rectangle” settings as well

You’re absolutely right, this is far from intuitive… It’s located there because the image rect operations just happened to be the first where this became an issue (this used to be at a time when dynamically changing parameters like binning, colour mode etc. were almost unthinkable…), which is why the function ended up attached to the image rect interface. Maybe we should consider moving it to a more prominent location when we integrate the new acquisition engine of 13.3 into the object-oriented wrappers…

---

<div class="post-metadata">

**Author:** ![johang](https://avatars.discourse-cdn.com/v4/letter/j/ecc23a/32.png) [@johang](https://forum.commonvisionblox.com/u/johang)\
**Post date:** [November 16, 2020, 1:32pm UTC](https://forum.commonvisionblox.com/t/distorted-pictures-on-first-run-after-camera-boot-second-run-works-fine/1267/12 "2020-11-16T13:32:26Z")

</div>

Thanks! Probably a good idea to move it around a bit for easier understanding 🙂. Been doing some heavy Python/USB development this autumn so if you are interested in feedback I’m happy to share.
