卷积5x5 C#编码 [英] Convolution 5x5 C# coding

查看:190
本文介绍了卷积5x5 C#编码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,

我需要卷积5x5的c#代码,我希望得到一个示例源代码。我有一个3x3的例子,但我不知道把它改成5x5。



我从这篇文章得到3x3:

< a href =http://www.codeproject.com/Articles/2008/Image-Processing-for-Dummies-with-C-and-GDI-Part?msg=2959886#xx2959886xx>用C#和傻瓜进行图像处理GDI +第2部分 - 卷积滤波器



我尝试过:



  public   static   bool  Conv3x3(位图b,ConvMatrix m)
{
// 避免除以零错误
如果 0 == m.Factor) 返回 false ;

位图bSrc =(位图)b.Clone();

// GDI +仍然在于我们 - 返回格式是BGR,而不是RGB。
BitmapData bmData = b.LockBits( new 矩形( 0 0 ,b.Width,b .Height),ImageLockMode.ReadWrite,PixelFormat.Format24bppRgb);
BitmapData bmSrc = bSrc.LockBits( new Rectangle( 0 0 ,bSrc.Width,bSrc.Height),ImageLockMode.ReadWrite,PixelFormat.Format24bppRgb);

int stride = bmData.Stride;
int stride2 = stride * 2 ;
System。 IntPtr Scan0 = bmData.Scan0;
System。 IntPtr SrcScan0 = bmSrc.Scan0;

不安全
{
byte * p = ( byte *)( void *)Scan0;
byte * pSrc =( byte *)( void *)SrcScan0;

int nOffset = stride - b.Width * 3;
int nWidth = b.Width - 2 ;
int nHeight = b.Height - 2 ;

int nPixel;

for int y = 0 ; y < nHeight; ++ y)
{
for int x = 0 ; x < ; nWidth; ++ x)
{
nPixel =((((pSrc [ 2 ] * m.TopLeft)+ (pSrc [ 5 ] * m.TopMid)+(pSrc [ 8 ] * m.TopRight)+
(pSrc [ 2 + stride] * m.MidLeft)+(pSrc [ 5 + stride] * m.Pixel)+(pSrc [ 8 + stride] * m.MidRight)+
(pSrc [ 2 + stride2] * m.BottomLeft)+(pSrc [ 5 + stride2] * m.BottomMid)+(pSrc [ 8 + stride2] * m.BottomRight)) / m.Factor)+ m.Offset);

if (nPixel < 0 )nPixel = 0 ;
if (nPixel > 255 )nPixel = 255 ;

p [ 5 + stride] =( byte )nPixel;

nPixel =((((pSrc [ 1 ] * m.TopLeft)+(pSrc [ 4 ] * m.TopMid)+(pSrc [ 7 ] * m.TopRight)+
(pSrc [ 1 + stride] * m.MidLeft)+(pSrc [ 4 + stride] * m.Pixel)+(pSrc [< span class =code-digit> 7 + stride] * m.MidRight)+
(pSrc [ 1 + stride2] * m .BottomLeft)+(pSrc [ 4 + stride2] * m.BottomMid)+(pSrc [ 7 + stride2] * m.BottomRight))/ m.Factor)+ m.Offset);

if (nPixel < 0 )nPixel = 0 ;
if (nPixel > 255 )nPixel = 255 ;

p [ 4 + stride] =( byte )nPixel;

nPixel =((((pSrc [ 0 ] * m.TopLeft)+(pSrc [ 3 ] * m.TopMid)+(pSrc [ 6 ] * m.TopRight)+
(pSrc [ 0 + stride] * m.MidLeft)+(pSrc [ 3 + stride] * m.Pixel)+(pSrc [< span class =code-digit> 6 + stride] * m.MidRight)+
(pSrc [ 0 + stride2] * m .BottomLeft)+(pSrc [ 3 + stride2] * m.BottomMid)+(pSrc [ 6 + stride2] * m.BottomRight))/ m.Factor)+ m.Offset);

if (nPixel < 0 )nPixel = 0 ;
if (nPixel > 255 )nPixel = 255 ;

p [ 3 + stride] =( byte )nPixel;

p + = 3 ;
pSrc + = 3 ;
}
p + = nOffset;
pSrc + = nOffset;
}
}

b.UnlockBits(bmData);
bSrc.UnlockBits(bmSrc);

return true ;
}

解决方案

对不起,我没有检查你的代码。毕竟,你应该更好地使用调试器并自己找出你的错误。



我会给你另一个建议。你可以看到这种卷积是如何在着名的AForge.NET中实现的,或者直接使用这个库。请参阅:

http://www.aforgenet.com/framework/features/convolution_filters .html [ ^ ],

http://www.aforgenet.com /framework/docs/html/3b7d8422-2d79-8019-8933-a0472040c124.htm [ ^ ]。



