验证 RGB 通道是否存在

Validate existance of RGB channels

我正在查看一些缺少方面的图像,即缺少红色、绿色或蓝色通道(在我提供图像之前,它们已被自动过程意外删除)。我需要对有效图像进行精细处理。

有没有一种快速检查图像是否具有所有三个(R、G 和 B)通道的方法? Alpha 通道(如果包含)将被忽略。

在此之前,我一直在使用 PIL 进行 Python 点的图像处理(我意识到这可能不是前进的方向)。我还没有尝试任何东西,因为我不确定最好的前进方式:我的第一个猜测,这可能是冗长的,将遍历每个像素并计算出所有红色,绿色或蓝色数据是否为零(假定丢失)但是我感觉有一种更快的方法。

几乎所有图像处理库都提供读取像素值的方法。最简单和最有效的方法确实是遍历所有像素,检查所有像素的任何值是否为 0。

当然很多库也提供方便的工具来提取颜色平面和计算平均像素值。但在内部,它们除了遍历像素外什么都不做。如果不是通过检查每个值,任何算法还应该如何知道所有值是否为零?所以你的感觉是错误的,除非像素读取功能实现不佳并且算法使用更有效的东西,这是不太可能的。

所以无论哪种方式你都没有做错。

您可以使用 ImageMagick 轻松检查图像是否缺少通道。它安装在大多数 Linux 发行版上,可用于 OSX 和 Windows - 它也有 Python 绑定,但现在,我只使用命令行:

tl;dr

将红色通道的平均值、绿色通道的平均值和蓝色通道的平均值相乘 - 如果其中任何一个为零,则答案将为零,因此您的测试既快速又简单:

convert NormalImage.png -format '%[fx:mean.r*mean.g*mean.b]' info:
0.0284282

convert NoRed.jpg -format '%[fx:mean.r*mean.g*mean.b]' info:
0

加长版

基本上,您可以像这样获取图像统计信息:

identify -verbose main.png

Image: main.png
  Format: PNG (Portable Network Graphics)
  Mime type: image/png
  Class: DirectClass
  Geometry: 1790x4098+0+0
  Units: Undefined
  Type: TrueColor
  Endianess: Undefined
  Colorspace: sRGB
  Depth: 8-bit
  Channel depth:
    red: 8-bit
    green: 8-bit
    blue: 8-bit
  Channel statistics:
    Pixels: 7335420
    Red:
      min: 0 (0)
      max: 184 (0.721569)
      mean: 139.605 (0.547471)                <--- Useful
      standard deviation: 76.5813 (0.300319)
      kurtosis: -0.46531
      skewness: -1.21572
      entropy: 0.210465
    Green:
      min: 0 (0)
      max: 115 (0.45098)
      mean: 87.2562 (0.342181)                <--- Useful
      standard deviation: 47.864 (0.187702)
      kurtosis: -0.465408
      skewness: -1.21572
      entropy: 0.223793
    Blue:
      min: 0 (0)
      max: 51 (0.2)
      mean: 38.6967 (0.151752)                <--- Useful
      standard deviation: 21.2286 (0.0832494)
      kurtosis: -0.46556
      skewness: -1.21571
      entropy: 0.253787
  Image statistics:
    Overall:
      min: 0 (0)
      max: 184 (0.721569)
      mean: 88.5193 (0.347134)
      standard deviation: 53.5609 (0.210043)
      kurtosis: 1.21045
      skewness: 0.306884
      entropy: 0.229348
  Rendering intent: Perceptual
  Gamma: 0.454545
  Chromaticity:
    red primary: (0.64,0.33)
    green primary: (0.3,0.6)
    blue primary: (0.15,0.06)
    white point: (0.3127,0.329)
  Background color: white
  Border color: srgb(223,223,223)
  Matte color: grey74
  Transparent color: black
  Interlace: None
  Intensity: Undefined
  Compose: Over
  Page geometry: 1790x4098+0+0
  Dispose: Undefined
  Iterations: 0
  Compression: Zip
  Orientation: Undefined
  Properties:
    date:create: 2016-05-04T12:09:37+01:00
    date:modify: 2016-05-04T12:00:06+01:00
    png:bKGD: chunk was found (see Background color, above)
    png:IHDR.bit-depth-orig: 8
    png:IHDR.bit_depth: 8
    png:IHDR.color-type-orig: 2
    png:IHDR.color_type: 2 (Truecolor)
    png:IHDR.interlace_method: 0 (Not interlaced)
    png:IHDR.width,height: 1790, 4098
    png:sRGB: intent=0 (Perceptual Intent)
    signature: 1b12ce9d2a18826aa215b7e8b87a050717572ed638a6be6332c741eddb36c0be
  Artifacts:
    filename: main.png
    verbose: true
  Tainted: False
  Filesize: 942KB
  Number pixels: 7.335M
  Pixels per second: 73.35MB
  User time: 0.090u
  Elapsed time: 0:01.099
  Version: ImageMagick 6.9.3-7 Q16 x86_64 2016-04-05 http://www.imagemagick.org

