<p>我会这样做-这不是很难,因为它看起来,有几行设置和一些注释掉调试行来显示我的想法。实际代码是一行代码。你知道吗</p>
<p>它基本上是检查图像的整个红色通道(<code>im[...,0]</code>)与整个绿色通道(<code>im[...,1]</code>)匹配,同样地,红色与蓝色匹配。你知道吗</p>
<pre><code>#!/usr/bin/env python3
import numpy as np
# Synthesize single channel, grey image of randomness
rangrey = np.random.randint(0,256,(480,640,1), dtype=np.uint8)
# Make RGB equivalent with R=G=B by stacking initial image 3x
ranrgb = np.dstack((rangrey,rangrey,rangrey))
# DEBUG: Check shape
# print(ranrgb,shape) # prints (480, 640, 3)
# DEBUG: Check red channel equals green channel throughout image
# DEBUG print(np.equal(ranrgb[...,0],ranrgb[...,1]).all()) # prints True
# DEBUG: Check red channel equals blue channel throughout image
# DEBUG print(np.equal(ranrgb[...,0],ranrgb[...,2]).all()) # prints True
isGrey = np.equal(ranrgb[...,0],ranrgb[...,1]).all() and np.equal(ranrgb[...,1],ranrgb[...,2]).all()
print(isGrey) # prints True
# Now change one pixel and check
ranrgb[0,0,0] += 1
isGrey = np.equal(ranrgb[...,0],ranrgb[...,1]).all() and np.equal(ranrgb[...,1],ranrgb[...,2]).all()
print(isGrey) # prints False
</code></pre>
<p>如果你的图像是<code>float64</code>由于某种未知的原因,你应该用<code>np.allclose()</code>代替<code>np.equal()</code>。你知道吗</p>
<p>注意,由于<code>and</code>在<code>isGrey</code>的测试中间,测试的后半部分可能会短路(省略以节省一半的时间),因为如果已经很清楚红色通道与绿色通道不匹配,就没有必要检查蓝色通道中发生了什么。你知道吗</p>