ST_Grayscale — Creates a new one-8BUI band raster from the source raster and specified bands representing Red, Green and Blue
(1) raster ST_Grayscale(raster rast, integer redband=1, integer greenband=2, integer blueband=3, text extenttype=INTERSECTION);
(2) raster ST_Grayscale(rastbandarg[] rastbandargset, text extenttype=INTERSECTION);
Create a raster with one 8BUI band given three input bands (from one or more rasters). Any input band whose pixel type is not 8BUI will be reclassified using ST_Reclass.
|
|
|
This function is not like ST_ColorMap with the |
Availability: 2.5.0
This self-contained example paints an apple with gradients and highlights into three raster bands. It compares the original RGB raster with the equivalent grayscale results produced by both function variants.
WITH pixels AS (
SELECT
col,
row,
x,
y,
(((x - 48.0)^2 / (25.0^2) + (y - 54.0)^2 / (29.0^2) < 1)
OR ((x - 72.0)^2 / (25.0^2) + (y - 54.0)^2 / (29.0^2) < 1))
AND NOT ((x - 60.0)^2 / (8.0^2) + (y - 26.0)^2 / (5.0^2) < 1) AS fruit,
((x - 82.0) * 0.84 + (y - 28.0) * 0.54)^2 / (22.0^2)
+ (-(x - 82.0) * 0.54 + (y - 28.0) * 0.84)^2 / (8.0^2) < 1 AS leaf,
abs((x - 62.0) * 0.96 - (y - 29.0) * 0.28) < 3
AND x BETWEEN 60 AND 69 AND y BETWEEN 21 AND 45 AS stem
FROM (
SELECT
col,
row,
col / 2.0 AS x,
row / 2.0 AS y
FROM generate_series(1, 180) AS row
CROSS JOIN generate_series(1, 240) AS col
) AS source_pixels
), shaded AS (
SELECT
col,
row,
x,
y,
CASE
WHEN stem THEN 95 + y * 0.7 + (x % 3) * 8
WHEN leaf THEN 28 + (90 - x) * 0.5
WHEN fruit THEN LEAST(255, GREATEST(0,
150
+ (85 - sqrt((x - 39.0)^2 + (y - 37.0)^2)) * 1.1
+ CASE
WHEN (x - 43.0)^2 / (10.0^2) + (y - 43.0)^2 / (7.0^2) < 1
THEN 35
ELSE 0
END
- CASE
WHEN (x - 77.0)^2 / (20.0^2) + (y - 67.0)^2 / (13.0^2) < 1
THEN 35
ELSE 0
END
+ ((x * 13 + y * 7) % 19 - 9)
))
ELSE 245
END AS red,
CASE
WHEN stem THEN 50 + y * 0.35
WHEN leaf THEN LEAST(190, GREATEST(40,
105 + (95 - x) * 1.2 + (y - 20) * 1.5
+ CASE
WHEN (x - 76.0)^2 / (12.0^2) + (y - 24.0)^2 / (4.0^2) < 1
THEN 45
ELSE 0
END
))
WHEN fruit THEN LEAST(95, GREATEST(15,
24 + (68 - y) * 0.35
+ CASE
WHEN (x - 43.0)^2 / (10.0^2) + (y - 43.0)^2 / (7.0^2) < 1
THEN 30
ELSE 0
END
+ ((x * 5 + y * 11) % 13 - 6)
))
ELSE 245
END AS green,
CASE
WHEN stem THEN 22 + (x % 4) * 3
WHEN leaf THEN 42 + (100 - x) * 0.4
WHEN fruit THEN LEAST(75, GREATEST(10,
18
+ CASE
WHEN (x - 43.0)^2 / (10.0^2) + (y - 43.0)^2 / (7.0^2) < 1
THEN 25
ELSE 0
END
+ ((x * 3 + y * 5) % 11 - 5)
))
ELSE 245
END AS blue
FROM pixels
), rows AS (
SELECT
row,
array_agg(red::double precision ORDER BY col) AS red_values,
array_agg(green::double precision ORDER BY col) AS green_values,
array_agg(blue::double precision ORDER BY col) AS blue_values
FROM shaded
GROUP BY row
), source AS (
SELECT ST_SetValues(ST_SetValues(ST_SetValues(
ST_AddBand(ST_AddBand(ST_AddBand(
ST_MakeEmptyRaster(240, 180, 0, 180, 1, -1, 0, 0, 0),
'8BUI'::text, 245::double precision, 0::double precision),
'8BUI'::text, 245::double precision, 0::double precision),
'8BUI'::text, 245::double precision, 0::double precision),
1, 1, 1, array_agg(red_values ORDER BY row)::double precision[][]),
2, 1, 1, array_agg(green_values ORDER BY row)::double precision[][]),
3, 1, 1, array_agg(blue_values ORDER BY row)::double precision[][]) AS rast
FROM rows
)
SELECT ST_AsPNG(rast) AS "original RGB",
ST_AsPNG(ST_Grayscale(rast)) AS "grayscale (variant 1)",
ST_AsPNG(ST_Grayscale(ARRAY[
ROW(rast, 1)::rastbandarg,
ROW(rast, 2)::rastbandarg,
ROW(rast, 3)::rastbandarg
]::rastbandarg[])) AS "grayscale (variant 2)"
FROM source;
original RGB | grayscale (variant 1) | grayscale (variant 2) -----------------------------+----------------------------+---------------------------- PNG image, 240 x 180 pixels | PNG image, 240 x 180 pixels | PNG image, 240 x 180 pixels (1 row)
Original photographic illustration:
original apple |
grayscale apple |