繁体   English   中英

如何在图像上添加噪点或将其转换为24bpp?

[英]How to add noise to image or convert it to 24bpp?

新手需要帮助! 我有一张图像,需要在其上添加噪点。 我尝试使用AForge库执行此操作,但是此方法仅适用于24bpp位图,并且在调整大小后会得到一些不同的结果。 问题是如何将位图转换为24bpp或如何在其上添加噪声? 也许有一些库可以简化此过程。

调整:

private Image Fit(Image image)
{
    Image img = image;
    if (filepath != null)
    {
        if (img.Width > pictureBox1.Width)
        {
            double op = ((pictureBox1.Width - (pictureBox1.Width % 100)) % 100) + (pictureBox1.Width % 100) * 0.01;
            double percent = img.Width / (pictureBox1.Width * 0.01);
            double temp = ((percent - percent % 100 + 100) - percent) * pictureBox1.Height * 0.01;

            double height = pictureBox1.Height * 0.01 * ((percent - percent % 100 + 100) - percent);

            System.Drawing.Size sz = new Size(pictureBox1.Width, (int)height);
            img = resizeImage(img, sz);
        }
        if (img.Height > pictureBox1.Height)
        {
            double percent = img.Height / (pictureBox1.Height * 0.01);
            double temp = ((percent - percent % 100 + 100) - percent) * pictureBox1.Width * 0.01;

            double width = pictureBox1.Width * 0.01 * ((percent - percent % 100 + 100) - percent);

            System.Drawing.Size sz = new Size((int)width, pictureBox1.Height);
            img = resizeImage(img, sz);
        }
    }
    return img;
}

PS>我有一种错误-系统完全拒绝将1除以100,所以我必须将1除以0.01或得到0。

调整图像大小以保持纵横比非常容易。 您计算水平和垂直缩放比例,然后选择两者中的最小值。

double vscale = 1.0;
double hscale = 1.0;
if (img.Width > pictureBox1.Width)
{
    hscale = (double)pictureBox1.Width/img.Width;
}
if (img.Height > pictureBox1.Height)
{
    vscale = (double)pictureBox1.Height/img.Height;
}
double scale = Math.Min(hscale, vscale);
double width = scale * img.Width;
double height = scale * img.Height;

Size sz = new Size((int)width, (int)height);
img = resizeImage(img, sz)

请注意,仅当图像大于框时才缩放。 如果图像小于框,则不会缩放图像以使其适合框。

没发现什么好东西。 我就是这样解决的:

public void GenerateNoise(Image img, int intense)
        {
            Bitmap finalBmp = img as Bitmap;
            Random r = new Random();
            int width = img.Width;
            int height = img.Height;
            for (int x = 0; x < width; x++)
            {
                for (int y = 0; y < height; y++)
                {
                    int def = r.Next(0, 100);
                    if (def < intense)
                    {
                        int op = r.Next(0, 1);
                        if (op == 0)
                        {
                            int num = r.Next(0, intense);
                            Color clr = finalBmp.GetPixel(x, y);
                            int R = (clr.R + clr.R + num)/2;
                            if (R > 255) R = 255;
                            int G = (clr.G + clr.G + num) / 2;
                            if (G > 255) G = 255;
                            int B = (clr.B + clr.B + num) / 2;
                            if (B > 255) B = 255;
                            Color result = Color.FromArgb(255, R, G, B);
                            finalBmp.SetPixel(x, y, result);
                        }
                        else
                        {
                            int num = r.Next(0, intense);
                            Color clr = finalBmp.GetPixel(x, y);
                            Color result = Color.FromArgb(255, (clr.R + clr.R - num) / 2, (clr.G + clr.G - num) / 2,
                                (clr.B + clr.B - num) / 2);
                            finalBmp.SetPixel(x, y, result);
                        }
                    }
                }
            }

        }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM