Пример #1
0
        //计算部分区域颜色
        public static Color GetMajorColor2(WriteableBitmap bitmap)
        {
            ImageBlur imageBlur = new ImageBlur(bitmap);

            //色相数组
            int[] majorHues = new int[361];
            for (int i = 0; i < majorHues.Length; i++)
            {
                majorHues[i] = 0;
            }

            int sat_sum = 0;
            int lig_sum = 0;
            int hue_sum = 0;
            int counts  = 0;

            //计算主色调
            for (int h = 0; h < bitmap.PixelHeight; h++)
            {
                for (int w = 0; w < bitmap.PixelWidth; w++)
                {
                    if (w > 880 & h < 40)
                    {
                        int hue = imageBlur.getPixelHue(w, h);
                        int sat = imageBlur.getPixelSat(w, h);
                        int lig = imageBlur.getPixelLig(w, h);

                        hue_sum += hue;
                        sat_sum += sat;
                        lig_sum += lig;
                        counts++;
                    }
                }
            }

            double finLig1 = Math.Sqrt(lig_sum / (counts)) * 10;
            int    finSat2 = Convert.ToInt32(Math.Sqrt(sat_sum / (counts)) * 10);
            int    finLig2 = Convert.ToInt32(Math.Sqrt(lig_sum / (counts)) * 10);

            int finLig = lig_sum / (counts);
            int finSat = sat_sum / (counts);
            int finHue = hue_sum / (counts);

            ColorAbouts colorAbouts = new ColorAbouts();

            ColorAbouts.RgbColor rgbColorMain = colorAbouts.HslToRgb(finHue, finSat2, finLig2);

            Color myColor = Color.FromArgb(255,
                                           Convert.ToByte(rgbColorMain.red),
                                           Convert.ToByte(rgbColorMain.green),
                                           Convert.ToByte(rgbColorMain.blue));

            return(myColor);
        }
Пример #2
0
        protected float[] ConvertImageWithPadding(ImageBlur imageIn, int width, int height)
        {
            int newheight = height + Padding * 2;
            int newwidth  = width + Padding * 2;

            float[] numArray = new float[(newheight * newwidth) * 3];
            int     index    = 0;
            int     num      = 0;

            for (int i = -3; num < newheight; i++)
            {
                int y = i;
                if (i < 0)
                {
                    y = 0;
                }
                else if (i >= height)
                {
                    y = height - 1;
                }
                int count      = 0;
                int negpadding = -1 * Padding;
                while (count < newwidth)
                {
                    int x = negpadding;
                    if (negpadding < 0)
                    {
                        x = 0;
                    }
                    else if (negpadding >= width)
                    {
                        x = width - 1;
                    }
                    numArray[index]     = imageIn.getRComponent(x, y) * 0.003921569f;
                    numArray[index + 1] = imageIn.getGComponent(x, y) * 0.003921569f;
                    numArray[index + 2] = imageIn.getBComponent(x, y) * 0.003921569f;

                    count++; negpadding++;
                    index += 3;
                }
                num++;
            }
            return(numArray);
        }
Пример #3
0
        //@Override
        public virtual ImageBlur process(ImageBlur imageIn)
        {
            int width  = imageIn.getWidth();
            int height = imageIn.getHeight();

            float[] imageArray = ConvertImageWithPadding(imageIn, width, height);
            imageArray = ApplyBlur(imageArray, width, height);
            int newwidth = width + Padding * 2;

            for (int i = 0; i < height; i++)
            {
                int num = ((i + 3) * newwidth) + 3;
                for (int j = 0; j < width; j++)
                {
                    int pos = (num + j) * 3;
                    imageIn.setPixelColor(j, i, (byte)(imageArray[pos] * 255f), (byte)(imageArray[pos + 1] * 255f), (byte)(imageArray[pos + 2] * 255f));
                }
            }
            return(imageIn);
        }
Пример #4
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="level">[0,40]</param>
        public async Task <WriteableBitmap> ApplyFilter(float level)
        {
            #region old

            /*
             * using (IRandomAccessStream fileStream = await file.OpenAsync(FileAccessMode.Read))
             * {
             *  BitmapDecoder decoder = await BitmapDecoder.CreateAsync(fileStream);
             *  // Scale image to appropriate size
             *  BitmapTransform transform = new BitmapTransform()
             *  {
             *      ScaledWidth = Convert.ToUInt32(wb.PixelWidth),
             *      ScaledHeight = Convert.ToUInt32(wb.PixelHeight)
             *  };
             *  PixelDataProvider pixelData = await decoder.GetPixelDataAsync(
             *      BitmapPixelFormat.Bgra8, // WriteableBitmap uses BGRA format
             *      BitmapAlphaMode.Straight,
             *      transform,
             *      ExifOrientationMode.IgnoreExifOrientation, // This sample ignores Exif orientation
             *      ColorManagementMode.DoNotColorManage
             *  );
             *
             *
             *
             *  // An array containing the decoded image data, which could be modified before being displayed
             *  byte[] sourcePixels = pixelData.DetachPixelData();
             *  using (Stream stream = wb.PixelBuffer.AsStream())
             *  {
             *      await stream.WriteAsync(sourcePixels, 0, sourcePixels.Length);
             *  }
             * }
             */
            #endregion

            // 拷贝
            WriteableBitmap new_bitmap = await Utility.BitmapClone(wb);

            // 添加高斯滤镜效果
            ImageBlur          mi     = new ImageBlur(new_bitmap);
            GaussianBlurFilter filter = new GaussianBlurFilter();
            filter.Sigma = level;
            filter.process(mi);

            // 图片添加完滤镜的 int[] 数组
            int[] array = mi.colorArray;

            // byte[] 数组的长度是 int[] 数组的 4倍
            byte[] result = new byte[array.Length * 4];

            // 通过自加,来遍历 byte[] 数组中的值
            int j = 0;
            for (int i = 0; i < array.Length; i++)
            {
                // 同时把 int 值中 a、r、g、b 的排列方式,转换为 byte数组中 b、g、r、a 的存储方式
                result[j++] = (byte)(array[i]);       // Blue
                result[j++] = (byte)(array[i] >> 8);  // Green
                result[j++] = (byte)(array[i] >> 16); // Red
                result[j++] = (byte)(array[i] >> 24); // Alpha
            }



            // Open a stream to copy the image contents to the WriteableBitmap's pixel buffer
            using (Stream stream = new_bitmap.PixelBuffer.AsStream())
            {
                await stream.WriteAsync(result, 0, result.Length);
            }

            return(new_bitmap);// 把 WriteableBitmap 对象赋值给 Image 控件

            // 用像素缓冲区的数据绘制图片
            //new_bitmap.Invalidate();
        }
