验证RGB通道的存在

时间:2016-03-21 16:31:10

标签: python-2.7 image-processing python-imaging-library rgb

我正在查看一些缺少方面的图像,即缺少红色,绿色或蓝色通道(在我给出图像之前,通过自动化过程意外删除了这些通道)。我需要对有效图像进行精细处理。

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

我一直在使用PIL,直到用于Python点的图像处理(我意识到它可能不是前进的方向)。我还没有尝试任何东西,因为我不确定最好的前进方式:我的第一个猜测,这可能是长时间的缠绕,如果所有的红色,绿色或蓝色数据为零,则可以循环显示每个像素并进行计算(推测失踪)但我觉得有一种更快的方法。

2 个答案:

答案 0 :(得分:1)

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

当然,许多库还提供了方便的工具来提取色彩平面和计算平均像素值。但在内部,他们除了迭代像素之外什么都不做。如果不是通过检查每个值,任何算法如何知道所有值是否为零?所以你的感觉是错误的,除非像素读取功能实现得不好,算法使用的效率更高,这是不太可能的。

所以无论如何你都没有做错。

答案 1 :(得分:1)

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

<强> TL;博士

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

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