检查图像是否是纯白色?

时间:2016-02-19 22:31:24

标签: c# imagemagick

我需要检查具有恒定大小(512x512像素)的PNG图像是否只有白色像素,而不是其他内容。

有没有一种简单的方法可以做到这一点,最好不要手动检查每个像素?也许使用ImageMagick?

6 个答案:

答案 0 :(得分:2)

我认为没有一种神奇的方法可以确定图像是否为白色。

您可能只需要检查所有像素,但是如果将图像转换为位图,则可以快速访问图像,而不是使用GetPixel(),而是使用{{{}}将位图锁定在内存中。 1}}方法。然后,您可以使用LockBits()类型并快速编写自己的BitmapData,如下所述:Working with BitmapData

修改

实际上,我的另一种方式是:你可以创建一个相同大小的纯白图像,然后通过计算和比较它们的哈希值来比较你的图像。看看这个:Comparing two images

答案 1 :(得分:1)

您可以使用Eutherpy答案中描述的方法。如果您想要执行更多图像操作并且需要高级图像库,则可能需要使用我创建的名为Magick.NET的ImageMagick C#包装器。

以下是如何在该库中检查它的一小部分示例:

private static bool IsWhite(MagickImage image)
{
  var white = MagickColors.White;

  using (var pixels = image.GetPixels())
  {
    foreach (var pixel in pixels)
    {
      var color = pixel.ToColor();
      if (color != white)
        return false;
    }
  }

  return true;
}

static void Main(string[] args)
{
  using (var image = new MagickImage(@"c:\folder\yourimage.png"))
  {
    if (IsWhite(image))
      Console.WriteLine("The image is all white");
  }
}

答案 2 :(得分:1)

从命令行,您可以运行

identify -verbose image

寻找

Channel statistics:
Pixels: 10
Gray:
  min: 65535 (1)
  max: 65535 (1)
  mean: 65535 (1)

如果图像有" min"那不是65535,那么它不是一个完全白色的图像。

答案 3 :(得分:1)

您可以通过要求Imagemagick告诉您答案来避免解析和循环以及两步测试。

如果像素的平均值为1.0(如果所有像素都是白色,则必须是)并且宽度为512且高度为512,则下面的测试将输出1,否则为0.

# Test a white 512x512 image => Result: 1
identify -format "%[fx:(mean==1)&&(w==512)&&(h==512)?1:0]" white.png
1

# Test a white 600x512 image => Result: 0
identify -format "%[fx:(mean==1)&&(w==512)&&(h==512)?1:0]" white600x512.png
0

# Test a gray image => Result: 0
identify -format "%[fx:(mean==1)&&(w==512)&&(h==512)?1:0]" gray90.png
0

答案 4 :(得分:1)

ImageMagick命令行中的另一种简单解决方案是仅将图像平均化为1个像素或计算图像的平均值,然后测试值是否为1(在0到1的范围内),其中1为白色,0为黑色。

创建测试图像

convert -size 512x512 xc:white white.png
convert -size 512x512 xc:black black.png
convert -size 512x512 xc:gray gray.png


方法1-缩放到1个像素:

convert white.png -scale 1x1! -format "%t = %[fx:u]\n" info:
white = 1

convert black.png -scale 1x1! -format "%t = %[fx:u]\n" info:
black = 0

convert gray.png -scale 1x1! -format "%t = %[fx:u]\n" info:
gray = 0.494118


方法2:

convert white.png -format "%t = %[fx:mean]\n" info:
white = 1

convert black.png -format "%t = %[fx:mean]\n" info:
black = 0

convert gray.png -format "%t = %[fx:mean]\n" info:
gray = 0.494118


您也可以在命令行中进行(三元)测试。 1为true,0为false。

test=$(convert white.png -format "%[fx:mean==1?1:0]\n" info:)
echo $test
1


使用Unix条件进行测试:

if [ $test -eq 1 ]; then
echo "full white"
else
echo "not white"
fi
full white


请注意,fx:是通用计算器,而u只是像素值。 t给出不带后缀的图像名称。

ImageMagick在Magick.Net模块中支持C#。参见https://github.com/dlemstra/Magick.NET

答案 5 :(得分:0)

使用此代码:

private book IsBlankImage(string path)
{
bool isBlank = false:
Image img = Image.FromFile(path);
Bitmap bmp = new Bitmap(img);
for(int x=0; x<bmp.Width;x++)
for(int y=0; y<bmp.Height;y++)
if(bmp.GetPixel(x, y).Name == "ffffffff")
isBlank = true;
else
isBlank = false;
return isBlank;
}

然后调用函数:

bool isBlank = isBlankImage("D:\\myImage.jpg");
if (isBlank == true)
MessageBox.Show("The Image is Blank");
else
MessageBox.Show("The Image is Not Blank");