Пример #5
0
        public static Color GetMajorColor(WriteableBitmap bitmap, StackPanel StatisticsGrid)
        {
            ImageBlur imageBlur = new ImageBlur(bitmap);

            //色相数组
            int[] majorHues = new int[361];
            for (int i = 0; i < majorHues.Length; i++)
            {
                majorHues[i] = 0;
            }
            int[] majorSatCounts = new int[361];
            for (int i = 0; i < majorSatCounts.Length; i++)
            {
                majorSatCounts[i] = 0;
            }

            double lum_sum       = 0;
            double imageCounts   = 0;
            int    darkLumCounts = 0;

            //计算主色调
            for (int h = 0; h < bitmap.PixelHeight; h++)
            {
                for (int w = 0; w < bitmap.PixelWidth; w++)
                {
                    int hue = imageBlur.getPixelHue(w, h);
                    int sat = imageBlur.getPixelSat(w, h);
                    int lum = imageBlur.getPixelLig(w, h);

                    imageCounts++;
                    lum_sum += lum;


                    if (lum > 10)
                    {
                        majorHues[hue] += sat;
                        majorSatCounts[hue]++;
                    }
                    else
                    {
                        darkLumCounts++;
                    }
                }
            }


            //比较得到最多的色调
            int hueMaxCounts = 0;
            int hueMax       = 0;

            for (int i = 0; i < majorHues.Length; i++)
            {
                if (majorHues[i] > hueMaxCounts)
                {
                    hueMaxCounts = majorHues[i];
                    hueMax       = i;
                }
            }



            int majorHue = hueMax;
            int majorSat = majorHues[hueMax] / majorSatCounts[hueMax];

            double majorLum = 0;

            majorLum = lum_sum / 600000;

            // double abcdef = 1 / 3;
            // int balanceLum = Convert.ToInt32(Math.Pow((2500*majorLum) - 125000, abcdef) +50);

            double aaaaaa     = 1 / 2;
            int    balanceLum = Convert.ToInt32(Math.Pow(100 * majorLum, aaaaaa));

            int balanceLum2 = Convert.ToInt32(majorLum);

            ColorAbouts colorAbouts = new ColorAbouts();

            ColorAbouts.RgbColor rgbColorMain = colorAbouts.HslToRgb(majorHue, majorSat, balanceLum2);

            Color myColor = Color.FromArgb(255,
                                           Convert.ToByte(rgbColorMain.red),
                                           Convert.ToByte(rgbColorMain.green),
                                           Convert.ToByte(rgbColorMain.blue));


            StatisticsGrid.Children.Clear();

            // 绘图部分
            for (int i = 0; i < majorSatCounts.Length; i++)
            {
                ColorAbouts colorAbouts2 = new ColorAbouts();

                int r = colorAbouts2.HslToRgb(i, 100, 100).red;
                int g = colorAbouts2.HslToRgb(i, 100, 100).green;
                int b = colorAbouts2.HslToRgb(i, 100, 100).blue;

                SolidColorBrush solidColorBrush = new SolidColorBrush(Color.FromArgb(255, Convert.ToByte(r), Convert.ToByte(g), Convert.ToByte(b)));

                var line1 = new Line
                {
                    Stroke = solidColorBrush,
                    X2     = Math.Pow(majorHues[i], 0.45) //这里有个值太小然后闪退的问题,复现:丢一张纯色图进去
                };
                StatisticsGrid.Children.Add(line1);
            }

            //绘制背景
            ColorAbouts colorAbouts3 = new ColorAbouts();

            int             r3 = colorAbouts3.HslToRgb(0, 0, balanceLum2).red;
            int             g3 = colorAbouts3.HslToRgb(0, 0, balanceLum2).green;
            int             b3 = colorAbouts3.HslToRgb(0, 0, balanceLum2).blue;
            SolidColorBrush solidColorBrush2 = new SolidColorBrush(Color.FromArgb(255, Convert.ToByte(r3), Convert.ToByte(g3), Convert.ToByte(b3)));

            StatisticsGrid.Background = solidColorBrush2;

            return(myColor);
        }