コード例 #1
0
        public static Image SuggestedFilter(UserImage userImage)
        {
            var color = userImage.MainColor;

            var category = userImage.Category;

            Bitmap suggested = new Bitmap(userImage.BM);

            if (category == "Nature")
            {
                // 3x3 Sharpening Filter
                double factor = 1.0;
                int    bias   = 0;

                double[,] filterMatrix =
                    new double[, ] {
                    { 0, -1, 0, },
                    { -1, 5, -1, },
                    { 0, -1, 0, },
                };

                int r = color.R;
                int g = color.G;
                int b = color.B;

                List <int> RGB = new List <int> {
                    r, g, b
                };
                int maxIndex = RGB.IndexOf(RGB.Max());

                if (maxIndex == 0)
                {
                    ColorMatrix ColMat = new ColorMatrix(new float[][]
                    {
                        new float[] { 1.8f, 0, 0, 0, 0 }, //R red scaling factor
                        new float[] { 0, 1.5f, 0, 0, 0 }, //G green scaling factor
                        new float[] { 0, 0, 1, 0, 0 },    //B blue scaling factor
                        new float[] { 0, 0, 0, 1, 0 },    //A alpha scaling factor
                        new float[] { 0, 0, 0, 0, 1 },    //W translations
                    });

                    suggested = ApplyColorMatrix(suggested, ColMat);
                    suggested = ConvolutionFilter(suggested, filterMatrix, factor, bias, false);
                }
                else if (maxIndex == 1)
                {
                    ColorMatrix ColMat = new ColorMatrix(new float[][]
                    {
                        new float[] { 1, 0, 0, 0, 0 },    //R red scaling factor
                        new float[] { 0, 1.5f, 0, 0, 0 }, //G green scaling factor
                        new float[] { 0, 0, 1.3f, 0, 0 }, //B blue scaling factor
                        new float[] { 0, 0, 0, 1, 0 },    //A alpha scaling factor
                        new float[] { 0, 0, 0, 0, 1 },    //W translations
                    });

                    suggested = ApplyColorMatrix(suggested, ColMat);
                    suggested = ConvolutionFilter(suggested, filterMatrix, factor, bias, false);
                }
                else
                {
                    ColorMatrix ColMat = new ColorMatrix(new float[][]
                    {
                        new float[] { 1, 0, 0, 0, 0 },    //R red scaling factor
                        new float[] { 0, 1.5f, 0, 0, 0 }, //G green scaling factor
                        new float[] { 0, 0, 2, 0, 0 },    //B blue scaling factor
                        new float[] { 0, 0, 0, 1, 0 },    //A alpha scaling factor
                        new float[] { 0, 0, 0, 0, 1 },    //W translations
                    });

                    suggested = ApplyColorMatrix(suggested, ColMat);
                    suggested = AdjustSaturation(suggested, 5);
                    suggested = ConvolutionFilter(suggested, filterMatrix, factor, bias, false);
                }
            }
            else if (category == "Urban")
            {
                double factor = 1.0;

                int bias = 0;
                // 3x3 Sharpening Filter
                double[,] filterMatrix =
                    new double[, ] {
                    { 0, -1, 0, },
                    { -1, 5, -1, },
                    { 0, -1, 0, },
                };

                suggested = ConvolutionFilter(suggested, filterMatrix, factor, bias, false);
            }

            else if (category == "Face")
            {
                double factor = 1.0 / 8.0;

                int bias = 0;
                // 5x5 Sharpening Filter
                double[,] filterMatrix =
                    new double[, ] {
                    { -1, -1, -1, -1, -1, },
                    { -1, 2, 2, 2, -1, },
                    { -1, 2, 8, 2, 1, },
                    { -1, 2, 2, 2, -1, },
                    { -1, -1, -1, -1, -1, },
                };

                suggested = ConvolutionFilter(suggested, filterMatrix, factor, bias, false);
            }


            return(suggested);
        }