参见:

AForge.NET开源框架 [ ^ ],

< a href =https://en.wikipedia.org/wiki/AForge.NET> AForge.NET - 维基百科,免费的百科全书 [ ^ ] ,

AForge.NET ::计算机视觉,人工智能,机器人技术 [ ^ ]。



-SA

Hello,
I need the c# code for convolution 5x5, I hope to get an example source code. I have got an example of a 3x3 but I do not know change it into 5x5.

I got 3x3 from this article :
Image Processing for Dummies with C# and GDI+ Part 2 - Convolution Filters

What I have tried:

public static bool Conv3x3(Bitmap b, ConvMatrix m)
		{
			// Avoid divide by zero errors
			if (0 == m.Factor) return false;

			Bitmap bSrc = (Bitmap)b.Clone(); 

			// GDI+ still lies to us - the return format is BGR, NOT RGB.
			BitmapData bmData = b.LockBits(new Rectangle(0, 0, b.Width, b.Height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
			BitmapData bmSrc = bSrc.LockBits(new Rectangle(0, 0, bSrc.Width, bSrc.Height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);

			int stride = bmData.Stride;
			int stride2 = stride * 2;
			System.IntPtr Scan0 = bmData.Scan0;
			System.IntPtr SrcScan0 = bmSrc.Scan0;

			unsafe
			{
				byte * p = (byte *)(void *)Scan0;
				byte * pSrc = (byte *)(void *)SrcScan0;

				int nOffset = stride - b.Width*3;
				int nWidth = b.Width - 2;
				int nHeight = b.Height - 2;

				int nPixel;

				for(int y=0;y < nHeight;++y)
				{
					for(int x=0; x < nWidth; ++x )
					{
						nPixel = ( ( ( (pSrc[2] * m.TopLeft) + (pSrc[5] * m.TopMid) + (pSrc[8] * m.TopRight) +
							(pSrc[2 + stride] * m.MidLeft) + (pSrc[5 + stride] * m.Pixel) + (pSrc[8 + stride] * m.MidRight) +
							(pSrc[2 + stride2] * m.BottomLeft) + (pSrc[5 + stride2] * m.BottomMid) + (pSrc[8 + stride2] * m.BottomRight)) / m.Factor) + m.Offset); 

						if (nPixel < 0) nPixel = 0;
						if (nPixel > 255) nPixel = 255;

						p[5 + stride]= (byte)nPixel;

						nPixel = ( ( ( (pSrc[1] * m.TopLeft) + (pSrc[4] * m.TopMid) + (pSrc[7] * m.TopRight) +
							(pSrc[1 + stride] * m.MidLeft) + (pSrc[4 + stride] * m.Pixel) + (pSrc[7 + stride] * m.MidRight) +
							(pSrc[1 + stride2] * m.BottomLeft) + (pSrc[4 + stride2] * m.BottomMid) + (pSrc[7 + stride2] * m.BottomRight)) / m.Factor) + m.Offset); 

						if (nPixel < 0) nPixel = 0;
						if (nPixel > 255) nPixel = 255;
							
						p[4 + stride] = (byte)nPixel;

						nPixel = ( ( ( (pSrc[0] * m.TopLeft) + (pSrc[3] * m.TopMid) + (pSrc[6] * m.TopRight) +
							(pSrc[0 + stride] * m.MidLeft) + (pSrc[3 + stride] * m.Pixel) + (pSrc[6 + stride] * m.MidRight) +
							(pSrc[0 + stride2] * m.BottomLeft) + (pSrc[3 + stride2] * m.BottomMid) + (pSrc[6 + stride2] * m.BottomRight)) / m.Factor) + m.Offset); 

						if (nPixel < 0) nPixel = 0;
						if (nPixel > 255) nPixel = 255;

						p[3 + stride] = (byte)nPixel;

						p += 3;
						pSrc += 3;
					}
					p += nOffset;
					pSrc += nOffset;
				}
			}

			b.UnlockBits(bmData);
			bSrc.UnlockBits(bmSrc);

			return true;
		}

解决方案

Sorry, I did not check up your code. After all, you should better use the debugger and find out your bugs by yourself.

I would offer you another advice instead. You can see how this kind of convolution is implemented in the famous AForge.NET, or use this library directly. Please see:
http://www.aforgenet.com/framework/features/convolution_filters.html[^],
http://www.aforgenet.com/framework/docs/html/3b7d8422-2d79-8019-8933-a0472040c124.htm[^].

See also:
AForge.NET open source framework[^],
AForge.NET — Wikipedia, the free encyclopedia[^],
AForge.NET :: Computer Vision, Artificial Intelligence, Robotics[^].

—SA


这篇关于卷积5x5 C#编码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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