コード例 #1
0
        //反相
        private System.Drawing.Bitmap NegativeImage(System.Drawing.Bitmap bitmap)
        {
            int height = bitmap.Height;
            int width  = bitmap.Width;

            System.Drawing.Bitmap newBitmap = new System.Drawing.Bitmap(width, height);

            pointBitmap pointBitMap    = new pointBitmap(bitmap);
            pointBitmap newPointBitMap = new pointBitmap(newBitmap);

            pointBitMap.LockBits();
            newPointBitMap.LockBits();

            System.Drawing.Color pixel;

            for (int i = 1; i < width; i++)
            {
                for (int j = 1; j < height; j++)
                {
                    int RGB_R, RGB_G, RGB_B;
                    pixel = pointBitMap.GetPixel(i, j);

                    RGB_R = 255 - pixel.R;
                    RGB_G = 255 - pixel.G;
                    RGB_B = 255 - pixel.B;

                    newPointBitMap.SetPixel(i, j, System.Drawing.Color.FromArgb(RGB_R, RGB_G, RGB_B));
                }
            }
            pointBitMap.UnlockBits();
            newPointBitMap.UnlockBits();
            return(newBitmap);
        }
コード例 #2
0
        //灰度图
        private System.Drawing.Bitmap BlackImage(System.Drawing.Bitmap bitmap)
        {
            int height = bitmap.Height;
            int width  = bitmap.Width;

            System.Drawing.Bitmap newBitmap = new System.Drawing.Bitmap(width, height);

            pointBitmap pointBitMap    = new pointBitmap(bitmap);
            pointBitmap newPointBitMap = new pointBitmap(newBitmap);

            pointBitMap.LockBits();
            newPointBitMap.LockBits();

            System.Drawing.Color pixel;

            for (int i = 0; i < width; i++)
            {
                for (int j = 0; j < height; j++)
                {
                    pixel = pointBitMap.GetPixel(i, j);
                    int RGB_R, RGB_G, RGB_B, Result = 0;

                    RGB_R = pixel.R;
                    RGB_G = pixel.G;
                    RGB_B = pixel.B;

                    switch (2)
                    {
                    case 0:    //平均值法
                        Result = ((RGB_R + RGB_G + RGB_B) / 3);
                        break;

                    case 1:    //最大值法
                        Result = RGB_R > RGB_G ? RGB_R : RGB_G;
                        Result = Result > RGB_B ? Result : RGB_B;
                        break;

                    case 2:    //加权平均值法
                        Result = ((int)(0.3 * RGB_R) + (int)(0.59 * RGB_G) + (int)(0.11 * RGB_B));
                        break;
                    }
                    newPointBitMap.SetPixel(i, j, System.Drawing.Color.FromArgb(Result, Result, Result));
                }
            }
            pointBitMap.UnlockBits();
            newPointBitMap.UnlockBits();
            return(newBitmap);
        }
