Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"Source pixel format is not supported by the filter" error in AForge.NET

Tags:

c#

aforge

I'm trying to apply Bradleys thresholding algorithm in Aforge

Everytime I try to process the image I get the exception below

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

I grayscaled the image using the below method before applying the algorithm

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

The code for the algorithm call

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

I tried the sane image in image processing lab and it did work but not on my system.

Any idea what I'm doing wrong?

like image 221
LiveEn Avatar asked Aug 04 '12 18:08

LiveEn


1 Answers

I've used the following code to get better information in cases like this. It doesn't fix the problem, but it at least gives more helpful information than AForge does by itself.

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

In your case, you can use it as:

public void bradley(ref Bitmap tmp)
{  
    BradleyLocalThresholding filter = new BradleyLocalThresholding();
    filter.AssertCanApply( tmp.PixelFormat );
    filter.ApplyInPlace(tmp);
}
like image 142
Phil N. Avatar answered Nov 05 '22 11:11

Phil N.