Example: Rearrange Channels using an Expression

Different image formats utilize different arrangements of channels within the actual image data.  Some have the red channel first, while other arrangements have the blue channel first.  In this example, we use an SQL expression to rearrange the order of channels within the image's data.  

 

See the video version of this topic in the Manifold 9 - Rearrange Channels in an Image video.

 

 This topic uses a simple SQL expression.  A point and click way to do the same thing is to use the Join dialog, as shown in the Example: Shuffle Channels with a Raster Self Join  topic.

Channels and Display

No matter what channel ordering is used within the data, Manifold's Style pane allows us to specify how those channels should be assigned to red, green and blue display outputs.  See the discussion in topics such as the Style: Channels and Outputs Tutorial  topic.

 

Using the Style pane to say how data channels should be used, as shown, for example, in the Example: Display an NAIP Four Band Image as Color Infrared (CIR) topic, or the Example: Assign Channels topic or the Example: Using the Assign Channels Button topic, does not  change the order of data channels within the actual image.   It simply specifies how the data should be interpreted for display purposes.   

 

Not all programs are as flexible as Manifold.  Some are hard-wired to require images to store their data channels in a particular order within the file format for images to appear correctly.   If we want to export images for use in such programs we may have to change the channel ordering their data uses.  This topic shows how to do that using an SQL expression.   

 

This topic shows how to change the actual order of data in the image, changing the data in the image by changing the order of numbers within each numeric vector that provides data for each pixel.  The sample image we use, Leonardo's portrait of Ginevra de' Benci, is the same image used in the Example: Change the Contrast of an Image topic.  

 

This is not a geographic image, but the use of a portrait of a person can make it easier to see visual effects caused by re-ordering of channels.  Exactly the same workflow works the same way for geographic images, such as aerial or satellite photos.

 

 

We open the image.

 

 

In the Style pane, we see the channel assignments currently in use.  Channel 2, the third channel in the data, is assigned to the Red output.    Channel 1, the second channel in the data, is assigned to the Green output.    Channel 0, the first channel in the data, is assigned to the Blue output.  

 

The actual order of channels in the data for each pixel is Channel 0, Channel 1, Channel 2.   Those channels are intended to be assigned to Blue, Green, and Red display outputs respectively.   What we have is a classic "BGR" image in terms of how data channel numbers are stored within pixel data, even though universal force of habit, among programmers as much as among non-technical users, is to call it an "RGB" image.  

 

Manifold's Style dialog can assign the channels to whatever display outputs we desire.  In the case of this image, the Style dialog (as seen in the illustration above) assigns the third data channel to the R output, the second data channel to the G output, and the first data channel to the B output, so the image is displayed with the intended appearance.  

 

If everything works and appears the way we think it should, why would we want to re-order the data channels in the data?   We might want to do that if we want to export the image into a format that will be consumed by some other software which does not have the capability, like Manifold, to make free-form assignments of data channels to display outputs.   

 

It is fairly common to encounter packages which require that data channels always must be stored in some specific order, such as RGB or BGR.    For example, a package might require that data channels must always be stored so the first channel is intended for red, the second channel intended for green, and the third channel intended for blue.  Such programs will be compatible with themselves, but they will not be compatible with images created by other image editing programs which use BGR channel ordering in their data.

 

Manifold can use whatever channel ordering the data uses, and Manifold can change that data ordering if we like, so we can use Manifold to alter the data ordering used by an image to whatever data ordering is required for interoperability with a less-flexible package.

 

For example, if we want to export an image that has the usual BGR storage, as our example Ginevra image does, for use in a program that requires RGB storage, we can reorder the actual data numbers for each pixel into RGB order so that the value intended for the red pixel comes first and the value for the blue pixel comes last.  Lucky for us, that is very easy to do in Manifold, as follows:

 

With the focus on the image window, in the Transform pane choose the Tile field within the image.  Double-click the Expression template to launch it.

 

 

In the Expression template, press the Edit Expression button.   That opens up the expression builder dialog into which we can enter SQL expressions.  

 

 

We enter the expression

 

TileChannels([Tile], VectorMakeX3(2, 1, 0))

 

We can read about the TileChannels function and the VectorMakeX3 function in the SQL Functions topic.  The TileChannels function will reorder the data channels in the image to the ordering given by the list of numeric arguments to the VectorMakeX3 function.   

 

The list of numbers 2, 1, 0 in our expression means to take what was the third channel and put it into the first position, take what was the second channel and put it into the second position, and take what was in the first channel and put it into the third position.   This flips BGR order into RGB order.

 

Press OK.  Manifold will quickly evaluate the expression for possible errors and then return to the Transform pane.

 

 

In the Transform pane, for the Result destination we choose New Table, to write the results of the transform into a new table and image.   If we preferred, we could choose Same Field to write the results back into the Tile field, updating it in place, or we could choose a different tile field in the same table if there is one, or we could choose New Field to write the results to a new tile field that the template would create for us in the same table, also creating a new image to visualize that new field.    In this case, we simply will write the results to a new table and image.

 

For the name of the New image we enter Rearranged channels and we enter an analogous name for the New table.   We can specify whatever names we want, but it is wise to use names that remind us what those new components are supposed to be.

 

The default table name created automatically as we enter a name for the New image ends in ...Table.  This topic uses a table name that ends in ...Tiles, which is how some users prefer to name tables for images.  Others prefer to have all of their tables, both for drawings and images, end in ...Table.  We can adjust the name automatically created for the table however we like.

 

