【问题标题】:Check if image is plain white?检查图像是否为纯白色?
【发布时间】:2020-12-15 21:53:38
【问题描述】:

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

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

【问题讨论】:

    标签: c# imagemagick


    【解决方案1】:

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

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

    编辑:

    实际上,我想到了另一种方法:您可以创建一个相同大小的纯白色图像,然后通过计算和比较它们的哈希值来将您的图像与该图像进行比较。看看这个:Comparing two images

    【讨论】:

    • 比较哈希的好主意!有用!谢谢
    • @OlegFilimonov 太棒了,我很高兴! :)
    【解决方案2】:

    您可以通过让 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
    

    【讨论】:

      【解决方案3】:

      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像素:

      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
      


      方法二:

      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 为真,0 为假。

      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

      【讨论】:

        【解决方案4】:

        从命令行,你可以运行

        identify -verbose image
        

        然后寻找

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

        如果图像的“最小值”不是 65535,则它不是全白图像。

        【讨论】:

          【解决方案5】:

          使用此代码:

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

          然后调用函数:

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

          【讨论】:

            【解决方案6】:

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

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

            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");
              }
            }
            

            【讨论】:

              猜你喜欢
              • 2021-05-05
              • 2013-10-02
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多