Exemplo n.º 1
0
        public void ApplyMask(double[][] maskH, double[][] maskV, int treshe = 0)
        {
            Color[][] colorsH = ImageExtension.GetolorMatrix(MaskApplier.ApplyMaskForAllChanales(BitmapImg, maskH, treshe));
            Color[][] colorsV = ImageExtension.GetolorMatrix(MaskApplier.ApplyMaskForAllChanales(BitmapImg, maskV, treshe));
            ResultImg = new Bitmap(BitmapImg);

            if (chanel == ColorChannel.All)
            {
                for (int i = 0; i < colorsH.Length; i++)
                {
                    for (int j = 0; j < colorsH[0].Length; j++)
                    {
                        var r = Math.Sqrt((colorsH[i][j].R * colorsH[i][j].R) + (colorsV[i][j].R * colorsV[i][j].R));
                        var g = Math.Sqrt((colorsH[i][j].G * colorsH[i][j].G) + (colorsV[i][j].G * colorsV[i][j].G));
                        var b = Math.Sqrt((colorsH[i][j].B * colorsH[i][j].B) + (colorsV[i][j].B * colorsV[i][j].B));

                        ResultImg.SetPixel(i, j, Color.FromArgb(255, (byte)r, (byte)g, (byte)b));
                    }
                }
            }
            else
            {
                var initialColors = ImageExtension.GetolorMatrix(BitmapImg);
                for (int i = 0; i < colorsH.Length; i++)
                {
                    for (int j = 0; j < colorsH[0].Length; j++)
                    {
                        var r = (chanel == ColorChannel.Red)
                            ? Math.Sqrt((colorsH[i][j].R * colorsH[i][j].R) + (colorsV[i][j].R * colorsV[i][j].R))
                            : initialColors[i][j].R;
                        var g = (chanel == ColorChannel.Green)
                            ? Math.Sqrt((colorsH[i][j].G * colorsH[i][j].G) + (colorsV[i][j].G * colorsV[i][j].G))
                            : initialColors[i][j].G;
                        var b = (chanel == ColorChannel.Blue)
                            ? Math.Sqrt((colorsH[i][j].B * colorsH[i][j].B) + (colorsV[i][j].B * colorsV[i][j].B))
                            : initialColors[i][j].B;

                        ResultImg.SetPixel(i, j, Color.FromArgb(255, (byte)r, (byte)g, (byte)b));
                    }
                }
            }

            var reHisto = HistogramCalc.GetHistogram(ResultImg, Chanel);

            var values = new ChartValues <ObservableValue>();

            foreach (var item in reHisto)
            {
                values.Add(new ObservableValue(item));
            }

            WorkCollection = new SeriesCollection
            {
                new ColumnSeries
                {
                    Values = values
                }
            };
        }
Exemplo n.º 2
0
        public static Bitmap ApplyMaskForAllChanales(Bitmap img, double[][] mask, int treshe = 0)
        {
            Bitmap resBmp = new Bitmap(img);
            var    colors = ImageExtension.GetolorMatrix(img);
            int    r_tr   = 0;
            int    g_tr   = 0;
            int    b_tr   = 0;

            for (int i = 0; i < colors.Length; i++)
            {
                for (int j = 0; j < colors[0].Length; j++)
                {
                    if (colors[i][j].R > r_tr)
                    {
                        r_tr = colors[i][j].R;
                    }
                    if (colors[i][j].G > g_tr)
                    {
                        g_tr = colors[i][j].G;
                    }
                    if (colors[i][j].B > b_tr)
                    {
                        b_tr = colors[i][j].B;
                    }
                }
            }

            r_tr /= 2;
            g_tr /= 2;
            b_tr /= 2;

            if (treshe != 0)
            {
                r_tr = treshe;
                g_tr = treshe;
                b_tr = treshe;
            }

            for (int i = 0; i < colors.Length; i++)
            {
                for (int j = 0; j < colors[0].Length; j++)
                {
                    var itemMatrix = getItemMatrix(i, j, colors, mask.Length);
                    var newRValue  = ItemCalculation(itemMatrix, mask, r_tr, ColorChannel.Red);
                    var bval       = (byte)newRValue;
                    var newGValue  = (byte)ItemCalculation(itemMatrix, mask, g_tr, ColorChannel.Green);
                    var newBValue  = (byte)ItemCalculation(itemMatrix, mask, b_tr, ColorChannel.Blue);

                    var newVal = (newRValue > 0 || newGValue > 0 || newBValue > 0) ? 255 : 0;

                    Color newColor;
                    newColor = Color.FromArgb(colors[i][j].A, (byte)newVal, newVal, newVal);

                    resBmp.SetPixel(i, j, newColor);
                }
            }

            return(resBmp);
        }