或者,如果你喜欢解析 JSON:

convert main.png json:

{
  "image": {
    "name": "main.png",
    "format": "PNG",
    "formatDescription": "Portable Network Graphics",
    "mimeType": "image/png",
    "class": "DirectClass",
    "geometry": {
      "width": 1790,
      "height": 4098,
      "x": 0,
      "y": 0
    },
    "units": "Undefined",
    "type": "TrueColor",
    "endianess": "Undefined",
    "colorspace": "sRGB",
    "depth": 8,
    "baseDepth": 8,
    "channelDepth": {
      "red": 8,
      "green": 8,
      "blue": 8
    },
    "pixels": 7335420,
    "imageStatistics": {
      "all": {
        "min": "0",
        "max": "184",
        "mean": "88.5193",
        "standardDeviation": "53.5609",
        "kurtosis": "1.21045",
        "skewness": "0.306884"
      }
    },
    "channelStatistics": {
      "red": {
        "min": "0",
        "max": "184",
        "mean": "139.605",
        "standardDeviation": "76.5813",
        "kurtosis": "-0.46531",
        "skewness": "-1.21572"
      },
      "green": {
        "min": "0",
        "max": "115",
        "mean": "87.2562",
        "standardDeviation": "47.864",
        "kurtosis": "-0.465408",
        "skewness": "-1.21572"
      },
      "blue": {
        "min": "0",
        "max": "51",
        "mean": "38.6967",
        "standardDeviation": "21.2286",
        "kurtosis": "-0.46556",
        "skewness": "-1.21571"
      }
    },
    "renderingIntent": "Perceptual",
    "gamma": 0.454545,
    "chromaticity": {
      "redPrimary": {
        "x": 0.64,
        "y": 0.33
      },
      "greenPrimary": {
        "x": 0.3,
        "y": 0.6
      },
      "bluePrimary": {
        "x": 0.15,
        "y": 0.06
      },
      "whitePrimary": {
        "x": 0.3127,
        "y": 0.329
      }
    },
    "backgroundColor": "#FFFFFF",
    "borderColor": "#DFDFDF",
    "matteColor": "#BDBDBD",
    "transparentColor": "#000000",
    "interlace": "None",
    "intensity": "Undefined",
    "compose": "Over",
    "pageGeometry": {
      "width": 1790,
      "height": 4098,
      "x": 0,
      "y": 0
    },
    "dispose": "Undefined",
    "iterations": 0,
    "compression": "Zip",
    "orientation": "Undefined",
    "properties": {
      "date:create": "2016-05-04T12:09:37+01:00",
      "date:modify": "2016-05-04T12:00:06+01:00",
      "png:bKGD": "chunk was found (see Background color, above)",
      "png:IHDR.bit-depth-orig": "8",
      "png:IHDR.bit_depth": "8",
      "png:IHDR.color-type-orig": "2",
      "png:IHDR.color_type": "2 (Truecolor)",
      "png:IHDR.interlace_method": "0 (Not interlaced)",
      "png:IHDR.width,height": "1790, 4098",
      "png:sRGB": "intent=0 (Perceptual Intent)",
      "signature": "1b12ce9d2a18826aa215b7e8b87a050717572ed638a6be6332c741eddb36c0be"
    },
    "artifacts": {
      "filename": "main.png"
    },
    "tainted": false,
    "filesize": "942KB",
    "numberPixels": "7.335M",
    "pixelsPerSecond": "73.35MB",
    "userTime": "0.090u",
    "elapsedTime": "0:01.099",
    "version": "ImageMagick 6.9.3-7 Q16 x86_64 2016-04-05 http://www.imagemagick.org"
  }
}

或者,你可以更手术,只得到你感兴趣的东西:

convert main.png -format '%[fx:255*mean.r]' info:
139.605