# Swap RGB Image to .net IntPtr bgr

**URL:** <https://forum.commonvisionblox.com/t/swap-rgb-image-to-net-intptr-bgr/1135>\
**Category:** Programming Questions\
**Created:** [May 12, 2020, 12:00pm UTC](https://forum.commonvisionblox.com/t/swap-rgb-image-to-net-intptr-bgr/1135 "2020-05-12T12:00:07Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![mariusmherrmann](https://avatars.discourse-cdn.com/v4/letter/m/8edcca/32.png) [@mariusmherrmann](https://forum.commonvisionblox.com/u/mariusmherrmann)\
**Post date:** [May 12, 2020, 12:00pm UTC](https://forum.commonvisionblox.com/t/swap-rgb-image-to-net-intptr-bgr/1135/1 "2020-05-12T12:00:07Z")

</div>

hi, is there another opportunity to swap or switch the planes without coping the hole image to get an bgr linear access by an intptr in c#.net?  
we tried to swap the planes by the funtion image.FromPlanes(…,image.Planes.Reverse.ToArray()) - after this operation and using the plane[0].ptr to get linear access the color planes are not as expected - see image (original, result) ![SwappedImage](https://canada1.discourse-cdn.com/flex027/uploads/commonvisionblox/original/1X/d2800ecd7850c5965407ada7f68c9568b484ec3f.png)

---

<div class="post-metadata">

**Author:** ![mariusmherrmann](https://avatars.discourse-cdn.com/v4/letter/m/8edcca/32.png) [@mariusmherrmann](https://forum.commonvisionblox.com/u/mariusmherrmann)\
**Post date:** [May 12, 2020, 12:00pm UTC](https://forum.commonvisionblox.com/t/swap-rgb-image-to-net-intptr-bgr/1135/2 "2020-05-12T12:00:52Z")

</div>

![org](https://canada1.discourse-cdn.com/flex027/uploads/commonvisionblox/original/1X/27b93644b5e4d5b1d344aadd972195536f79f028.png)

---

<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:** [May 12, 2020, 12:53pm UTC](https://forum.commonvisionblox.com/t/swap-rgb-image-to-net-intptr-bgr/1135/3 "2020-05-12T12:53:25Z")

</div>

Hi @mariusmherrmann 🙂

I am a bit curious, why the image has the wrong plane order in the first place?  
Do you load the image from file, or is it acquired from a camera? If it comes from a camera, maybe the acquisition device isn’t configured correctly.

Anyway, you can use the _SwapChannels()_ method from the :cvb: Foundation package as follows:

`var newImg = Stemmer.Cvb.Foundation.ConvertColorSpace.SwapChannels(img, new int[] {2, 1, 0} );`

The array defines the new layout. So with the array { 2, 1, 0 }, an RGB image would be rearranged to BGR plane order.

I hope this helps!  
Thomas

---

<div class="post-metadata">

**Author:** ![mariusmherrmann](https://avatars.discourse-cdn.com/v4/letter/m/8edcca/32.png) [@mariusmherrmann](https://forum.commonvisionblox.com/u/mariusmherrmann)\
**Post date:** [May 13, 2020, 6:58am UTC](https://forum.commonvisionblox.com/t/swap-rgb-image-to-net-intptr-bgr/1135/4 "2020-05-13T06:58:03Z")

</div>

Hallo Herr Steinbinder,

die Methode benötigt ca. 37ms bei unserem Bildformat. Es wird also eine Kopie erstellt richtig? Gibt es hier was performanteres?

Vielen Dank!  
Marius Herrmann

---

<div class="post-metadata">

**Author:** ![Chris](https://yyz1.discourse-cdn.com/flex027/user_avatar/forum.commonvisionblox.com/chris/32/1154_2.png) [@Chris](https://forum.commonvisionblox.com/u/Chris)\
**Post date:** [May 13, 2020, 8:33am UTC](https://forum.commonvisionblox.com/t/swap-rgb-image-to-net-intptr-bgr/1135/5 "2020-05-13T08:33:18Z")

</div>

Hello @mariusmherrmann

please let us know, if you can change the format to BGR in the driver.  
We will continue searching until we hear from you.

Cheers  
Chris

---

<div class="post-metadata">

**Author:** ![Chris](https://yyz1.discourse-cdn.com/flex027/user_avatar/forum.commonvisionblox.com/chris/32/1154_2.png) [@Chris](https://forum.commonvisionblox.com/u/Chris)\
**Post date:** [May 13, 2020, 8:53am UTC](https://forum.commonvisionblox.com/t/swap-rgb-image-to-net-intptr-bgr/1135/6 "2020-05-13T08:53:10Z")

</div>

@parsd any tips on this one?  
What I am curious about is, whether any other SDK will interpret the color format as BGR just for the fact that we changed the alignment of the planes in CVB…  
If so, any tip to get this done without copying the imagedata would be great.

---

<div class="post-metadata">

**Author:** ![parsd](https://yyz1.discourse-cdn.com/flex027/user_avatar/forum.commonvisionblox.com/parsd/32/7_2.png) [@parsd](https://forum.commonvisionblox.com/u/parsd)\
**Post date:** [May 13, 2020, 10:32am UTC](https://forum.commonvisionblox.com/t/swap-rgb-image-to-net-intptr-bgr/1135/7 "2020-05-13T10:32:23Z")

</div>

Hi @mariusmherrmann,

I am not sure if I understand correctly. Therfore a clarification on what the :cvb: `Image` is might help: The `Image` is a so called abstract data type, i.e. it only defines behaviour, but not the exact implementation and memory layout. You can think of it as a view on some memory where our plane 0 always represents red, 1 green and 2 blue independent of memory layout. :cvb: `Image`s also have no limitation on memory layouts: so the buffer can be planar (first all reds, then all greens, …) or even not linear at all. The `Image` just helps you to access these varying buffers and make sense of the actual memory layout. Thus it is not safe to just get the linear access of plane 0 and assume that the memory layout is like an RGB or even BGR array.

So the actual memory layout depends on the image source. Most cameras by default send either Bayer or RGB data. CVB’s default is RGB memory order for conversions and loading files as historically data was (and often still is) sent in this order. The implementation of the `Image` is done like this to reduce latency when streaming from cameras. We deliver the buffer as is if possible.

So if you use

```auto
Image.FromPlanes(MappingOption.LinkPixels, image.Planes.Reverse().ToArray())

```

only CVB’s view on the buffer is changed. But as no copying is done the memory layout stays RGB. If you require a BGR memory layout for your other SDK there is no way around copying or inline swapping. Inline Swapping can be done like this:

```auto
void SwapPlanes(Image image)
{
  using (var swapped = Image.FromPlanes(MappingOption.LinkPixels, image.Planes.Reverse().ToArray()))
  {
    image.CopyTo(swapped);
  }
}

```

This safes at least memory. But if you do that, then CVB’s convention of plane 0 being red is broken. So e.g. our displays would render the images wrong.

**And another caveat** : If the image memory layout was not RGBRGB upfront, the resulting memory layout would not be changed to BGRBGR as just the logical memory locations are reused. To be safe you need to analyze the layout via linear access and if it does not match use Foundations `SwapChannels`.

I hope that clearifies the issue here.

---

<div class="post-metadata">

**Author:** ![parsd](https://yyz1.discourse-cdn.com/flex027/user_avatar/forum.commonvisionblox.com/parsd/32/7_2.png) [@parsd](https://forum.commonvisionblox.com/u/parsd)\
**Post date:** [May 13, 2020, 10:37am UTC](https://forum.commonvisionblox.com/t/swap-rgb-image-to-net-intptr-bgr/1135/8 "2020-05-13T10:37:01Z")

</div>

And if you need this for OpenCV interop which uses BGR memory layout by default and has no logical channel association (channel 0 just refers to the first in memory), you can have a look at this post which does the conversion, although always copying:

[https://forum.commonvisionblox.com/t/lib-bridge-cvb-image-to-emgu-mat/962/2?u=parsd](https://forum.commonvisionblox.com/t/lib-bridge-cvb-image-to-emgu-mat/962/2)