Exemplo n.º 3
0
        public static int[] GetHistogram(Bitmap bmp, ColorChannel colorChannel)
        {
            int[] myHistogram = new int[256];
            for (int i = 0; i < myHistogram.Length; i++)
            {
                myHistogram[i] = 0;
            }

            var col    = (int)colorChannel;
            var colors = ImageExtension.GetolorMatrix(bmp);

            for (int i = 0; i < colors.Length; i++)
            {
                for (int j = 0; j < colors[0].Length; j++)
                {
                    switch (col)
                    {
                    case 0:
                    {
                        ++myHistogram[colors[i][j].B];
                        break;
                    }

                    case 1:
                    {
                        ++myHistogram[colors[i][j].G];
                        break;
                    }

                    case 2:
                    {
                        ++myHistogram[colors[i][j].R];
                        break;
                    }

                    case 3:
                    {
                        ++myHistogram[colors[i][j].A];
                        break;
                    }

                    default:
                        continue;
                    }
                }
            }

            return(myHistogram);
        }
Exemplo n.º 4
0
        public static Bitmap ApplyMask(Bitmap img, double[][] mask, ColorChannel colorChannel, int treshe = 0)
        {
            Bitmap resBmp = new Bitmap(img);

            var col    = (int)colorChannel;
            var colors = ImageExtension.GetolorMatrix(img);
            int r_tr   = 0;
            int g_tr   = 0;
            int b_tr   = 0;

            for (int _i = 0; _i < colors.Length; _i++)
            {
                for (int _j = 0; _j < colors[0].Length; _j++)
                {
                    if (colors[_i][_j].R > r_tr)
                    {
                        r_tr = colors[_i][_j].R;
                    }
                    if (colors[_i][_j].G > g_tr)
                    {
                        g_tr = colors[_i][_j].G;
                    }
                    if (colors[_i][_j].B > b_tr)
                    {
                        b_tr = colors[_i][_j].B;
                    }
                }
            }

            r_tr /= 2;
            g_tr /= 2;
            b_tr /= 2;
            int tr = (treshe != 0) ? treshe : (r_tr + g_tr + b_tr) / 3;

            for (int i = 0; i < colors.Length; i++)
            {
                for (int j = 0; j < colors[0].Length; j++)
                {
                    var itemMatrix = getItemMatrix(i, j, colors, mask.Length);
                    var newValue   = (byte)ItemCalculation(itemMatrix, mask, treshe, colorChannel);

                    Color newColor;

                    switch ((int)colorChannel)
                    {
                    //blue
                    case 0:
                    {
                        newColor = Color.FromArgb(colors[i][j].A, colors[i][j].R, colors[i][j].G, newValue);
                        break;
                    }

                    // green
                    case 1:
                    {
                        newColor = Color.FromArgb(colors[i][j].A, colors[i][j].R, newValue, colors[i][j].B);
                        break;
                    }

                    // red
                    case 2:
                    {
                        newColor = Color.FromArgb(colors[i][j].A, newValue, colors[i][j].G, colors[i][j].B);
                        break;
                    }

                    default:
                    {
                        newColor = Color.FromArgb(colors[i][j].A, colors[i][j].R, colors[i][j].G, colors[i][j].B);
                        break;
                    }
                    }

                    resBmp.SetPixel(i, j, newColor);
                }
            }

            return(resBmp);
        }
Exemplo n.º 5
0
        public static Bitmap EkvilizeCustom(Bitmap bmp, int[] histogramValues, ColorChannel chanel)
        {
            Bitmap resultBmp = new Bitmap(bmp);

            int[] cdf    = new int[histogramValues.Length];
            int   cdfMin = 0;

            for (int i = 0; i < histogramValues.Length; i++)
            {
                cdf[i]  = (i == 0) ? 0 : cdf[i - 1];
                cdf[i] += histogramValues[i];

                if (cdf[i] > 0 && (cdfMin == 0 || cdf[i] < cdfMin))
                {
                    cdfMin = cdf[i];
                }
            }

            Color[][] colors      = ImageExtension.GetolorMatrix(bmp);
            int       countPixels = colors.Length * colors[0].Length;

            for (int i = 0; i < colors.Length; i++)
            {
                for (int j = 0; j < colors[i].Length; j++)
                {
                    byte R = colors[i][j].R;
                    byte G = colors[i][j].G;
                    byte B = colors[i][j].B;

                    switch (chanel)
                    {
                    case ColorChannel.Blue:
                    {
                        B = (byte)((int)((((double)(cdf[colors[i][j].B] - cdfMin)) / ((double)countPixels))
                                         * ((double)(CountColors - 1))));
                        break;
                    }

                    case ColorChannel.Green:
                    {
                        G = (byte)((int)((((double)(cdf[colors[i][j].G] - cdfMin)) / ((double)countPixels))
                                         * ((double)(CountColors - 1))));
                        break;
                    }

                    case ColorChannel.Red:
                    {
                        R = (byte)((int)((((double)(cdf[colors[i][j].R] - cdfMin)) / ((double)countPixels))
                                         * ((double)(CountColors - 1))));
                        break;
                    }

                    default:
                        break;
                    }

                    resultBmp.SetPixel(i, j, Color.FromArgb(colors[i][j].A, R, G, B));
                }
            }

            return(resultBmp);
        }