如何在Asp.net C#中减少图像噪声(jpg或Png) [英] How to reduce noise from image (jpg or Png) in Asp.net C#

查看:202
本文介绍了如何在Asp.net C#中减少图像噪声(jpg或Png)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好

我正在使用网络摄像头捕捉图像。我必须使用OCR从捕获的图像中读取文本。但问题是。

I am capturing an image using Web cam. i have to read text from captured image using OCR. But the issue is.

在某些图像中有噪音或谷物,图像质量不太好。我也尝试过1080p高清网络摄像头,但效果不理想。

In some images there is noise or Grains and image quality is not so good. i have also tried a 1080p HD Web cam but it is not satisfactory.

请告诉我如何减少c#中图像的噪点。(asp.net c#)

Please tell me how can i reduce noise from image in c#.(asp.net c#)

谢谢

推荐答案

您好,

您可以使用上面的代码。

You can use the code above.

参数:图像为位图,噪声级别和噪声半径。

Parameters: Image as bitmap, level of noise and radius of noise.

public static Bitmap NoiseReduction(Bitmap original, int level, int radius)
        {
            int width = original.Width;
            int height = original.Height;

            Bitmap source = null;

            source = original.PixelFormat != PixelFormat.Format8bppIndexed
                ? BitmapManipulator.ConvertToGrayScale8bpp(original, false)
                : original;


            Bitmap destination = BitmapManipulator.CreateGrayscaleImage(width, height);

            BitmapData srcData = source.LockBits(new Rectangle(0, 0, width, height), ImageLockMode.ReadOnly,
                source.PixelFormat);
            BitmapData dstData = destination.LockBits(new Rectangle(0, 0, width, height), ImageLockMode.WriteOnly,
                destination.PixelFormat);

            int stride = srcData.Stride;
            byte pxVal;
            int matrixWidth = (radius*2 + 1)*(radius*2 + 1);
            var pxM = new byte[matrixWidth];
            int meanIndex = (matrixWidth - 1)/2;
            unsafe
            {
                var srcScan0 = (byte*) srcData.Scan0;
                var dstScan0 = (byte*) dstData.Scan0;

                for (int l = 0; l < level; l++)
                {
                    for (int y = radius; y < height - radius; y++)
                    {
                        byte* srcRow = srcScan0 + (y*stride);
                        byte* dstRow = dstScan0 + (y*stride);
                        for (int x = radius; x < width - radius; x++)
                        {
                            pxVal = srcRow[x];
                            int mIndex = 0;
                            for (int k = y - radius; k < y + radius + 1; k++)
                            {
                                byte* srcRowMatrix = srcScan0 + (k*stride);
                                for (int j = x - radius; j < x + radius + 1; j++)
                                {
                                    pxM[mIndex++] = srcRowMatrix[j];
                                }
                            }
                            Array.Sort(pxM);
                            dstRow[x] = pxM[meanIndex];
                        }
                    }
                }
            }

            destination.UnlockBits(dstData);
            source.UnlockBits(srcData);

            // Dispose of fastOriginal if not originally supplied bitmap
            if (source != original)
                source.Dispose();

            // Return
            return destination;
        }





这篇关于如何在Asp.net C#中减少图像噪声(jpg或Png)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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