AForge.NET中的“过滤器不支持源像素格式”错误

时间:2012-08-04 18:27:08

标签: c# aforge

我试图在Aforge中应用Bradleys阈值算法

每当我尝试处理图像时,我都会得到以下例外

  

抛出新的UnsupportedImageFormatException("源像素格式不是   由过滤器支持。" );

在应用算法之前,我使用以下方法对图像进行灰度级

private void button2_Click(object sender, EventArgs e)
{
    Grayscale filter = new Grayscale(0.2125, 0.7154, 0.0721);
    Bitmap grayImage = filter.Apply(img);

    pictureBox1.Image = grayImage;
}

算法调用的代码

public void bradley(ref Bitmap tmp)
{  
    BradleyLocalThresholding filter = new BradleyLocalThresholding();
    filter.ApplyInPlace(tmp);
}

我在图像处理实验室尝试了理智的图像,但它确实有效但不在我的系统上。

知道我做错了吗?

1 个答案:

答案 0 :(得分:5)

我已经使用以下代码在这种情况下获得更好的信息。它没有解决问题,但它至少提供了比AForge本身更有用的信息。

namespace AForge.Imaging.Filters {

    /// <summary>
    /// Provides utility methods to assist coding against the AForge.NET 
    /// Framework.
    /// </summary>
    public static class AForgeUtility {

        /// <summary>
        /// Makes a debug assertion that an image filter that implements 
        /// the <see cref="IFilterInformation"/> interface can 
        /// process an image with the specified <see cref="PixelFormat"/>.
        /// </summary>
        /// <param name="filterInfo">The filter under consideration.</param>
        /// <param name="format">The PixelFormat under consideration.</param>
        [Conditional("DEBUG")]
        public static void AssertCanApply(
            this IFilterInformation filterInfo, 
            PixelFormat format) {
            Debug.Assert(
                filterInfo.FormatTranslations.ContainsKey(format),
                string.Format("{0} cannot process an image " 
                    + "with the provided pixel format.  Provided "
                    + "format: {1}.  Accepted formats: {2}.",
                    filterInfo.GetType().Name,
                    format.ToString(),
                    string.Join( ", ", filterInfo.FormatTranslations.Keys)));
        }
    }
}

在您的情况下,您可以将其用作:

public void bradley(ref Bitmap tmp)
{  
    BradleyLocalThresholding filter = new BradleyLocalThresholding();
    filter.AssertCanApply( tmp.PixelFormat );
    filter.ApplyInPlace(tmp);
}