コード例 #3
0
        //IHS色彩空间转RGB色彩空间
        private System.Drawing.Bitmap RGBImage(System.Drawing.Bitmap bitmap, IHS[] imageIHS)
        {
            int height = bitmap.Height;
            int width  = bitmap.Width;

            IHS[] newIHS = imageIHS;

            System.Drawing.Bitmap newBitmap = new System.Drawing.Bitmap(width, height);

            pointBitmap pointBitMap    = new pointBitmap(bitmap);
            pointBitmap newPointBitMap = new pointBitmap(newBitmap);

            pointBitMap.LockBits();
            newPointBitMap.LockBits();

            System.Drawing.Color pixel;

            double round = 180 / Math.PI;

            for (int i = 1; i < width; i++)
            {
                for (int j = 1; j < height; j++)
                {
                    pixel = pointBitMap.GetPixel(i, j);

                    double RGB_R = 0;
                    double RGB_G = 0;
                    double RGB_B = 0;

                    //////////////////////////////////////////////////////////////

                    double I = newIHS[i * height + j].I;
                    double H = newIHS[i * height + j].H * 360;
                    double S = newIHS[i * height + j].S;



                    if (H <= 120 && H >= 0)
                    {
                        RGB_R = I * (1 + S * Math.Cos(H / round) / Math.Cos((60 - H) / round));
                        RGB_B = I * (1 - S);
                        RGB_G = 3 * I - RGB_R - RGB_B;
                    }
                    else if (H <= 240 && H >= 120)
                    {
                        RGB_G = I * (1 + S * Math.Cos((H - 120) / round) / Math.Cos((180 - H) / round));
                        RGB_R = I * (1 - S);
                        RGB_B = 3 * I - RGB_G - RGB_R;
                    }
                    else
                    {
                        RGB_B = I * (1 + S * Math.Cos((H - 240) / round) / Math.Cos((300 - H) / round));
                        RGB_G = I * (1 - S);
                        RGB_R = 3 * I - RGB_B - RGB_G;
                    }

                    //if (RGB_R > 1)
                    //{
                    //    RGB_R = 1;
                    //}
                    //if (RGB_R < 0)
                    //{
                    //    RGB_R = 0;
                    //}

                    //if (RGB_G > 1)
                    //{
                    //    RGB_G = 1;
                    //}
                    //if (RGB_G < 0)
                    //{
                    //    RGB_G = 0;
                    //}
                    //if (RGB_B > 1)
                    //{
                    //    RGB_B = 1;
                    //}
                    //if (RGB_B < 0)
                    //{
                    //    RGB_B = 0;
                    //}

                    //////////////////////////////////////////////////////////////

                    //if (imageIHS[i * height + j].S == 0)
                    //{
                    //    RGB_R = RGB_G = RGB_B = imageIHS[i * height + j].I;
                    //}
                    //else
                    //{
                    //    double sectorPos = imageIHS[i * height + j].H / 60.0;
                    //    int sectorNumber = (int)(Math.Floor(sectorPos));

                    //    double fractionalSector = sectorPos - sectorNumber;

                    //    double p = imageIHS[i * height + j].I * (1.0 - imageIHS[i * height + j].S);
                    //    double q = imageIHS[i * height + j].I * (1.0 - (imageIHS[i * height + j].S * fractionalSector));
                    //    double t = imageIHS[i * height + j].I * (1.0 - (imageIHS[i * height + j].S * (1 - fractionalSector)));

                    //    switch (sectorNumber)
                    //    {
                    //        case 0:
                    //            RGB_R = imageIHS[i * height + j].I;
                    //            RGB_G = t;
                    //            RGB_B = p;
                    //            break;
                    //        case 1:
                    //            RGB_R = q;
                    //            RGB_G = imageIHS[i * height + j].I;
                    //            RGB_B = p;
                    //            break;
                    //        case 2:
                    //            RGB_R = p;
                    //            RGB_G = imageIHS[i * height + j].I;
                    //            RGB_B = t;
                    //            break;
                    //        case 3:
                    //            RGB_R = p;
                    //            RGB_G = q;
                    //            RGB_B = imageIHS[i * height + j].I;
                    //            break;
                    //        case 4:
                    //            RGB_R = t;
                    //            RGB_G = p;
                    //            RGB_B = imageIHS[i * height + j].I;
                    //            break;
                    //        case 5:
                    //            RGB_R = imageIHS[i * height + j].I;
                    //            RGB_G = p;
                    //            RGB_B = q;
                    //            break;
                    //    }
                    //}

                    //////////////////////////////////////////////////////////////

                    if (double.IsNaN(RGB_R))
                    {
                        RGB_R = 0;
                    }
                    if (double.IsNaN(RGB_G))
                    {
                        RGB_G = 0;
                    }
                    if (double.IsNaN(RGB_B))
                    {
                        RGB_B = 0;
                    }

                    RGB_R *= 255;
                    RGB_G *= 255;
                    RGB_B *= 255;

                    int R = Convert.ToInt16(RGB_R);
                    int G = Convert.ToInt16(RGB_G);
                    int B = Convert.ToInt16(RGB_B);


                    R = R > 255 ? 255 : R;
                    R = R < 0 ? 0 : R;

                    G = G > 255 ? 255 : G;
                    G = G < 0 ? 0 : G;

                    B = B > 255 ? 255 : B;
                    B = B < 0 ? 0 : B;

                    newPointBitMap.SetPixel(i, j, System.Drawing.Color.FromArgb(R, G, B));
                }
            }
            pointBitMap.UnlockBits();
            newPointBitMap.UnlockBits();

            return(newBitmap);
        }
