如何从 gm recognize -verbose 获取图像红绿蓝标准差值?

问题描述 投票:0回答:2

我正在尝试获取您可以在

Standard Deviation
中看到的
Red
Green
Blue
通道中为
gm identity -verbose
给出的值,但它们未在
-format
选项中列出。

如何从命令行获取这些?

使用 PHP 列出值的解决方法:

    $raw = `gm identify -verbose {$file}|grep -E -e '^ +(Standard Deviation): *[^b]*$'`;
    preg_match_all("/(?<=Standard Deviation:)(?: +)([\d\.]+)/", $raw, $matches);
    $ret = [$matches[1][0], $matches[1][1], $matches[1][2]];

这是

gm identify -verbose
的输出。 示例我们需要
Channel Statistics
下的属性值:

Image: /.../.../img.jpg
  Format: JPEG (Joint Photographic Experts Group JFIF format)
  Geometry: 1064x1600
  Class: DirectClass
  Type: true color
  Depth: 8 bits-per-pixel component
  Channel Depths:
    Red:      8 bits
    Green:    8 bits
    Blue:     8 bits
  Channel Statistics:
    Red:
      Minimum:                     0.00 (0.0000)
      Maximum:                   255.00 (1.0000)
      Mean:                      132.32 (0.5189)
      Standard Deviation:         45.92 (0.1801)
    Green:
      Minimum:                     0.00 (0.0000)
      Maximum:                   255.00 (1.0000)
      Mean:                      104.17 (0.4085)
      Standard Deviation:         55.13 (0.2162)
    Blue:
      Minimum:                     0.00 (0.0000)
      Maximum:                   255.00 (1.0000)
      Mean:                      103.61 (0.4063)
      Standard Deviation:         55.71 (0.2185)
  Filesize: 452.0Ki
  Interlace: No
  Orientation: Unknown
  Background Color: white
  Border Color: #DFDFDF
  Matte Color: #BDBDBD
  Page geometry: 1064x1600+0+0
  Compose: Over
  Dispose: Undefined
  Iterations: 0
  Compression: JPEG
  JPEG-Quality: 95
  JPEG-Colorspace: 2
  JPEG-Colorspace-Name: RGB
  JPEG-Sampling-factors: 2x2,1x1,1x1
  Signature: 136912e901ae9314fd683868418cae1f5d838c6891ddd8c13ce28057fb39365a
  Tainted: False
  User Time: 0.010u
  Elapsed Time: 0m:0.014459s
  Pixels Per Second: 112.3Mi
php regex grep zsh graphicsmagick
2个回答
1
投票

使用

GNU Grep
,其中
-o
仅打印匹配的部分,
-P
用于 Perl 兼容的正则表达式:

grep -oP "\bStandard Deviation:\h+\K\d[\d.]*"

模式匹配:

  • \bStandard Deviation:
    从单词边界开始字面匹配
  • \h+
    匹配 1+ 个水平空白字符
  • \K
    忘记到目前为止匹配了什么
  • \d[\d.]*
    匹配单个数字后跟可选数字或点(或
    [\d.]+
    但请注意,它也只能匹配点

您也可以使用

[\d.]+
但也只能匹配点。使用
\.?\d[\d.]*
可以匹配可选的前导点,或更严格的格式不允许连续点
\.?\d+(?:\.\d+)*\b

查看正则表达式匹配


或者使用

sed

的替代方案
sed -n 's/^[[:space:]]\+Standard Deviation:[[:space:]]\+\([[:digit:]][[:digit:].]*\).*/\1/p'

0
投票

在 Macos 上对我有用的解决方案基于@thefourthbird 的答案。我无法安装

ggrep
,但从
pcre2grep
(macports) 成功安装了
port

gm identify -verbose img.jpg|\
pcre2grep -o --allow-lookaround-bsk "\b(?:Minimum|Maximum|Mean|Standard Deviation): +\d+\.\d+ \(\K\d+\.\d+"

--allow-lookaround-bsk
中的
pcre2grep
选项启用了
\K
符号,需要丢弃每个值之前的内容。

结果是我可以从问题中的示例中使用的提取值:

0.0000
1.0000
0.5189
0.1801
0.0000
1.0000
0.4085
0.2162
0.0000
1.0000
0.4063
0.2185

注意:值的数量各不相同。

  • 单色图像只有 1 个通道
  • 彩色图像(参见示例)有 3 个红色、绿色和蓝色。
  • 每个通道始终有 4 个值:
    Minimum
    |
    Maximum
    |
    Mean
    |
    Standard Deviation
© www.soinside.com 2019 - 2024. All rights reserved.