在 C# 中使用 3x3 中值滤波器

发布于 2024-10-19 20:40:54 字数 1153 浏览 10 评论 0原文

你好,朋友们我正在尝试将 3x3 中值滤波器应用于 appxo 500x500 的指纹图像。 我正在使用指针来访问图像数据。但我真的不知道该怎么做。我非常了解这个概念,但是如果你在代码中帮助我,那将是很大的帮助。我在网上搜索,但没有得到任何帮助。谢谢

public void medianfilter(Bitmap image)
{ 
    Byte[,] rtemp = new Byte[3, 3];
    Byte[,] gtemp = new Byte[3, 3]; 
    Byte[,] btemp = new Byte[3, 3]; 
    BitmapData data = image.LockBits(new Rectangle(0, 0, image.Width, image.Height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb); 
    int stride = data.Stride; 
    unsafe {
           byte* imgPtr = (byte*)(void*)(data.Scan0);
           int nOffset = stride - image.Width * 3;
           for (int i = 0; i < image.Width; i++)
           { 
               for (int j = 0; j < image.Height; j++)
               { 
                   for (int x = i; x < 3 + i; x++)
                   {
                       for (int y = j; y < 3 + j; y++) {
                           rtemp[x, y] = imgPtr[0];
                           gtemp[x, y] = imgPtr[1];
                           btemp[x, y] = imgPtr[2];
                           imgPtr += 3; } } imgPtr += nOffset;
                    }
               }
           }
     }

Hello friends am trying to apply 3x3 median filter to fingerprint image of appxo 500x500.
I am using pointers to acess the image data. But i realy cant figure out how to do it. I know the concept very well, but if u guyz help me out in code it will be great help. I searched on net, but i dint get any help. thank you

public void medianfilter(Bitmap image)
{ 
    Byte[,] rtemp = new Byte[3, 3];
    Byte[,] gtemp = new Byte[3, 3]; 
    Byte[,] btemp = new Byte[3, 3]; 
    BitmapData data = image.LockBits(new Rectangle(0, 0, image.Width, image.Height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb); 
    int stride = data.Stride; 
    unsafe {
           byte* imgPtr = (byte*)(void*)(data.Scan0);
           int nOffset = stride - image.Width * 3;
           for (int i = 0; i < image.Width; i++)
           { 
               for (int j = 0; j < image.Height; j++)
               { 
                   for (int x = i; x < 3 + i; x++)
                   {
                       for (int y = j; y < 3 + j; y++) {
                           rtemp[x, y] = imgPtr[0];
                           gtemp[x, y] = imgPtr[1];
                           btemp[x, y] = imgPtr[2];
                           imgPtr += 3; } } imgPtr += nOffset;
                    }
               }
           }
     }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

难理解 2024-10-26 20:40:54

首先,您根本没有修改位图!

您需要在应用更改之前取消引用指针,然后必须解锁位图...

这是我在旧计算机图形课程中学到的内容。根据需要修改它。

unsafe
{
    //Go to first pixel, the cast is important
    byte* p = (byte*)imageData.Scan0.ToPointer();

    //For each line
    for (int y = 0; y < bmp.Height; y++)
    {
         //For each pixel (bmp.Width * 3) because jpg has R, G, and B value if the bitmap has an alpha do a *4 multiplier
         for (int x = 0; x < bmp.Width * 3; x++)
         {
                //Invert from the original image
                *p = (byte)(255 - *p);
                //Go to next pointer
                p++;
          }
          //Move pointer to the right end of the line and then go down to a new line
          //Skip the unused space
          p += offset;
     }
}
bmp.UnlockBits(imageData);
bmp.Save(path);

希望有帮助!

First of all you are not modifying the Bitmap at all!

You need to dereference the pointer before to apply the change and then you have to UNLOCK the bitmap...

Here's what I had in my old computer graphics course. Modify it as needed.

unsafe
{
    //Go to first pixel, the cast is important
    byte* p = (byte*)imageData.Scan0.ToPointer();

    //For each line
    for (int y = 0; y < bmp.Height; y++)
    {
         //For each pixel (bmp.Width * 3) because jpg has R, G, and B value if the bitmap has an alpha do a *4 multiplier
         for (int x = 0; x < bmp.Width * 3; x++)
         {
                //Invert from the original image
                *p = (byte)(255 - *p);
                //Go to next pointer
                p++;
          }
          //Move pointer to the right end of the line and then go down to a new line
          //Skip the unused space
          p += offset;
     }
}
bmp.UnlockBits(imageData);
bmp.Save(path);

Hope it helps!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文