コード例 #4
0
        //RGB色彩空间转IHS色彩空间
        private IHS[] IHSImage(System.Drawing.Bitmap bitmap)
        {
            int height = bitmap.Height;
            int width  = bitmap.Width;

            IHS[] imageIHS = new IHS[height * width];

            System.Drawing.Bitmap newBitmap = new System.Drawing.Bitmap(width, height);

            pointBitmap pointBitMap    = new pointBitmap(bitmap);
            pointBitmap newPointBitMap = new pointBitmap(newBitmap);

            pointBitMap.LockBits();
            newPointBitMap.LockBits();

            System.Drawing.Color pixel;

            double theta;
            double round = 180 / Math.PI;

            for (int i = 1; i < width; i++)
            {
                for (int j = 1; j < height; j++)
                {
                    pixel = pointBitMap.GetPixel(i, j);

                    double RGB_R = (pixel.R / 255.0);
                    double RGB_G = (pixel.G / 255.0);
                    double RGB_B = (pixel.B / 255.0);

                    double Max = Math.Max(RGB_R, Math.Max(RGB_G, RGB_B));
                    double Min = Math.Min(RGB_R, Math.Min(RGB_G, RGB_B));

                    //////////////////////////////////////////////////////////

                    imageIHS[i * height + j].I = (RGB_R + RGB_G + RGB_B) / 3;

                    if (RGB_R == RGB_G && RGB_R == RGB_B)
                    {
                        theta = 0;
                    }
                    else
                    {
                        theta = Math.Acos((((RGB_R - RGB_G) + (RGB_R - RGB_B)) / 2) / Math.Sqrt((RGB_R - RGB_G) * (RGB_R - RGB_G) + (RGB_R - RGB_B) * (RGB_G - RGB_B))) * round;
                    }

                    if (RGB_B <= RGB_G)
                    {
                        imageIHS[i * height + j].H = theta / 360;
                    }
                    else
                    {
                        imageIHS[i * height + j].H = 1 - theta / 360;
                    }

                    imageIHS[i * height + j].S = 1 - 3 * Min / (RGB_R + RGB_G + RGB_B);

                    //////////////////////////////////////////////////////////

                    //imageIHS[10 * i + j].H = 0.0;
                    //if (Max == RGB_R && RGB_G >= RGB_B)
                    //{
                    //    if (Max - Min == 0) imageIHS[10 * i + j].H = 0.0;
                    //    else imageIHS[10 * i + j].H = 60 * (RGB_G - RGB_B) / (Max - Min);
                    //}
                    //else if (Max == RGB_R && RGB_G < RGB_B)
                    //{
                    //    imageIHS[10 * i + j].H = 60 * (RGB_G - RGB_B) / (Max - Min) + 360;
                    //}
                    //else if (Max == RGB_G)
                    //{
                    //    imageIHS[10 * i + j].H = 60 * (RGB_B - RGB_R) / (Max - Min) + 120;
                    //}
                    //else if (Max == RGB_B)
                    //{
                    //    imageIHS[10 * i + j].H = 60 * (RGB_R - RGB_G) / (Max - Min) + 240;
                    //}

                    //imageIHS[10 * i + j].S = (Max == 0) ? 0.0 : (1.0 - ((double)Min / (double)Max));
                    //imageIHS[10 * i + j].I = Max;

                    //////////////////////////////////////////////////////////
                }
            }

            pointBitMap.UnlockBits();
            newPointBitMap.UnlockBits();

            return(imageIHS);
        }
