如何判断图像是否为90%黑色?

时间:2012-08-18 08:22:05

标签: image algorithm fuzzy-comparison

我以前从未做过图像处理。

我现在需要通过相机中的许多jpeg图像来丢弃那些非常暗(几乎是黑色)的图像。

我可以使用免费库(.NET)吗?感谢。

2 个答案:

答案 0 :(得分:3)

Aforge是一个很棒的图像处理库。特别是Aforge.Imaging程序集。 您可以尝试应用阈值过滤器,并使用区域或blob运算符并从那里进行比较。

答案 1 :(得分:1)

我需要做同样的事情。我想出了这个解决方案来标记大多数黑色图像。它就像一个魅力。您可以对其进行增强以删除或移动文件。

// set limit
const double limit = 90;

foreach (var img in Directory.EnumerateFiles(@"E:\", "*.jpg", SearchOption.AllDirectories))
{
    // load image
    var sourceImage = (Bitmap)Image.FromFile(img);

    // format image
    var filteredImage = AForge.Imaging.Image.Clone(sourceImage);

    // free source image
    sourceImage.Dispose();

    // get grayscale image
    filteredImage = Grayscale.CommonAlgorithms.RMY.Apply(filteredImage);

    // apply threshold filter
    new Threshold().ApplyInPlace(filteredImage);

    // gather statistics
    var stat = new ImageStatistics(filteredImage);
    var percentBlack = (1 - stat.PixelsCountWithoutBlack / (double)stat.PixelsCount) * 100;

    if (percentBlack >= limit)
        Console.WriteLine(img + " (" + Math.Round(percentBlack, 2) + "% Black)");

    filteredImage.Dispose();
}

Console.WriteLine("Done.");
Console.ReadLine();