How to determine the background color of document when there are 3 options, using c# or imagemagick

后端 未结 3 1196
Happy的楠姐
Happy的楠姐 2020-12-09 23:37

i am currently developing an application that has to process scanned forms. One of the tasks of my application is to determine which kind of form is scanned. There are 3 pos

3条回答
  •  情深已故
    2020-12-10 00:27

    I haven't tried this out, but how about resizing the image to 1x1 pixel (which should "average" out all the pixels) and then check the hue of that pixel to see if it closest to red, blue or green.

    EDIT

    I don't have ImageMagik installed, so I hacked this with GetThumbnailImage:

    private static bool ThumbnailCallback()
    {
        return false;
    }
    
    static void Main(string[] args)
    {
        var blueImage = Image.FromFile("blue.jpg").GetThumbnailImage(1, 1, new Image.GetThumbnailImageAbort(ThumbnailCallback), IntPtr.Zero);
        var blueBitmap = new Bitmap(blueImage);
        var blueHue = blueBitmap.GetPixel(0, 0).GetHue();
    
        var greenImage = Image.FromFile("green.jpg").GetThumbnailImage(1, 1, new Image.GetThumbnailImageAbort(ThumbnailCallback), IntPtr.Zero);
        var greenBitmap = new Bitmap(greenImage);
        var greenHue = greenBitmap.GetPixel(0, 0).GetHue();
    }
    

    Using your images I got a blueHue value of 169.0909 (Where true blue would be 180), and greenHue equal to 140 (pure green is 120, cyan is 150).

    Red forms should be somewhere near 0 or 360.

    I know you've already found an answer - just thought I'd give you an alternative.

提交回复
热议问题