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.  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.


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


Using the Style pane to say how data channels should be used, as shown, for example, in 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 reassigns 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 have a specific order to their data channels in order 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 change the actual order of data in the image, changing the data in the image by changing the order of numbers in use 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 data channel order, even though universal force of habit, among programmers as much as among non-technical users, is to call it a "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 in some specific order, such as RGB or BGR.    For example, a package might require that data channels must always be ordered 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, iff we want to export an image for use in a program that requires RGB ordering of data channels instead of the usual BGR order, if that image uses BGR order in the data, as our example Ginevra image does, 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:




Switch to the Transform pane, choose Tile as the destination field if it is not chosen by default, and click the Expression tab.   That opens up a simple expression builder pane into which we can enter expressions.  The expression we enter will be evaluated, and whatever value results will be put into the Tile field of the image.




We enter the expression


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


In the illustration above we have split the expression into two lines for legibility.  Expressions do not care about white space, such as extra newlines, so we can use those for better legibility.


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.




As soon as we write a valid expression, Manifold will preview what that expression will do in the image window.   If images are very big, the preview will not occur unless we zoom into a smaller region of the image where the number of pixels in view is not more than a preview allows, as illustrated in the Example: Zoom In to See Transform Previews for Big Images topic.    


We can see from the preview that the visual effect is as if we used the Assign Channels button to assign RGB ordering instead of BGR ordering of data channels to display outputs, as illustrated in the Example: Using the Assign Channels Button topic.




We can press the command button at the bottom of the display to Update Field (the default choice), which will alter the data channel order within the image "in place."   Alternately, we can switch the command button to Add Component, so when we press Add Component the system will save a copy of the image using the new channel order under a new name.   We press Add Component.




When we open the resulting Ginevra Tiles Transform Image (the default name... we could have specified a different name in the Options button), 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.   We can fix that by a quick use of the Style dialog.




btn_style_assign_channels.png 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.



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



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.



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



Transform pane


Style: Images


Style: Channels and Outputs Tutorial


Transform Templates - Images


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: 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.