锐化使用C#位图 [英] Sharpen on a Bitmap using C#

查看:210
本文介绍了锐化使用C#位图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想提出一个锐化滤镜的图像上。我发现用简短的教程网络
我试着做它在C#所以这里是我的code。反正,我试图找出为什么它不工作。我不知道如果我做错了什么,如果是,请告诉我怎样做才能让它工作,因为它应该是。谢谢

 公共静态位图锐化(位图图像​​)
    {
        位图sharpenImage =新位图(image.Width,image.Height);        INT filterWidth = 3;
        INT filterHeight = 3;
        INT W = image.Width;
        INT H = image.Height;        双[,] =过滤器新的双[filterWidth,filterHeight]        过滤[0,0] =过滤[0,1] =过滤[0,2] =过滤[1,0] =滤波器[1,2] =滤波器[2,0] =过滤[2,1] =滤波器[2,2] = -1;
        滤波[1,1] = 9;        双因子= 1.0;
        双偏压= 0.0;        颜色[,]结果=新的色彩[image.Width,image.Height]        为(中间体X = 0; X&下;瓦; ++ x)的
        {
            对于(INT Y = 0; Y< H ++ Y)
            {
                双红= 0.0,绿= 0.0,蓝色= 0.0;
                色imageColor = image.GetPixel(X,Y);                对(INT filterX = 0; filterX&下; filterWidth; filterX ++)
                {
                    对(INT filterY = 0; filterY&下; filterHeight; filterY ++)
                    {
                        INT IMAGEX =(X - filterWidth / 2 + filterX + W)%(重量);
                        INT宜美=(Y - filterHeight / 2 + filterY + H)%H;
                        红色+ = imageColor.R *过滤器[filterX,filterY]
                        绿色+ = imageColor.G *过滤器[filterX,filterY]
                        蓝+ = imageColor.B *过滤器[filterX,filterY]
                    }
                    INT R = Math.Min(Math.Max​​((INT)(因子×红+偏差),0),255);
                    INT G = Math.Min(Math.Max​​((INT)(*系数+绿色偏差),0),255);
                    INT B = Math.Min(Math.Max​​((INT)(因子×蓝+偏差),0),255);                    结果[X,Y] = Color.FromArgb(R,G,B);
                }
            }
        }
        的for(int i = 0; I<瓦; ++ I)
        {
            对于(INT J = 0; J< H ++ j)条
            {
                sharpenImage.SetPixel(I,J,导致[I,J]);
            }
        }
        返回sharpenImage;
    }


 公共静态位图锐化(位图图像​​)
{
    位图sharpenImage =新位图(image.Width,image.Height);    INT filterWidth = 3;
    INT filterHeight = 3;
    INT W = image.Width;
    INT H = image.Height;    双[,] =过滤器新的双[filterWidth,filterHeight]    过滤[0,0] =过滤[0,1] =过滤[0,2] =过滤[1,0] =滤波器[1,2] =滤波器[2,0] =过滤[2,1] =滤波器[2,2] = -1;
    滤波[1,1] = 9;    双因子= 1.0;
    双偏压= 0.0;    颜色[,]结果=新的色彩[image.Width,image.Height]    为(中间体X = 0; X&下;瓦; ++ x)的
    {
        对于(INT Y = 0; Y< H ++ Y)
        {
            双红= 0.0,绿= 0.0,蓝色= 0.0;// ===== [REMOVE线] ======================================= =================
//颜色必须按照过滤器条目被读取,而不是每个图像像素。
            色imageColor = image.GetPixel(X,Y);
// ================================================ ===========================            对(INT filterX = 0; filterX&下; filterWidth; filterX ++)
            {
                对(INT filterY = 0; filterY&下; filterHeight; filterY ++)
                {
                    INT IMAGEX =(X - filterWidth / 2 + filterX + W)%(重量);
                    INT宜美=(Y - filterHeight / 2 + filterY + H)%H;// ===== [插入线] ======================================= =================
//此处获取颜色 - 每一次进入fiter和图像像素。
                    颜色imageColor = image.GetPixel(IMAGEX,宜美);
// ================================================ ===========================                    红色+ = imageColor.R *过滤器[filterX,filterY]
                    绿色+ = imageColor.G *过滤器[filterX,filterY]
                    蓝+ = imageColor.B *过滤器[filterX,filterY]
                }
                INT R = Math.Min(Math.Max​​((INT)(因子×红+偏差),0),255);
                INT G = Math.Min(Math.Max​​((INT)(*系数+绿色偏差),0),255);
                INT B = Math.Min(Math.Max​​((INT)(因子×蓝+偏差),0),255);                结果[X,Y] = Color.FromArgb(R,G,B);
            }
        }
    }
    的for(int i = 0; I<瓦; ++ I)
    {
        对于(INT J = 0; J< H ++ j)条
        {
            sharpenImage.SetPixel(I,J,导致[I,J]);
        }
    }
    返回sharpenImage;
}

