How to sharpen a Bitmap? [closed]

萝らか妹 提交于 2019-12-12 04:17:03

问题


I am now developing an app for Android in Xamarin and I am stuck on a little step. I was searching on the interwebs for a sharpening alogrithm for MonoDroid and everything I find doesn`t seem to work. Can anybody provide me with some as-is script or some documentation? Thanks in advance.


回答1:


The great answer by Mohit is targeted at the .NET System.Drawing.Bitmap class. Android has a different drawing class and representation of Color. You can find documentation on Color here: https://developer.xamarin.com/api/type/Android.Graphics.Color/

The A, R, G, and B values from a color can be obtained with:

int color = bitmap.GetPixel(x, y);
var androidColor = new Color(color);
byte r = androidColor.R;
byte g = androidColor.G;
byte b = androidColor.B;
byte alpha = androidColor.A;

It should be possible to use the provided algorithm as-is with those modifications in place.




回答2:


This might be helpful to you

public static Bitmap ImageSharpen(Bitmap InpImg)
{
    Bitmap sharpenImage = new Bitmap(InpImg.Width, InpImg.Height);

    int wdth = InpImg.Width;
    int hght = InpImg.Height;

    double[,] filter = new double[3, 3];

    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[InpImg.Width, InpImg.Height];

    for (int x = 0; x < wdth; ++x)
    {
        for (int y = 0; y < hght; ++y)
        {
            double red = 0.0, green = 0.0, blue = 0.0;
            Color imageColor = InpImg.GetPixel(x, y);
            for (int filterX = 0; filterX < 3; filterX++)
            {
                for (int filterY = 0; filterY < 3; filterY++)
                {
                    int imageX = (x - 3 / 2 + filterX + wdth) % wdth;
                    int imageY = (y - 3 / 2 + filterY + hght) % hght;
                    Color imageColor = InpImg.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 < wdth; ++i)
    {
        for (int j = 0; j < hght; ++j)
        {
            sharpenImage.SetPixel(i, j, result[i, j]);
        }
    }
    return sharpenImage;
}


来源:https://stackoverflow.com/questions/34848720/how-to-sharpen-a-bitmap

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!