コード例 #5
0
        //柔化
        private System.Drawing.Bitmap SoftenImage(System.Drawing.Bitmap bitmap)
        {
            int height = bitmap.Height;
            int width  = bitmap.Width;

            System.Drawing.Bitmap newBitmap = new System.Drawing.Bitmap(width, height);

            pointBitmap pointBitMap    = new pointBitmap(bitmap);
            pointBitmap newPointBitMap = new pointBitmap(newBitmap);

            pointBitMap.LockBits();
            newPointBitMap.LockBits();

            System.Drawing.Color pixel;

            int[] template;
            int   ratio;

            //高斯算子
            int[] Gauss      = { 1, 2, 1, 2, 4, 2, 1, 2, 1 };
            int   ratioGauss = 16;

            //均值算子1
            int[] Average1      = { 9, 9, 9, 9, 9, 9, 9, 9, 9 };
            int   ratioAverage1 = 9;

            //均值算子2
            int[] Average2      = { 8, 8, 8, 8, 0, 8, 8, 8, 8 };
            int   ratioAverage2 = 8;

            switch (0)
            {
            case 0:    //高斯算子
                template = Gauss;
                ratio    = ratioGauss;
                break;

            case 1:    //均值算子1
                template = Average1;
                ratio    = ratioAverage1;
                break;

            case 2:    //均值算子2
                template = Average2;
                ratio    = ratioAverage2;
                break;
            }

            for (int i = 1; i < width - 1; i++)
            {
                for (int j = 1; j < height - 1; j++)
                {
                    int RGB_R = 0, RGB_G = 0, RGB_B = 0;
                    int Index = 0;

                    for (int column = -1; column <= 1; column++)
                    {
                        for (int row = -1; row <= 1; row++)
                        {
                            pixel = pointBitMap.GetPixel(i + row, j + column);

                            RGB_R += pixel.R * template[Index];
                            RGB_G += pixel.G * template[Index];
                            RGB_B += pixel.B * template[Index];

                            Index++;
                        }
                    }
                    RGB_R /= ratio;
                    RGB_G /= ratio;
                    RGB_B /= ratio;

                    //处理颜色值溢出
                    RGB_R = RGB_R > 255 ? 255 : RGB_R;
                    RGB_R = RGB_R < 0 ? 0 : RGB_R;

                    RGB_G = RGB_G > 255 ? 255 : RGB_G;
                    RGB_G = RGB_G < 0 ? 0 : RGB_G;

                    RGB_B = RGB_B > 255 ? 255 : RGB_B;
                    RGB_B = RGB_B < 0 ? 0 : RGB_B;

                    newPointBitMap.SetPixel(i - 1, j - 1, System.Drawing.Color.FromArgb(RGB_R, RGB_G, RGB_B));
                }
            }
            pointBitMap.UnlockBits();
            newPointBitMap.UnlockBits();
            return(newBitmap);
        }
コード例 #6
0
        //边缘提取
        private System.Drawing.Bitmap SharpenImage(System.Drawing.Bitmap bitmap)
        {
            int height = bitmap.Height;
            int width  = bitmap.Width;

            System.Drawing.Bitmap newBitmap = new System.Drawing.Bitmap(width, height);

            pointBitmap pointBitMap    = new pointBitmap(bitmap);
            pointBitmap newPointBitMap = new pointBitmap(newBitmap);

            pointBitMap.LockBits();
            newPointBitMap.LockBits();

            System.Drawing.Color pixel;

            int[] template;

            //Laplace算子
            int[] Laplacian1 = { -1, -1, -1, -1, 9, -1, -1, -1, -1 };
            int[] Laplacian2 = { 0, 1, 0, 1, -4, 1, 0, 1, 0 };

            //Prewitt算子
            int[] Prewitt1 = { -1, -1, -1, 0, 0, 0, 1, 1, 1 };
            int[] Prewitt2 = { -1, 0, 1, -1, 0, 1, -1, 0, 1 };

            //Soble算子
            int[] Soble1 = { 1, 2, 1, 0, 0, 0, -1, -2, -1 };
            int[] Soble2 = { -1, 0, 1, -2, 0, 2, -1, 0, 1 };

            //边缘提取
            int[] Soble3 = { 0, 1, 0, 1, -4, 1, 0, 1, 0 };

            switch (6)
            {
            case 0:    //Laplace1算子
                template = Laplacian1;
                break;

            case 1:    //Laplace2算子
                template = Laplacian2;
                break;

            case 2:    //Prewitt1算子
                template = Prewitt1;
                break;

            case 3:    //Prewitt2算子
                template = Prewitt1;
                break;

            case 4:    //Soble1算子
                template = Prewitt1;
                break;

            case 5:    //Soble2算子
                template = Prewitt1;
                break;

            case 6:    //Soble3算子
                template = Prewitt1;
                break;
            }

            for (int i = 1; i < width - 1; i++)
            {
                for (int j = 1; j < height - 1; j++)
                {
                    int RGB_R = 0, RGB_G = 0, RGB_B = 0;
                    int Index = 0;

                    for (int column = -1; column <= 1; column++)
                    {
                        for (int row = -1; row <= 1; row++)
                        {
                            pixel = pointBitMap.GetPixel(i + row, j + column);

                            RGB_R += pixel.R * template[Index];
                            RGB_G += pixel.G * template[Index];
                            RGB_B += pixel.B * template[Index];

                            Index++;
                        }
                    }

                    //处理颜色值溢出
                    RGB_R = RGB_R > 255 ? 255 : RGB_R;
                    RGB_R = RGB_R < 0 ? 0 : RGB_R;

                    RGB_G = RGB_G > 255 ? 255 : RGB_G;
                    RGB_G = RGB_G < 0 ? 0 : RGB_G;

                    RGB_B = RGB_B > 255 ? 255 : RGB_B;
                    RGB_B = RGB_B < 0 ? 0 : RGB_B;

                    newPointBitMap.SetPixel(i - 1, j - 1, System.Drawing.Color.FromArgb(RGB_R, RGB_G, RGB_B));
                }
            }
            pointBitMap.UnlockBits();
            newPointBitMap.UnlockBits();
            return(newBitmap);
        }