I want to put a sharpen filter on an image. I have found a web with short tutorial. I tried to do it in C# so here is my code. Anyway, I tried to find out why it is not working. I do not know if I am doing something wrong, if yes, please advise me what to do to make it work as it should be. Thanks

        public static Bitmap sharpen(Bitmap image)
    {
        Bitmap sharpenImage = new Bitmap(image.Width, image.Height);

        int filterWidth = 3;
        int filterHeight = 3;
        int w = image.Width;
        int h = image.Height;

        double[,] filter = new double[filterWidth, filterHeight];

        filter[0, 0] = filter[0, 1] = filter[0, 2] = filter[1, 0] = filter[1, 2] = filter[2, 0] = filter[2, 1] = filter[2, 2] = -1;
        filter[1, 1] = 9;

        double factor = 1.0;
        double bias = 0.0;

        Color[,] result = new Color[image.Width, image.Height];

        for (int x = 0; x < w; ++x)
        {
            for (int y = 0; y < h; ++y)
            {
                double red = 0.0, green = 0.0, blue = 0.0;
                Color imageColor = image.GetPixel(x, y);

                for (int filterX = 0; filterX < filterWidth; filterX++)
                {
                    for (int filterY = 0; filterY < filterHeight; filterY++)
                    {
                        int imageX = (x - filterWidth / 2 + filterX + w) % w;
                        int imageY = (y - filterHeight / 2 + filterY + h) % h;
                        red += imageColor.R * filter[filterX, filterY];
                        green += imageColor.G * filter[filterX, filterY];
                        blue += imageColor.B * filter[filterX, filterY];
                    }
                    int r = Math.Min(Math.Max((int)(factor * red + bias), 0), 255);
                    int g = Math.Min(Math.Max((int)(factor * green + bias), 0), 255);
                    int b = Math.Min(Math.Max((int)(factor * blue + bias), 0), 255);

                    result[x, y] = Color.FromArgb(r, g, b);
                }
            }
        }
        for (int i = 0; i < w; ++i)
        {
            for (int j = 0; j < h; ++j)
            {
                sharpenImage.SetPixel(i, j, result[i, j]);
            }
        }
        return sharpenImage;
    }

解决方案

public static Bitmap sharpen(Bitmap image)
{
    Bitmap sharpenImage = new Bitmap(image.Width, image.Height);

    int filterWidth = 3;
    int filterHeight = 3;
    int w = image.Width;
    int h = image.Height;

    double[,] filter = new double[filterWidth, filterHeight];

    filter[0, 0] = filter[0, 1] = filter[0, 2] = filter[1, 0] = filter[1, 2] = filter[2, 0] = filter[2, 1] = filter[2, 2] = -1;
    filter[1, 1] = 9;

    double factor = 1.0;
    double bias = 0.0;

    Color[,] result = new Color[image.Width, image.Height];

    for (int x = 0; x < w; ++x)
    {
        for (int y = 0; y < h; ++y)
        {
            double red = 0.0, green = 0.0, blue = 0.0;

//=====[REMOVE LINES]========================================================
// Color must be read per filter entry, not per image pixel.
            Color imageColor = image.GetPixel(x, y);
//===========================================================================

            for (int filterX = 0; filterX < filterWidth; filterX++)
            {
                for (int filterY = 0; filterY < filterHeight; filterY++)
                {
                    int imageX = (x - filterWidth / 2 + filterX + w) % w;
                    int imageY = (y - filterHeight / 2 + filterY + h) % h;

//=====[INSERT LINES]========================================================
// Get the color here - once per fiter entry and image pixel.
                    Color imageColor = image.GetPixel(imageX, imageY);
//===========================================================================

                    red += imageColor.R * filter[filterX, filterY];
                    green += imageColor.G * filter[filterX, filterY];
                    blue += imageColor.B * filter[filterX, filterY];
                }
                int r = Math.Min(Math.Max((int)(factor * red + bias), 0), 255);
                int g = Math.Min(Math.Max((int)(factor * green + bias), 0), 255);
                int b = Math.Min(Math.Max((int)(factor * blue + bias), 0), 255);

                result[x, y] = Color.FromArgb(r, g, b);
            }
        }
    }
    for (int i = 0; i < w; ++i)
    {
        for (int j = 0; j < h; ++j)
        {
            sharpenImage.SetPixel(i, j, result[i, j]);
        }
    }
    return sharpenImage;
}

这篇关于锐化使用C#位图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