If we would like to see a preview, we can press the Preview button.

 

 

The preview shows the result, using shades of blue preview color.  A blue preview caption bar at the top of the window appears, giving the name of the template that is being previewed.  

 

Previews with channel rearrangements have limited utility, since the result is rendered in shades of blue preview color.  But at least the preview shows we have not made any catastrophic mistakes in the expression we wrote that would prevent a sensible image from appearing.

 

To apply the template and to see the final result in the new image that will be created, press Transform.

 

A new image called Rearranged channels and its table appear in the Project pane.

 

 

When we open the resulting Rearranged channels image, we see it uses RGB data channel ordering, showing the characteristically otherworldly tones of RGB data ordering interpreted as if it was BGR data ordering.   The visual effect is like using the Assign Channels button to assign RGB ordering instead of BGR ordering, as illustrated in the Example: Using the Assign Channels Button topic.

 

If we want a natural look, we can easily tell the Style dialog to use RGB data channel ordering.

 

 

In the Style pane, we use the Assign Channels button to choose RGB ordering in one click, and then we press Update Style to apply the new interpretation of data channels to the image.

 

 

The image immediately changes appearance to the expected colors.    What we have now is an image that uses RGB data channel ordering in the numbers for each pixel, with a Style that has been told to use that RGB data ordering to assign the first channel in the data to the Red display output, the second channel in the data to the Green display output, and the third channel in the data to the Blue display output.

 

If we export the image now to a format where the order of data channels is preserved, it will be correctly set up for use in any program that requires data channels to be in Red, Green, and Blue order.

 

Downloads

Download the full size ginevra image from the Product Downloads page.

 

Notes

Four channel images - The example in this topic uses a three channel image. If we are working with a four channel image, then instead of using the VectorMakeX3 function in our expression we would use the VectorMakeX4 function, with four numbers in the argument:

 

TileChannels([Tile], VectorMakeX4(2, 1, 0, 3))

 

The fourth channel in a four channel image is usually alpha or, with NAIP images, near infrared.

 

Videos

Manifold 9 - Rearrange Channels in an Image - Manifold Release 9 and Manifold Viewer can work with images using any arrangement of data channels, quickly and easily specifying how those channels should be interpreted as display outputs.  The Style pane allows us to quickly specify whatever assignment we want, without changing the data in the image.  Sometimes, however, we want to change the actual channel arrangement within the image and not just the interpretation.  This video shows how that is extremely easy to do in Manifold, with even a visual preview provided before the change is committed.  Fast! Easy! ...and, it works in the free Viewer, too!

See Also

Images

 

Transform pane

 

Style: Images

 

Style: Channels and Outputs Tutorial

 

Transform - Tiles

 

SQL Functions

 

Example: Change the Contrast of an Image - In this example we use the Style pane to change the contrast of an image.

 

Example: Using the Assign Channels Button - The Assign Channels button in the Style pane for images allows us to assign channels to the standard three Red, Green, and Blue display outputs using frequently-desired arrangements.   The button provides a short cut way to assign all channels at once instead of doing each channel individually.

 

Example: Assign Channels - How to use the Style pane for images to assign channels to display outputs such as R, G, B or A.  This topic shows examples of channel combinations and the visual results.

 

Example: Display an NAIP Four Band Image as Color Infrared (CIR) - How to use the Style pane for images to re-assign channels in a four band NAIP image to produce a Color Infrared (CIR) image display.

 

Example: Shuffle Channels with a Raster Self Join - We use the Join dialog to rearrange channels within an image,   Starting with a four channel image that has RGB plus infrared channels, we rearrange the order of channels so that infrared values are in the first channel powering the red output, red values are in the second channel powering the green output, and green values are in the third channel the blue output.   This is the classic Color Infrared (CIR) channel arrangement.  Unlike a virtual rearrangement using Style shown in the  Example: Display an NAIP Four Band Image as Color Infrared (CIR) topic, rearranging channels in this way changes the structure of the data so that any exported image will retain the new arrangement.

 

Example: Set Image Transparency using Alpha - The A row in the Style pane allows us to specify what transparency we want to apply to the image, either by applying the same value for A for all pixels or by using one of the other channels to also control the A value.

 

Example: Autocontrast and Hill Shading Images using Style - This example shows how the Style pane can hill shade an image using the values of pixels as heights and generating shadows as if the Sun were located at the specified azimuth and altitude.   This capability is used most frequently with raster images to give an impression of three dimensionality in cases where the values of pixels represent terrain elevations.

 

Example: Style Applied to an Image Server Image - Because the Style pane simply changes the way an image is displayed and not the data, it can operate on read-only data served by various web servers such as WMS REST servers.    In this example we look at every detail of creating a data source using an image server and then manipulating the appearance of the display with Style.  We will connect to a WMS server that provides LiDAR data in various forms, including as terrain elevation.

 

SQL Example: Create NDVI Displays - How to create a query that creates an NDVI display from a four-band NAIP image, with tips and tricks on how to copy and paste existing information to get the result we want.

 

Example: Transform Elevation Image to Flatten Bathymetry to Zero - Using the Transform pane with an image, which contains a single data channel for terrain elevation data for land together bathymetry data for oceans, we use the Expression tab of the Transform dialog to reset all pixel values less than zero to zero.   This takes all below-zero elevations and sets them to zero, in effect removing bathymetry effects so that ocean areas are represented with zero elevation.