コード例 #7
0
        //**功能**/**//////////////////////////////////////////////////////////////////////

        //灰度值拉伸
        private System.Drawing.Bitmap GrayStretchImage(System.Drawing.Bitmap bitmap)
        {
            int height = bitmap.Height;
            int width  = bitmap.Width;

            MinGrayValues = 255;
            MaxGrayValues = 0;

            double slope = 1.0;

            System.Drawing.Bitmap newBitmap = new System.Drawing.Bitmap(width, height);

            pointBitmap pointBitMap    = new pointBitmap(bitmap);
            pointBitmap newPointBitMap = new pointBitmap(newBitmap);

            pointBitMap.LockBits();
            newPointBitMap.LockBits();

            System.Drawing.Color pixel;

            for (int i = 1; i < width - 1; i++)
            {
                for (int j = 1; j < height - 1; j++)
                {
                    pixel = pointBitMap.GetPixel(i, j);

                    if (MinGrayValues > pixel.R)
                    {
                        MinGrayValues = pixel.R;
                    }
                    if (MinGrayValues > pixel.G)
                    {
                        MinGrayValues = pixel.G;
                    }
                    if (MinGrayValues > pixel.B)
                    {
                        MinGrayValues = pixel.B;
                    }

                    if (MaxGrayValues < pixel.R)
                    {
                        MaxGrayValues = pixel.R;
                    }
                    if (MaxGrayValues < pixel.G)
                    {
                        MaxGrayValues = pixel.G;
                    }
                    if (MaxGrayValues < pixel.B)
                    {
                        MaxGrayValues = pixel.B;
                    }
                }
            }

            GrayWindow GrayWindow = new GrayWindow();

            GrayWindow.ShowDialog();

            this.Cursor = Cursors.Wait;

            if (MaxNewValues != MaxGrayValues || MinNewValues != MinGrayValues)
            {
                slope = Convert.ToDouble((MaxNewValues - MinNewValues)) / Convert.ToDouble((MaxGrayValues - MinGrayValues));

                for (int i = 1; i < width - 1; i++)
                {
                    for (int j = 1; j < height - 1; j++)
                    {
                        int RGB_R = 0, RGB_G = 0, RGB_B = 0;

                        pixel = pointBitMap.GetPixel(i, j);

                        RGB_R = Convert.ToInt16(slope * (pixel.R - MinGrayValues) + MinNewValues);
                        RGB_G = Convert.ToInt16(slope * (pixel.G - MinGrayValues) + MinNewValues);
                        RGB_B = Convert.ToInt16(slope * (pixel.B - MinGrayValues) + MinNewValues);

                        newPointBitMap.SetPixel(i - 1, j - 1, System.Drawing.Color.FromArgb(RGB_R, RGB_G, RGB_B));
                    }
                }

                pointBitMap.UnlockBits();
                newPointBitMap.UnlockBits();
                return(newBitmap);
            }
            else
            {
                pointBitMap.UnlockBits();
                newPointBitMap.UnlockBits();
                return(bitmap);
            }
        }