image

condenseImages

Description

The condenseImages action is a utility function that converts a CAS table with numeric pixel values into a CAS table that contains images. Each row in the input table corresponds to a single pixel, and the table must contain columns for pixel values, dimensions, and an identifier for each image.

proc cas; image.condenseImages / table={...} casOut={...} <copyVars={...}> <decode={...}> <depth=...> <groupedChannels=...> <height=...> <inputs={...}> <numberOfChannels=...> <width=...>; run;
Settings
ParameterDescription
tableSpecifies the input table that contains the pixel data to be condensed into images.
casOutSpecifies the output table to store the condensed images.
copyVarsSpecifies a list of variables to be copied from the input table to the output table.
decodeWhen set to true, decodes the images in the input table before processing.
depthSpecifies the bit depth of the output images (e.g., BIT8, BIT32, BIT64).
groupedChannelsWhen set to true, indicates that the pixel data is grouped by channels (e.g., all blue values, then all green, then all red).
heightSpecifies the height of the output images in pixels.
inputsSpecifies the input variables (columns) to use for constructing the images. If not specified, all numeric columns are used.
numberOfChannelsSpecifies the number of channels for the output images (e.g., 1 for grayscale, 3 for color).
widthSpecifies the width of the output images in pixels.
Data Preparation View data prep sheet
Data Creation

This example first creates a synthetic dataset named 'pixel_data'. This table simulates a flattened image format where each row contains an image identifier ('_id_'), a channel identifier ('_channel_'), a row index ('_x_'), a column index ('_y_'), and a pixel value ('_value_'). We generate data for two 2x2 grayscale images.

Copied!
1DATA pixel_data;
2 DO _id_ = 1 to 2;
3 DO _channel_ = 0 to 0; /* Grayscale */
4 DO _y_ = 0 to 1; /* Height */
5 DO _x_ = 0 to 1; /* Width */
6 _value_ = floor(rand('UNIFORM') * 255);
7 OUTPUT;
8 END;
9 END;
10 END;
11 END;
12RUN;
13 
14PROC CASUTIL;
15 load DATA=pixel_data outcaslib='casuser' casout='pixel_data_cas' replace;
16RUN;

Examples

This example demonstrates the basic usage of the condenseImages action. It takes the flattened pixel data from 'pixel_data_cas' and reconstructs two 2x2 grayscale images, saving them to the 'condensed_images' table.

SAS® / CAS Code Code awaiting community validation
Copied!
1PROC CAS;
2 image.condenseImages /
3 TABLE={caslib='casuser', name='pixel_data_cas'},
4 width=2,
5 height=2,
6 numberOfChannels=1,
7 inputs={{name='_value_'}},
8 copyVars={'_id_'},
9 casOut={caslib='casuser', name='condensed_images', replace=true};
10RUN;
Result :
The action creates a new CAS table named 'condensed_images' in the 'casuser' caslib. This table will contain two rows, one for each condensed image. It will have columns for '_id_' and '_image_', where '_image_' is a binary column holding the image data.

This example shows how to condense a color image where pixel data is provided with grouped channels. First, we create a dataset for a 2x1 color image where all red pixel values are listed first, then all green, then all blue. The 'groupedChannels=true' parameter is essential for correctly interpreting this data structure.

SAS® / CAS Code Code awaiting community validation
Copied!
1DATA pixel_data_color_grouped;
2 _id_ = 1;
3 /* Red channel */
4 _channel_ = 0; _y_ = 0; _x_ = 0; _value_ = 255; OUTPUT;
5 _channel_ = 0; _y_ = 0; _x_ = 1; _value_ = 0; OUTPUT;
6 /* Green channel */
7 _channel_ = 1; _y_ = 0; _x_ = 0; _value_ = 0; OUTPUT;
8 _channel_ = 1; _y_ = 0; _x_ = 1; _value_ = 255; OUTPUT;
9 /* Blue channel */
10 _channel_ = 2; _y_ = 0; _x_ = 0; _value_ = 0; OUTPUT;
11 _channel_ = 2; _y_ = 0; _x_ = 1; _value_ = 0; OUTPUT;
12RUN;
13 
14PROC CASUTIL;
15 load DATA=pixel_data_color_grouped outcaslib='casuser' casout='pixel_data_color_grouped_cas' replace;
16RUN;
17 
18PROC CAS;
19 image.condenseImages /
20 TABLE={caslib='casuser', name='pixel_data_color_grouped_cas'},
21 width=2,
22 height=1,
23 numberOfChannels=3,
24 groupedChannels=true,
25 inputs={{name='_value_'}},
26 copyVars={'_id_'},
27 casOut={caslib='casuser', name='condensed_color_image', replace=true};
28RUN;
Result :
A new table 'condensed_color_image' is created. It contains one row for a 2x1 color image. The first pixel will be red (255,0,0) and the second will be green (0,255,0), correctly reconstructed due to the 'groupedChannels' parameter.

This example demonstrates condensing pixel data and then immediately encoding the resulting image into PNG format using the 'decode' parameter with 'value=true' and specifying an 'encodeType'. This is useful for creating a viewable or transportable image format directly.

SAS® / CAS Code Code awaiting community validation
Copied!
1PROC CAS;
2 image.condenseImages /
3 TABLE={caslib='casuser', name='pixel_data_cas'},
4 width=2,
5 height=2,
6 numberOfChannels=1,
7 inputs={{name='_value_'}},
8 copyVars={'_id_'},
9 decode={value=true, encodeType='PNG'},
10 casOut={caslib='casuser', name='condensed_encoded_images', replace=true};
11RUN;
Result :
The output table 'condensed_encoded_images' will be created. In addition to the standard '_image_' column, it will contain a '_image_decoded_' column with the image data encoded in PNG format, ready for saving or display.

FAQ

What is the primary purpose of the condenseImages action?
Which parameters are required to run the condenseImages action?
How can I define the dimensions of the images created by this action?
Can I carry over variables from the input table to the output table?
What does the 'groupedChannels' parameter do?