名称

ST_Grayscale — 根据源光栅和表示红、绿、蓝的三条指定波段创建新的单 8BUI 波段光栅

语法

(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);

说明

根据三个输入波段(来自一个或多个光栅)创建一个具有一个 8BUI 波段的光栅。任何像素类型不是 8BUI 的输入波段都将使用 ST_Reclass 进行重新分类。

[Note]

此函数与 ST_ColorMapgrayscale 关键字不同,因为 ST_ColorMap 仅对一个波段进行操作,而此函数需要三个波段用于 RGB。此函数应用以下公式将 RGB 转换为灰度:0.2989 * RED + 0.5870 * GREEN + 0.1140 * BLUE

可用性:2.5.0

示例:变体 1

SET postgis.gdal_enabled_drivers = 'ENABLE_ALL';
SET postgis.enable_outdb_rasters = True;

WITH apple AS (
    SELECT ST_AddBand(
        ST_MakeEmptyRaster(350, 246, 0, 0, 1, -1, 0, 0, 0),
        '/tmp/apple.png'::text,
        NULL::int[]
    ) AS rast
)
SELECT
    ST_AsPNG(rast) AS original_png,
    ST_AsPNG(ST_Grayscale(rast)) AS grayscale_png
FROM apple;
                    

original_png

grayscale_png

示例:变体 2

SET postgis.gdal_enabled_drivers = 'ENABLE_ALL';
SET postgis.enable_outdb_rasters = True;

WITH apple AS (
    SELECT ST_AddBand(
        ST_MakeEmptyRaster(350, 246, 0, 0, 1, -1, 0, 0, 0),
        '/tmp/apple.png'::text,
        NULL::int[]
    ) AS rast
)
SELECT
    ST_AsPNG(rast) AS original_png,
    ST_AsPNG(ST_Grayscale(
        ARRAY[
            ROW(rast, 1)::rastbandarg, -- red
            ROW(rast, 2)::rastbandarg, -- green
            ROW(rast, 3)::rastbandarg, -- blue
        ]::rastbandarg[]
    )) AS grayscale_png
FROM apple;
                    

另请参阅

ST_AsPNGST_ReclassST_ColorMap