c# - AForge.NET 中的 "Source pixel format is not supported by the filter"错误

标签 c# aforge

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

每次我尝试处理图像时都会出现以下异常

throw new UnsupportedImageFormatException( "Source pixel format is not supported by the filter." );

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

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

我在图像处理实验室中尝试了 sane image,它确实可以工作,但在我的系统上不行。

知道我做错了什么吗?

最佳答案

在这种情况下,我使用了以下代码来获取更好的信息。它不能解决问题,但至少比 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);
}

关于c# - AForge.NET 中的 "Source pixel format is not supported by the filter"错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11811052/

相关文章:

c# - 在另一个图像中查找图像

memory-leaks - AForge相机内存泄漏

c# - 如何以编程方式刷新组合框的项目源的绑定(bind)?

c# - 共享正则表达式(C# 到 JS)- 始终为 false - 这里有什么问题?

c# - 在设计时获取文件的相对路径

c# - 如何更新 EF 6 中的外键 - Code First - 一对多关系

c# - 线程.BeginThreadAffinity

c# - 如何将图像的单个字母旋转到正确的方向以获得最佳 OCR?

c# - 面部形状分割

c# - 使 AForge VideoCaptureDevice 与 Unity 一起工作